#!/system/bin/sh

MODULE_DIR="/data/adb/modules/RaiRin-AI"
RAIRIN="$MODULE_DIR/engine"
TMP_ICON="/data/local/tmp/rairin.webp"
TMP="/data/local/tmp/RaiRin-AI"
PERM_SCRIPT="$MODULE_DIR/permission.sh"
ACTION="$MODULE_DIR/action.sh"
logfile="/data/adb/modules/RaiRin-AI/vsync.log"
LICENSE_FILE="/data/adb/modules/RaiRin-AI/engine/license.txt"
TMP_LICENSE="/data/local/tmp/license.txt"
while [ "$(getprop sys.boot_completed)" != "1" ]; do
    sleep 10
done
sleep 10
echo "[VSYNC] === Disable VSYNC Service Start ===" > "$logfile"
props=(
  "debug.cpurend.vsync false"
  "debug.egl.vsync false"
  "debug.hwui.vsync false"
  "debug.hwui.disable_vsync true"
  "debug.hwui.skip_vsync true"
  "debug.sf.disable_vsync true"
  "debug.egl.swapinterval 0"
  "debug.hwui.render_thread false"
  "debug.sf.disable_backpressure 1"
  "debug.sf.enable_gl_backpressure 0"
  "vendor.debug.egl.vsync false"
  "vendor.debug.sf.vsync false"
  "vendor.debug.mali.vsync 0"
  "vendor.debug.mali.skip_vsync 1"
  "debug.pvr.vsync 0"
  "persist.sys.gpu_rendering opengl"
)

for entry in "${props[@]}"; do
  key=$(echo "$entry" | awk '{print $1}')
  val=$(echo "$entry" | awk '{print $2}')  
  current=$(getprop "$key")
  if [ -n "$current" ]; then
    setprop "$key" "$val"
    echo "[VSYNC] Applied: $key=$val (old=$current)" >> "$logfile"
  else
    echo "[VSYNC] Skipped: $key (not found)" >> "$logfile"
  fi
done
echo "[VSYNC] === Safe Disable VSYNC Completed ===" >> "$logfile"
chmod 0755 "$PERM_SCRIPT"
chmod 0755 "$ACTION"
sh "$PERM_SCRIPT"
"$MODULE_DIR/cortex/log/save.sh"
sh "$MODULE_DIR/cortex/soc/soc.sh"
/data/local/tmp/RaiRin-AI/kernel_panic.sh
/data/adb/modules/RaiRin-AI/cortex/cpu/norgov.sh
/data/adb/modules/RaiRin-AI/cortex/thermal/kaname.sh

echo "Default" > "/data/adb/modules/RaiRin-AI/cortex/net/booster.txt"
echo "enabled" > "/data/local/tmp/RaiRin-AI/log_option.txt"
echo "disabled" > "/data/adb/modules/RaiRin-AI/cortex/dex2oat/status.txt"
if [ -s "$TMP_LICENSE" ]; then
    cp "$TMP_LICENSE" "$LICENSE_FILE"
else
    echo ""
fi
sleep 2
"$MODULE_DIR/cortex/render/skiagl"
nohup "$RAIRIN/RaiRin-game" >/dev/null 2>&1 &
nohup "$RAIRIN/RaiRin-bat" >/dev/null 2>&1 &
nohup "$RAIRIN/RaiRin-thermal" >/dev/null 2>&1 &
sleep 5
nohup "$RAIRIN/RaiRin-main" >/dev/null 2>&1 &
