#!/bin/sh

# After running this script, you can run ./Start_Server_Monitor_In_Background to start the server monitor agent in the background an automated way.
# For example, you can add the line \"${PWD}/Start_Server_Monitor_In_Background\" to your /etc/rc.local, to launch the server monitor whenever the machine boots.
# Or remotely using something like: ssh ${HOSTNAME} \'${PWD}/Start_Server_Monitor_In_Background\'
# This script should fail silently if the server monitor is already running."
# The ASA will run inside a screen session named WPASA.  To terminate the ASA, you will need to connect to this screen session (using 'screen -Dr WPASA') and type 'quit').

# Try to find the root install directory.
[ -f ./Server_Monitor_Agent ] || cd ..
[ -f ./Server_Monitor_Agent ] || cd ..

# Check that we have the resources we need
if [ ! -f ./Server_Monitor_Agent ]
then
    echo "./Server_Monitor_Agent not found."
    echo "You should run this script from the root of the installation directory, and only for the linux Server Monitor Agent."
    exit 1
fi

if [ ! -f ./config/start/wpasa-start.template ]
then
    echo "./config/start/wpasa-start.template not found."
    exit 1
fi

ASA_DIR="$PWD"
ASA_USER=`whoami`
HOSTNAME=`hostname --fqdn`

echo "Installation directory is \"$ASA_DIR\"."
echo "Username is \"$ASA_USER\"."

sed -e "s:ASA_DIR:$ASA_DIR:g" -e "s:ASA_USER:${ASA_USER}:g" < ./config/start/wpasa-start.template > ./Start_Server_Monitor_In_Background &&
chmod u+x ./Start_Server_Monitor_In_Background ||
echo "Script generation failed."

