#!/bin/bash

set -euo pipefail

repoRoot='/mnt/d/Sites/RadioPlayer/web'
localDistFolder="$repoRoot/dist/."
remoteFolder='/opt/www/virtual/RadioPlayer/'
remoteServer='klevze@server.klevze.si'

run_build() {
      if grep -qi microsoft /proc/version 2>/dev/null && command -v powershell.exe >/dev/null 2>&1; then
            local windowsRepoRoot
            windowsRepoRoot="$(wslpath -w "$repoRoot")"
            powershell.exe -NoProfile -Command "Set-Location -LiteralPath '$windowsRepoRoot'; npm run build"
            return
      fi

      npm run build
}

run_build

if [[ "${SKIP_DEPLOY:-0}" == "1" ]]; then
      echo "Build completed; skipping deploy because SKIP_DEPLOY=1"
      exit 0
fi

rsync -avz \
      --delete \
      --chmod=D755,F644 \
      --exclude ".phpintel/" \
      --exclude ".env" \
      --exclude "node_modules" \
      --exclude ".git/" \
      --exclude ".gemini" \
      --exclude ".github" \
      --exclude ".vscode" \
      -e ssh  \
      $localDistFolder \
      $remoteServer:$remoteFolder/

echo "Deployed to $remoteServer:$remoteFolder"
      