#!/bin/sh
VERSION=260130
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
if [ -e "$SCRIPTDIR/esc.sh" ] ; then . "$SCRIPTDIR/esc.sh"; fi
LOGDIR=$SCRIPTDIR/logs
LOG=$LOGDIR/log-$SCRIPTNAME.log

if [ -z "$1" ] ; then
        echo "show      - show current cronjobs"
        echo "edit      - edit crontab"
        echo "add       - add the backup cronjob"
        echo "restart   - restart cron services/jobs"
	echo "cmd	- show generated cron command"
	echo "check	- check if cron backups are running"
	echo "new 'cmd'	- add a custom cron job (add single-quotes to ensure vars etc)"
fi

if [ "$1" == "show" ] ; then
        cat /var/spool/cron/crontabs/root
        exit
fi

if [ "$1" == "restart" ] ; then
        echo "restarting crontab services..."
        /bin/kill $(cat /var/run/crond.pid)
        echo $?
        /usr/lib/vmware/busybox/bin/busybox crond
        echo $?
        exit
fi

if [ "$1" == "edit" ] ; then
        vi /var/spool/cron/crontabs/root
        exit
fi

if [ "$1" == "add" ] ; then
	echo "adding info to crontab..."
        /bin/echo "# this is the cronjob to run the backups" >> /var/spool/cron/crontabs/root
        /bin/echo "0 22 * * 5 $SCRIPTDIR/ghettoVCB.sh -g $SCRIPTDIR/esc.sh -f $SCRIPTDIR/list.vms -c $SCRIPTDIR/vmconfigs >> $LOG" >> /var/spool/cron/crontabs/root
        /bin/echo "# this is the cronjob to check backups" >> /var/spool/cron/crontabs/root
        /bin/echo "0 7 * * 6 $SCRIPTDIR/cronjob.sh check >> $LOG" >> /var/spool/cron/crontabs/root
        echo "restarting crontab services..."
        /bin/kill $(cat /var/run/crond.pid)
        echo $?
        /usr/lib/vmware/busybox/bin/busybox crond
        echo $?
	echo "TODO adding to autostart jobs..."
        #$SCRIPTDIR/autostart.sh $SCRIPT add
        exit
fi

if [ "$1" == "cmd" ] ; then
	echo "0 0 * * 1-7 $SCRIPTDIR/ghettoVCB.sh -g $SCRIPTDIR/esc.sh -f $SCRIPTDIR/list.vms -c $SCRIPTDIR/vmconfigs  >> $LOG"
	echo "0 7 * * 1-7 $SCRIPTDIR/cronjob.sh check >> $LOG"
fi

if [ "$1" == "check" ] ; then
	#bkpdir=$(grep -m 1 "VM_BACKUP_VOLUME=" $SCRIPTDIR/ghettoVCB.sh | cut -d"=" -f2)
        ts=$(date +"%Y-%m-%d")
        bkps=$(find $bkpdir -mtime 0 -name STATUS.ok)
        count=$(echo "$bkps" | wc -l)
        vms=$($SCRIPTDIR/list.sh -c) # get the count of all to be backupped VMs
        echo "ts: $ts bkpdir: $bkpdir count: $count vms: $vms"
        if [ $count != 0 ] ; then
                # add found backups to the log
                echo "$bkps" >> $LOG
                if [ $count == $vms ] ; then
                        echo "OK, found $count/$vms backups for $ts";
                        wget "https://monitor3.svzone.de/?name=$host&state=0&msg=$count:$vms" --no-check-certificate -O -
                else
                        echo "WARNING, found $count/$vms backups for $ts"
                        wget "https://monitor3.svzone.de/?name=$host&state=2&msg=$count:$vms" --no-check-certificate -O -
                fi
        else
                echo "ERROR, found no backups for $ts" >> $LOG
                wget "https://monitor3.svzone.de/?name=$host&state=1&msg=$count:$vms" --no-check-certificate -O -
        fi
fi

if [ "$1" == "new" ] ; then
	if [ -z "$2" ]; then echo "no command given"; exit 1; fi
        echo "adding command: \"$2\" to crontab..."
        /bin/echo "# custom cronjob: running every 10 minutes, adapt to your needs!" >> /var/spool/cron/crontabs/root
        /bin/echo "*/10  * * 1-7 $2 >> $LOG" >> /var/spool/cron/crontabs/root
        echo "restarting crontab services..."
        /bin/kill $(cat /var/run/crond.pid)
        echo $?
        /usr/lib/vmware/busybox/bin/busybox crond
        echo $?
        exit
fi


exit 0
