#!/bin/bash
set -e
cd "$(dirname "$0")"
ROOT="$(pwd)"
NODE_DIR="$HOME/.local/node"
CF_DIR="$HOME/.local/cloudflared"
NODE_VER="20.18.1"
CF_VER="2024.12.2"
NODE_BIN="$NODE_DIR/bin/node"

load_env_file() {
  local f="$1"
  [ -f "$f" ] || return 0
  while IFS= read -r line || [ -n "$line" ]; do
    line="${line%$'\r'}"
    case "$line" in
      ''|\#*) continue ;;
    esac
    local key="${line%%=*}"
    local val="${line#*=}"
    key="$(echo "$key" | tr -d '[:space:]')"
    [ -n "$key" ] || continue
    if [ -n "${!key+x}" ] && [ -n "${!key}" ]; then
      continue
    fi
    if [ -z "$val" ]; then
      continue
    fi
    export "$key=$val"
  done < "$f"
}

if [ -f .env ]; then
  load_env_file .env
elif [ -f .env.example ]; then
  cp .env.example .env
  load_env_file .env
fi

need_node=0
if ! command -v node >/dev/null 2>&1; then
  need_node=1
elif ! node -e "process.exit(Number(process.versions.node.split('.')[0])<18?1:0)" 2>/dev/null; then
  need_node=1
fi

if [ "$need_node" = "1" ]; then
  if [ ! -x "$NODE_BIN" ]; then
    echo "installing node $NODE_VER into $NODE_DIR ..."
    mkdir -p "$NODE_DIR"
    arch="$(uname -m)"
    case "$arch" in
      x86_64|amd64) node_arch="x64" ;;
      aarch64|arm64) node_arch="arm64" ;;
      *) echo "unsupported arch: $arch"; exit 1 ;;
    esac
    url="https://nodejs.org/dist/v${NODE_VER}/node-v${NODE_VER}-linux-${node_arch}.tar.gz"
    tmp="$HOME/node-download.tar.gz"
    if command -v curl >/dev/null 2>&1; then
      curl -fsSL "$url" -o "$tmp"
    elif command -v wget >/dev/null 2>&1; then
      wget -qO "$tmp" "$url"
    else
      echo "need curl or wget to install node"
      exit 1
    fi
    tar -xzf "$tmp" -C "$NODE_DIR" --strip-components=1
    rm -f "$tmp"
  fi
  export PATH="$NODE_DIR/bin:$PATH"
fi

echo "node $(node -v) / npm $(npm -v)"

if [ -n "${SERVER_PORT:-}" ]; then
  export PORT="$SERVER_PORT"
fi
if [ -z "${PORT:-}" ]; then
  export PORT=3080
fi
if [ -z "${PUBLIC_HOST:-}" ]; then
  export PUBLIC_HOST="krying.opik.net"
fi

if [ ! -d node_modules ] || [ ! -f node_modules/.package-lock.json ]; then
  npm install --no-audit --no-fund
else
  npm install --prefer-offline --no-audit --no-fund
fi

install_cloudflared() {
  if [ -x "$CF_DIR/cloudflared" ]; then
    return 0
  fi
  echo "installing cloudflared $CF_VER ..."
  mkdir -p "$CF_DIR"
  arch="$(uname -m)"
  case "$arch" in
    x86_64|amd64) cf_arch="amd64" ;;
    aarch64|arm64) cf_arch="arm64" ;;
    *) echo "unsupported arch for cloudflared: $arch"; return 1 ;;
  esac
  url="https://github.com/cloudflare/cloudflared/releases/download/${CF_VER}/cloudflared-linux-${cf_arch}"
  tmp="$HOME/cloudflared.bin"
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$url" -o "$tmp"
  else
    wget -qO "$tmp" "$url"
  fi
  chmod +x "$tmp"
  mv "$tmp" "$CF_DIR/cloudflared"
}

TOKEN="${TUNNEL_TOKEN:-${CLOUDFLARE_TUNNEL_TOKEN:-}}"
USE_TUNNEL="$(echo "${USE_TUNNEL:-0}" | tr '[:upper:]' '[:lower:]')"

echo ""
echo "starting krying on port ${PORT} ..."
echo "open: http://${PUBLIC_HOST}:${PORT}"
echo "also: http://vps5.eas-x.com:${PORT}"
echo ""
exec node server.js
