#!/bin/bash
#
# script to synchronize the MPIWG fileserver
#
# V0.0.3l (7.4.2015 ROC)

DEBUG=1

LOCKFILE=/tmp/mpiwg-all-sync.lock
LOGFILE=/var/tmp/mpiwg-all-sync.log
#EXCLUDEFILE=/usr/local/sbin/rsync-exclude.list

MAILTO=casties@mpiwg-berlin.mpg.de,jsteinhoff@mpiwg-berlin.mpg.de

MIRDIR=
REMOTEDIR=

RSYNC=/usr/bin/rsync
RSYNCHOST="foxridge2"
#RSYNCOPTS="-e rsh --delete --exclude-from=$EXCLUDEFILE"

function syncdir () {
#
# sync given directory
#
    DIR=$1
    REMDIR=${2:-$REMOTEDIR}
    if [ -n "$3" ] && [ ${3:1:1} == "-" ]
    then
	# if the third param starts with "-" it's the options
	MOREOPTS=$3
	shift
    fi
    LOCDIR=${3:-$MIRDIR}
    echo "***** syncing $DIR on $(date)" >> $LOGFILE
    if [ -n $DEBUG ] ; then echo "syncing $DIR on $(date)"; fi
    if [ -n $DEBUG ] ; then echo "    $RSYNC ${RSYNCOPTS[@]} $MOREOPTS \"$RSYNCHOST:$REMDIR/$DIR/\" \"$LOCDIR/$DIR/\" >> $LOGFILE 2>&1" >> $LOGFILE ; fi
    $RSYNC "${RSYNCOPTS[@]}" $MOREOPTS "$RSYNCHOST:$REMDIR/$DIR/" "$LOCDIR/$DIR/" >> $LOGFILE 2>&1 
    if [ $? != 0 ]
    then
	ERROR="${ERROR}ERROR syncing $DIR "
    fi
}



#
# check if script is already running
#
if [ -f $LOCKFILE ]
then
    if [ -n $DEBUG ] ; then echo "lockfile present! script seems to be running!"; fi

    mail -t $MAILTO -s "$( hostname ): MPIWG-ALL-MIRROR: already running!" <<EOF

`ls -l $LOCKFILE `
EOF
    exit 1
fi

touch $LOCKFILE

echo "***** started `date`" > $LOGFILE

#
# sync
#

RSYNCOPTS=(-avAX --delete -e 'ssh -i /root/.ssh/foxridge1-sync-mpiwg')
syncdir "mpiwg"

# /usr/local/mpiwg as well
RSYNCOPTS=(-avAX --delete -e 'ssh -i /root/.ssh/foxridge1-sync-usrlocal')
syncdir "mpiwg" "/usr/local" "/usr/local"

#
# send mail with error log if an error occurred
#
if [ -n "$ERROR" ]
then
    # don't send logs bigger than 1MB
    LOGSIZE=`ls -l $LOGFILE |cut -d' ' -f 5`
    if [ "$LOGSIZE" -gt 900000 ]
    then
	LOG="LOGFILE too big to send: $LOGSIZE"
    else
	LOG=`cat $LOGFILE`
    fi

    mail -t $MAILTO -s "foxridge1 mpiwg-all-sync: $ERROR" <<EOF

$LOG
EOF
fi

echo "***** done `date`" >> $LOGFILE

# remove lock
rm $LOCKFILE
