#!/bin/bash

help()
{
    echo ""
    echo "Deploying the frontend to Nginx."
    echo "Usage: $0 {user@ip:path/to/html} [Password]"
    echo -e "\t{user@ip:path/to/html}\tIP and path to deploy"
    echo -e "\t[Password]\t\tSSH Password"
    echo ""
    echo "e.g. ./scripts/bootstrap_ui.sh root@172.xxx.xxx.xxx:/usr/share/tcms/html mypassword"
    echo ""
    echo "NOTE: If failed, please install sshpass manually."
    exit 0
}

if [ $# = 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    help
fi

which sshpass &>/dev/null || brew install hudochenkov/sshpass/sshpass

url=(${1//:/ })
password=${2:-}

set -xeuo pipefail

make ui
sshpass -p "$password" scp bin/html.tar.gz "$1"
sshpass -p "$password" ssh -T "${url[0]}" << EOF
set -xeuo pipefail
cd "${url[1]}"
tar xvf html.tar.gz
rm html.tar.gz
EOF
