#!/system/bin/sh

PACKAGE1="com.hortor.juliancysj"
PACKAGE2="com.e4bcf981d09"
RETAIN_DIRS="swap enterprise"
TARGET_BASE="/data/user/0/${PACKAGE1}/app_flutter"

if [ "$(id -u)" -ne 0 ]; then
    echo "需要root"
fi

# 先杀进程
echo "杀进程 $PACKAGE1"
killall $PACKAGE1 2>/dev/null
am force-stop $PACKAGE1

echo "杀进程 $PACKAGE2"
killall $PACKAGE2 2>/dev/null
am force-stop $PACKAGE2

sleep 2

# 清理
if [ -d "$TARGET_BASE" ]; then
    echo "清理目录: $TARGET_BASE"
    echo "保留: $RETAIN_DIRS"
    cd "$TARGET_BASE" || exit 1

    for item in * .*; do
        [ "$item" = "." ] || [ "$item" = ".." ] && continue

        keep=0
        for retain in $RETAIN_DIRS; do
            if [ "$item" = "$retain" ]; then
                keep=1
                break
            fi
        done

        if [ $keep -eq 0 ]; then
            echo "删除: $item"
            rm -rf "$item"
        else
            echo "保留: $item"
        fi
    done
fi

# 启动
echo "启动 $PACKAGE1，手动重新开一下创游就行了"
monkey -p $PACKAGE1 1 >/dev/null 2>&1 &

echo "启动 $PACKAGE2"
monkey -p $PACKAGE2 1 >/dev/null 2>&1 &

echo "完事"