set -eu
trap 'echo -e "\nError occurred on line $LINENO:\n$BASH_COMMAND"; exit 1' ERR

APP_DIR='/apps/frpc'
ARCH=$(uname -m)
deviceNo=$(lib::getDeviceNo "$CONFIG_FILE")

bin='frpc'
if [ "${ARCH}" = 'aarch64' ]; then
  bin="frpc-${ARCH}"
fi

mkdir -p $APP_DIR || true

lib::rsyncDownload /frp/$bin "${APP_DIR}/"
if [ "${bin}" != 'frpc' ]; then
  mv $APP_DIR/$bin $APP_DIR/frpc
fi
lib::rsyncDownload /frp/frpc.toml "${APP_DIR}/"
lib::rsyncDownload /frp/frpc.service /etc/systemd/system/

sed -i "s/device-000/device-$deviceNo/g" "${APP_DIR}/frpc.toml"

chmod +x $APP_DIR/frpc
systemctl daemon-reload
chmod +x /etc/systemd/system/frpc.service

systemctl enable frpc
if systemctl is-active --quiet frpc; then
  echo "frpc 服务已存在"
else
  systemctl start frpc
fi