#!/system/bin/sh
echo -n "\033[33;1m"
#去掉信息s全家
# 指定要检查的目录
sgame_DIR="/data/data/com.tencent.tmgp.sgame"
TARGET_DIR="$sgame_DIR/files/ano_tmp"
TARGET_DIR2="$TARGET_DIR/custom_cache"
DIR="/storage/emulated/0/Android/data/com.tencent.tmgp.sgame"
TARGET_DIR3="$DIR/files"

echo -n "\033[36;1m"
# 检查目录是否存在
if [ -d "$TARGET_DIR2" ]; then
  # 进入目标目录
  cd "$TARGET_DIR2"
  echo "[-] 开始运行[1/4]\n"
  # 遍历目录中的所有文件和子目录
  for item in *; do
    # 检查当前条目的名称是否为files
    # if [ "$item" != "files" ]; then
      # 根据当前条目的类型执行删除操作
      if [ -d "$item" ]; then
        echo "[-] 删除目录: $item"
        rm -rf "$item"
      elif [ -f "$item" ]; then
        echo "[-] 删除文件: $item"
        rm "$item"
      fi
    # fi
  done
  echo -n "\033[32;1m"
  echo "\n[-] 完成!"
else
  echo -n "\033[31;1m"
  echo "\n[!] [$TARGET_DIR2] 目录不存在"
  #exit 1
fi

echo "\033[36;1m"
# 检查目录是否存在
if [ -d "$TARGET_DIR" ]; then
  # 进入目标目录
  cd "$TARGET_DIR"
echo "[-] 开始运行[2/4]\n"
  # 遍历目录中的所有文件和子目录
  for item in *; do
    # 检查当前条目的名称是否为QtsVFSCache
    # if [ "$item" != "QtsVFSCache" ]; then
      # 根据当前条目的类型执行删除操作
      if [ -d "$item" ]; then
        echo "[-] 删除目录: $item"
        rm -rf "$item"
      elif [ -f "$item" ]; then
        echo "[-] 删除文件: $item"
        rm "$item"
      fi
    # fi
  done
  echo -n "\033[32;1m"
  echo "\n[-] 完成!"
else
  echo -n "\033[31;1m"
  echo "\n[!] [$TARGET_DIR] 目录不存在"
  # exit 1
fi

echo "\033[36;1m"
# 检查目录是否存在
if [ -d "$TARGET_DIR3" ]; then
  # 进入目标目录
  cd "$TARGET_DIR3"
echo "[-] 开始运行[3/4]\n"
  # 遍历目录中的所有文件和子目录
  for item in *; do
      # 根据当前条目的类型执行删除操作
      if [ -d "$item" ]; then
        echo "[-] 删除目录: $item"
        rm -rf "$item"
      elif [ -f "$item" ]; then
        echo "[-] 删除文件: $item"
        rm "$item"
      fi
  done
  echo -n "\033[32;1m"
  echo "\n[-] 完成!"
else
  echo -n "\033[31;1m"
  echo "\n[!] [$TARGET_DIR3] 目录不存在"
  # exit 1
fi

echo "\033[36;1m"
echo "[-] 开始运行[4/4]\n"
if [ -d $sgame_DIR/cache ]; then
	echo "[-] 删除缓存目录: $sgame_DIR/cache"
    rm -rf "$sgame_DIR/cache"
fi
if [ -d $DIR/cache ]; then
	echo "[-] 删除缓存目录: $DIR/cache"
    rm -rf "$DIR/cache"
fi
if [ -d $sgame_DIR/files/qm ]; then
	echo "[-] 删除缓存目录: $sgame_DIR/files/qm"
    rm -rf "$sgame_DIR/files/qm"
fi
if [ -d $sgame_DIR/databases ]; then
	echo "[-] 删除缓存目录: $sgame_DIR/databases"
    rm -rf "$sgame_DIR/databases"
fi
if [ -d $sgame_DIR/shared_prefs ]; then
	echo "[-] 删除缓存目录: $sgame_DIR/shared_prefs"
    rm -rf "$sgame_DIR/shared_prefs"
fi
if [ ! -d $sgame_DIR/cache ] && [ ! -d $DIR/cache ]; then
	echo -n "\033[32;1m"
	echo "\n[-] 完成!"
else
  echo -n "\033[31;1m"
  echo "\n[!] 清理缓存失败"
  # exit 1
fi

echo "\033[36;1m"
echo "[-] 开始修改ID\n"
PKG="com.tencent.tmgp.sgame"
ID=$(cat /data/system/users/0/settings_ssaid.xml | grep "$PKG" | sed 's/^.*com.tencent.tmgp.sgame\///; s/^\(.\{20\}\).*/\1/' | sed 's/[^a-zA-Z0-9]//g')

if [ $? = 0 ]; then
echo "[-] 王者ID: $ID\n"
for i in $(seq 16)
do P=$P$(uuidgen|head -c 1|tr '-' -d)
done
echo "[-] 生成随机ID: $P\n"
sed -i s/$ID/$P/g /data/system/users/0/settings_ssaid.xml
if [ $? = 0 ]; then
	echo "[-] ID已修改\n"
	echo "[-] 重启手机生效\n"
else
	echo -n "\033[31;1m"
	echo "\n[!] 写入随机ID失败,请联系作者"
fi
else
echo -n "\033[31;1m"
echo "\n[!] 获取原ID失败\n请检查王者是否安装\n若已安装\n请在第1次运行游戏后再试"
fi

iptables -F
iptables -X
iptables -Z

echo -n "\033[33;1m"
echo "[-] 首次使用执行一次以后不用执行\n"
echo "[-] 清理+改ID完成"
echo "[-] 请按下键盘回车重启手机\n"

echo "\033[36;1m"
read sth
if [ "$sth" = "" ]; then
  cs=3
  until [ $cs -eq 0 ]
  do
    echo "[-] $cs后重启手机\n"
    let cs--
    sleep 1.2
  done
  reboot
fi