#!/system/bin/sh

PACKAGE1="com.hortor.juliancysj"
TMP_FILE="/data/user/0/com.hortor.juliancysj/app_flutter/swap/cysj2.tmp"

if [ "$(id -u)" -ne 0 ]; then
    echo "警告：需要root权限"
fi

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

sleep 2

# 备份原文件内容
if [ -f "$TMP_FILE" ]; then
    echo "备份文件内容"
    CONTENT=$(cat "$TMP_FILE")
    rm -f "$TMP_FILE"
    echo "已删除原文件"
else
    CONTENT=""
    echo "文件不存在"
fi

# 启动游戏
echo "启动 $PACKAGE1"
monkey -p $PACKAGE1 1 >/dev/null 2>&1 &

echo "等待5秒..."
sleep 5

# 杀游戏
echo "停止 $PACKAGE1"
killall $PACKAGE1 2>/dev/null
am force-stop $PACKAGE1

sleep 1

# 恢复原文件
if [ -n "$CONTENT" ]; then
    echo "$CONTENT" > "$TMP_FILE"
    echo "已恢复原文件"
fi

# 重启游戏
echo "重新启动 $PACKAGE1"
monkey -p $PACKAGE1 1 >/dev/null 2>&1 &

echo "完事"