#!/bin/bash
# Run on server as dulcfokx: bash ~/chart.tradingbite.net/deploy/chart-server-install.sh
set -eu

APP_ROOT="${HOME}/chart.tradingbite.net"
cd "$APP_ROOT"

export NVM_DIR="${HOME}/.nvm"
# shellcheck source=/dev/null
[ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh"
nvm use 22

# shellcheck source=/dev/null
if [ -f /opt/rh/gcc-toolset-11/enable ]; then
  source /opt/rh/gcc-toolset-11/enable
fi

if [ ! -f .env ]; then
  echo "Missing .env — create ${APP_ROOT}/.env with OANDA_API_TOKEN and OANDA_ENVIRONMENT=practice"
  exit 1
fi

echo "Node: $(node -v)"
echo "GCC: $(gcc -v 2>&1 | head -1)"

rm -rf node_modules
if [ -f package-lock.json ]; then
  npm ci
else
  npm install
fi

npm run build

echo "Build OK. Next: bash deploy/chart-server-start.sh"
