#! /bin/bash
source /usr/share/nvm/init-nvm.sh

# Requirements
# Node v18
function build_static(){
    #Global ENV VAR: BUILD_NAME
    #Global ENV VAR: VITE_API_URL
    nvm use 18
    npm install --force 2>/dev/null
		set -x
    cat dotenv > .env
    npm run build 2>/dev/null
    cp htaccess dist/.htaccess
    zip -r $BUILD_NAME.zip dist/
		set +x
}

function deploy(){
    # Global Env Var: SYSTEM_HOSTINGER_HOSTNAME
    # Global Env Var: SYSTEM_HOSTINGER_SSH_USERNAME
    # Global Env Var: SYSTEM_HOSTINGER_SSH_PORT
    # Global Env Var: BUILD_NAME
    INSTALL_PATH="public_html/subdomains/console/"
    set -x
      scp -i ~/.ssh/id_rsa -P $SYSTEM_HOSTINGER_SSH_PORT ./$BUILD_NAME.zip "$SYSTEM_HOSTINGER_SSH_USERNAME"@"$SYSTEM_HOSTINGER_HOSTNAME":~/$INSTALL_PATH
      scp -i ~/.ssh/id_rsa -P $SYSTEM_HOSTINGER_SSH_PORT ./scripts/ssh_install_script.sh "$SYSTEM_HOSTINGER_SSH_USERNAME"@"$SYSTEM_HOSTINGER_HOSTNAME":~/$INSTALL_PATH
      ssh -i ~/.ssh/id_rsa -p $SYSTEM_HOSTINGER_SSH_PORT "$SYSTEM_HOSTINGER_SSH_USERNAME"@"$SYSTEM_HOSTINGER_HOSTNAME" "cd ~/$INSTALL_PATH && bash ssh_install_script.sh && rm ssh_install_script.sh"
    set +x
}
