#!/bin/bash
# 2006-07-13 Created script (ARM)
# 2011-04-07 reworked for use by SSSG          lstolp
#            thanks Andy!
#            just using the reverse ssh tunnel for now
#
# 2011-04-13 reworked for use by R2R netbooks   lstolp
#            just the reverse tunnel for now...
#
# 2011-05-17 reworked for LatMix cruise using
#            endeavor, oceanus and cape hatteras  lstolp
#
#
# 2011-05-17 reworked for LatMix cruise using
#            endeavor, oceanus and cape hatteras  lstolp
#
# 2012-03-15 figured out quoting to get the rsync to work
#            correctly at install, I think..
#            need to uncomment tunnel lines and enter correct
#            port.                                lstolp
#
#
# netbook sshtunnel ports numbers:
#
#    r2rsrv1  44122
#    r2rsrv2  44222
#    r2rsrv3  44322
#    r2rsrv4  44422
#


DATESTAMP=`date +"%Y/%m/%d %H:%M:%S"`

cruise=CRUISEID
shore=128.128.76.118
command=r2r_"$cruise"_command.sh
rsyncit="$cruise"_rsyncit.log

# rsync command
RSCOMMAND="/bin/bash /shipdata/$cruise/r2r/eventlog/rsync/$command >> /shipdata/$cruise/r2r/eventlog/logfiles/$rsyncit 2>&1"

# uncomment for tunnel
# change XXXXX to port number for netbook still working on this lstolp
# TUCOMMAND="ssh -nNT -C -p 22 -R XXXXX:localhost:22 $shore"


CHECK_RSYNC=`ps -eo args | grep "$RSCOMMAND" | grep -v grep`
# uncomment for tunnel
# CHECK_TUNNEL=`ps -eo args | grep "$TUCOMMAND" | grep -v grep`

# If Rsync command not running run it
if [ -z "$CHECK_RSYNC" ] ; then
   echo $TODAY "$DATESTAMP $HOSTNAME Rsync Down: Running Rsync"
   $RSCOMMAND &
else
echo $TODAY "$DATESTAMP $HOSTNAME Rsync Up."
fi

# If TUNNEL command not running run it
#if [ -z "$CHECK_TUNNEL" ] ; then
#   echo $TODAY "$DATESTAMP $HOSTNAME Tunnel Down: Creating Tunnel"
#   $TUCOMMAND &
#else
#echo $TODAY "$DATESTAMP $HOSTNAME Tunnel Up."
#fi
#
