#!/bin/bash
# AirAudio — rawfile 运行数据部署(随 hap 分发,首启由 FileManager 拷入沙箱 share/)
# 数据源:audacity 源码树(nyquist/plug-ins/locale)+ AirGimp 栈共享(fonts/schemas)
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/env.sh" >/dev/null

AU="$AIRAUDIO_SOURCES/audacity"
RAW="$AIRAUDIO_ROOT/entry/src/main/resources/rawfile/audacity-share"
AG_RAW="/Users/andy/DevEcoStudioProjects/AirGimp/entry/src/main/resources/rawfile/gimp-share"

rm -rf "$RAW"
mkdir -p "$RAW/audacity"

# 1) Nyquist 运行时(lisp 库)
cp -R "$AU/nyquist" "$RAW/audacity/nyquist"
rm -rf "$RAW/audacity/nyquist/CMakeLists.txt"

# 2) Nyquist 效果脚本
cp -R "$AU/plug-ins" "$RAW/audacity/plug-ins"

# 3) 翻译(zh_CN 优先 + 英文兜底)
mkdir -p "$RAW/audacity/locale/zh_CN/LC_MESSAGES"
if [ -f "$AU/locale/zh_CN.po" ]; then
    msgfmt "$AU/locale/zh_CN.po" -o "$RAW/audacity/locale/zh_CN/LC_MESSAGES/audacity.mo" 2>/dev/null || \
        rm -f "$RAW/audacity/locale/zh_CN/LC_MESSAGES/audacity.mo"
fi

# 4) 预置配置(首启中文语言 + 禁效果扫描;EntryAbility 仅在无既有配置时落盘)
mkdir -p "$RAW/config"
cat > "$RAW/config/audacity.cfg" <<'EOF'
[Locale]
Language=zh_CN
[Effects]
SkipEffectsScanAtStartup=1
[GUI]
ShowSplashScreen=0
EOF

# 5) GTK/GLib 栈共享数据(fonts.conf、gschemas.compiled——复用 AirGimp rawfile)
mkdir -p "$RAW/etc/fonts" "$RAW/glib-2.0/schemas"
cp "$AG_RAW/etc/fonts/fonts.conf" "$RAW/etc/fonts/" 2>/dev/null || true
cp "$AG_RAW/glib-2.0/schemas/gschemas.compiled" "$RAW/glib-2.0/schemas/" 2>/dev/null || true

du -sh "$RAW"
echo "rawfile 数据部署完成"