#!/bin/bash
# PeerCortex deploy script
# Usage: ./deploy/deploy.sh
# Requires: gh CLI logged in, SSH access to erik

set -e
REMOTE=erik
APP_DIR=/opt/peercortex-app

echo "[deploy] Pushing to GitHub..."
git push origin main

echo "[deploy] Pulling on ${REMOTE}..."
GH_TOKEN=$(gh auth token)
GH_USER=$(gh api user --jq .login)
ssh ${REMOTE} "cd ${APP_DIR} && \
  git remote set-url origin https://${GH_USER}:${GH_TOKEN}@github.com/${GH_USER}/PeerCortex.git && \
  git pull origin main && \
  git remote set-url origin https://github.com/${GH_USER}/PeerCortex.git && \
  npm install --omit=dev --silent && \
  pm2 restart peercortex"

echo "[deploy] Done."
