#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo 'Usage: syncAndBuild.sh <hostname>|build'
    exit 1
fi

SERVICE_NAME="portal"

if [[ $1 == build ]]; then
    # This part is meant to run on the VPS
    source ~/.nvm/nvm.sh
    nvm install 18
    nvm use 18
    npm install
    npm run build-vps

else
    # This part is meant to run on your local dev machine
    SSH_HOST="k2geo@${1}.jmapcloud.io"

    RSYNC_OPTIONS=(-a -v --delete --exclude="node_modules" --exclude="public" --exclude=".git" --exclude="build/env-config.js")
    RSYNC_SOURCE="."
    RSYNC_DESTINATION="${SSH_HOST}:${SERVICE_NAME}"

    rsync "${RSYNC_OPTIONS[@]}" "$RSYNC_SOURCE" "$RSYNC_DESTINATION" && ssh "$SSH_HOST" "cd $SERVICE_NAME && ./syncAndBuild.sh build"
fi
