#!/bin/bash
##
if [ $# -lt 2 ]
then
    echo "Usage: runSetupNodeJS.sh <MW Pulbic IP> <DB Public IP>"
    exit 1
fi
MW_PUBLIC_IP=$1
DB_PUBLIC_IP=$2
#
echo "************************************************"
echo "* Copy over needed files ...."
echo "************************************************"
scp -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key installNodeJS.sh opc@${MW_PUBLIC_IP}:/home/opc/.
scp -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key NodeJSDemo.tar.gz opc@${MW_PUBLIC_IP}:/home/opc/.
scp -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key startNode.sh opc@${MW_PUBLIC_IP}:/home/opc/.
echo "************************************************"
echo "* Install NodeJS ...."
echo "************************************************"
ssh -t -t -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key opc@${MW_PUBLIC_IP} "sudo sh /home/opc/installNodeJS.sh"
#
echo "************************************************"
echo "* Open firewall for Port 8080 ...."
echo "************************************************"
ssh -t -t -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key opc@${MW_PUBLIC_IP} "sudo firewall-cmd --permanent --add-port=8080/tcp"
ssh -t -t -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key opc@${MW_PUBLIC_IP} "sudo firewall-cmd --reload"
#
echo "************************************************"
echo "* Start Node server ...."
echo "************************************************"
ssh -t -t -o "ServerAliveInterval 60" -o "StrictHostKeyChecking no" -i ../Keys/oci_instance_key opc@${MW_PUBLIC_IP} "sh /home/opc/startNode.sh ${DB_PUBLIC_IP}"
#
echo "**********************************************"
echo "* NodeJS Setup is Complete ...."
echo "**********************************************"
