#!/bin/sh

 if [ $(id -u) != 0 ]; then
     echo "\n请使用Root权限执行 !"
     exit 1
 fi
PID=$(pgrep -f "com.tencent.tmgp.pubgmhd")
if [ -z "$PID" ]; then
    echo "\n[-] 游戏未运行，请先启动游戏！"
        echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    echo "\n[-] 游戏未运行，请先启动游戏！"
    exit 1
else
    echo "\n[+] 检测到游戏进程 PID: $PID，继续执行..."
fi
path=`pm path com.tencent.tmgp.pubgmhd | sed -E  's/(.*):(.*)/\2/'| sed -E 's/(.*)base.apk/\1/'`"lib/arm64/" 
chmod 000 /data/data/com.tencent.tmgp.pubgmhd/files/ano_tmp
find /data/data/com.tencent.tmgp.pubgmhd/ -type f -name "*.so" -exec rm -f {} +
file_path="${path}inject_arm64"
if [ -e "$file_path" ]; then 
    md5_inject=$(md5sum "${path}inject_arm64" | awk '{print $1}')
    md5_arm64_inject=$(md5sum inject_arm64 | awk '{print $1}')
    if [ "$md5_inject" != "$md5_arm64_inject" ]; then
    cp inject_arm64 "${path}"
    chmod 777 "${path}inject_arm64"
    echo "已更新新版本注入器"
    else
    echo "注入器已存在"
fi
 
else
    echo "注入器不存在，正在执行写入操作......"
    cp inject_arm64 "${path}"
    chmod 777 "${path}inject_arm64"
fi

md5_arm64=$(md5sum libTrack.so | awk '{print $1}')
echo "libTrack.so 的 MD5 值： $md5_arm64" 

if [ -e "${path}libTrack.so" ]; then
    md5_target=$(md5sum "${path}libTrack.so" | awk '{print $1}')
    echo "游戏目录/libTrack.so 的 MD5 值： $md5_target"
    if [ "$md5_arm64" != "$md5_target" ]; then
        cp arm64-v8a/libTrack.so "${path}" 
        chmod 755 "${path}libTrack.so"
        echo "MD5 值不匹配，已将 libTrack.so 复制到游戏目录"
        echo "准备注入......"
    fi
else
    cp arm64-v8a/libTrack.so "${path}"
    chmod 755 "${path}libTrack.so"
    echo "准备注入......"
fi

if [ -f "${path}libTrack.so" ] && [ "$(stat -c '%a' ${path}libTrack.so)" = "755" ]; then
    echo "[+] 动态库文件正常"
else
    echo "[-] 动态库文件不正确"
    exit 1
fi


"${path}inject_arm64" -n com.tencent.tmgp.pubgmhd -so "${path}libTrack.so"