#!/bin/bash

# setup an environment (or maybe not?) then run a script,
# inform the admin at the end

# get loginForScript/scriptToRun
destMachine=$1
scriptToRun=$2

if [ "xxx$destMachine" = "xxx" ] ; then
	echo "Usage: $0 <destMachine> <scriptToRun>"
	exit 1
fi

if [ "xxx$scriptToRun" = "xxx" ] ; then
	echo "Usage: $0 <destMachine> <scriptToRun>"
	exit 1
fi

if [ $destMachine = $HOSTNAME ] ; then
	rexecCmd="/bin/bash -c"
else
	rexecCmd="/usr/local/bin/ssh $destMachine"
fi

# full-featured mailer
export MAIL_PROGRAM=/usr/bin/mailx
export ADMINS_WHO_CARE="database.admin@random.company.org.uk"
# oracle environment vars
# WARNING: Must have symbolic link ORACLE_HOME in $HOME
export ORACLE_HOME=$HOME/ORACLE_HOME
export TNS_ADMIN=$ORACLE_HOME/network/admin
export NLS_DATE_FORMAT='mm/dd/yy hh:mi:ss am'

# run the scriptToRun -- noOutput=good, output=bad.
outputFromScript=`$rexecCmd $scriptToRun`
errorString="(ERRORS IN RUN)"
if [ "xxx$outputFromScript" = "xxx" ] ; then
        errorString="(No problems found)"
fi

# send mail to the admins who care
$MAIL_PROGRAM -s "[RUNOSSCRIPT] $destMachine -- $errorString" $ADMINS_WHO_CARE <<EOLETTER
The RUNOSSCRIPT job on $HOSTNAME just ran. I tried to run:
  dest: $destMachine
  cmnd: $rexecCmd $scriptToRun
----------- outputFollows -----------
$outputFromScript
EOLETTER

