#!/usr/bin/env bash

SettingsFile=settings

if ! [ -f ${SettingsFile} ];then
    echo "export OpenContentIp=" > ${SettingsFile}
    echo "export pemfile=" >> ${SettingsFile}
    echo "ALERT: Created ${SettingsFile}, please fill in 'host' and 'pemfile' !"
    echo ""
    exit
fi


function usage() {
        echo ${1}
        echo ""
        echo "Usage:"
        echo "${0} command [host]"
        echo ""
        echo "command : admin|xlibris|apidocs|terminal|replicator|rabbitmq"
        echo ""
        exit 1
}

if [ -z ${1} ];then
  usage "ERROR: Missing command"
fi

command=${1}
if [ -z ${2} ];then
    . ${SettingsFile}
	host=${OpenContentIp}
else
	host=${2}
fi

case $command in

	health)
		 curl -s http://${host}:8080/opencontent/health | jq
		 ;;
	support)
		 eval ${BrowserCommand} http://${host}/support
		 ;;
	admin)
		 eval ${BrowserCommand} http://${host}/admin
		 ;;
	xlibris)
		 eval ${BrowserCommand} http://${host}/client
		 ;;
	apidocs)
		 eval ${BrowserCommand} http://${host}:8080/opencontent/apidocs/
		 ;;
	terminal)
	    if ! [ -z ${pemfile} ]; then
		 ssh -i ${pemfile} ec2-user@${host}
		else
		 echo "No pem file set ..."
		fi
		;;
	replicator)
		 eval ${BrowserCommand} http://${host}:8180/replication
		 ;;
	rabbitmq)
		 eval ${BrowserCommand} http://${host}:15672
		 ;;
	*)
		usage "ERROR: Unknown Command: ${command}"

		;;

esac
