#!/bin/bash
VSCODE_PATCH_DIR="$HOME/.vscode-patch"
if [ ! -d "$VSCODE_PATCH_DIR/lib" ]; then
  echo "错误：补丁文件夹应放在这儿：$HOME/.vscode-patch/"
  exit 1
fi
VSCODE_APP_PATH=""
if [ -n "$1" ]; then
  VSCODE_APP_PATH="$1"
else
  read -p "指定VSCode App路径: " VSCODE_APP_PATH
fi
echo "[VSCode应用-补丁]"
echo "-- VSCode Path: $VSCODE_APP_PATH"
if [ ! -f "$VSCODE_APP_PATH/code" ]; then
  echo "错误：指定App路径下不存在VSCode可执行文件"
  exit 1
fi
echo "-- 找到code程序，即将补丁code程序..."
if [ ! -f "$VSCODE_APP_PATH/code.bak" ]; then
  echo "-- 备份原始code程序..."
  cp "$VSCODE_APP_PATH/code" "$VSCODE_APP_PATH/code.bak"
else
  echo "-- 已存在code.bak原始备份"
fi
echo "-- 开始打补丁..."
$VSCODE_PATCH_DIR/tools/patchelf \
  --set-interpreter "$VSCODE_PATCH_DIR/lib/ld-linux-x86-64.so.2" \
  --set-rpath "\$ORIGIN:$HOME/.vscode-patch/lib" \
  "$VSCODE_APP_PATH/code"
echo "-- 补丁完成！"

echo "VSCode App 已做旧版本兼容动态库补丁（修改code文件，原文件为code.bak）\n补丁数据路径$HOME/.vscode-patch" >>$VSCODE_APP_PATH/Patch说明.md
