#!/bin/sh
# Remote Auto Backup Menu script	Version 1.00
#
# Copyright  RTK Media, Inc 2012
# All Rights Reserved.
# Any unauthorized use, reproduction, transfer or disclosure is prohibited.
#
# name: rabmenu
#
# function: Remote Auto Backup Menu script
#
# Author: Richard P. Nolan  RTK Media, Inc (719) 522-0377 rich@rtkmedia.com
#
# inputs: 
#
# outputs: 
#
# history:                  	     	(spr#) 
# version  date      name		reason/description
# -------  --------  -------------------------------------------------------
# 1.00     09/25/12  rpn Original Version
# ----------------------------------------------------------------------------
#
#

# Variables for rautobak files
RABDIR="/versyss/rab"                 # Where the remote autobackup files are
RABOUT="${RABDIR}/rautobak.out"    # File for details of the last backup
RABLOG="${RABDIR}/rautobak.log"    # File for summary of start and stop of B/Us
TEMPLOG="${RABDIR}/temp.log$$"    # A work file to make rautobak.log smaller
RABSTAT="${RABDIR}/rautobak.stat"  # Summary (no tar stuff) of last backup
RABLOGS="${RABDIR}/rablogs"        # Directory of rautobak.stat history
AGE=30                            # How many days we will keep the logs
RABFTPPATH="${RABDIR}/ftppath"  # Save all none comment lines here for path
RABFTPFILES="${RABDIR}/ftpfiles" # Save all none comment lines for files
RABTAR="/versyss/data/tmp"            # A place to do the tar file MAKE IT BIG!
RABTEMPDIR="${RABDIR}/tmp"         # Directory for Temporary files
TEMPPATH="$RABTEMPDIR/temp.path$$" # Save all none comment lines here for path
TEMPFILES="$RABTEMPDIR/temp.files$$" # Save all none comment lines for files
#
# Programs to Call
#
RABNOTIFY="$RABDIR/RNotify" # Optional program used to notify users of complete
RABINSTALL="${RABDIR}/RABInstall"  # Program to modify crontab for rautobak
RAUTOBAK="${RABDIR}/rautobak"      # The program for running a manual backup


Version="1.00"
TempFile="${RABTEMPDIR}/RABTemp$$"

CronDir="/usr/spool/cron/crontabs"
CronUser="root"
CronFile=${CronDir}/${CronUser}

ESC=27
CAN=24

if [ -x /usr/bin/fgrep ]
then
  GREP="/usr/bin/fgrep"
else
  GREP="/bin/grep"
fi

IsROOT=0 # Assume we are not root
if [ -x $RAUTOBAK ]
then
	IsROOT=1 # This assumes the autobackup is only available to ROOT
fi

FlagOnlyROOT()
{
if [ $IsROOT -ne 1 ]
then
	echo " (ROOT ONLY)\c"
fi
echo "\r"
}

FixPermiss()
{
chmod 666 $1
chown root $1
chgrp system $1
}

Ask()
{
DefaultAnswer=$1
Prompt="$2"

while :
do
  echo "$Prompt\c"
  read Answer
	case $Answer in
	y|y*|Y|Y*|1) Answer=1
	  break;;
	n|n*|N|N*|0) Answer=0
	  break;;
	q*|Q*|2)     Answer=2
	  break;;
	*)         Answer=$DefaultAnswer
	  break;;

	esac
done
}

waitkey()
{
echo "\nPress Enter to continue. \c"
read selection
}

RABStatus()
{
  StatusMessage="is available."     # Default Message
  if [ -f ${RABLOG} ]    # If rautobak.log exists
  then
    set `cat ${RABLOG} | tail -n 1`  # Print out the rautobak.log | get last ln
    if [ $1 = "Start" ]             # If last line has Start in it
    then
      StatusMessage="did not complete."
      if [ `ps -ef | ${GREP} "${RABDIR}/rautobak" | wc -l` -gt 1 ]
      then    # We can see the rautobak process is running
        StatusMessage="is still in progress."
      fi
    fi
  fi
  echo "\nRemote Auto Backup ${StatusMessage}\n"
}

GetSelection()
{
echo "Select => \c"
read selection
}

Clear()
{
  tput clear 2>/dev/null
}

Home()
{
  tput home 2>/dev/null
}

RevVidOn()
{
  tput smso 2>/dev/null
}

RevVidOff()
{
  tput rmso 2>/dev/null
}

CursorAt()
{
  tput cup $1 $2 2>/dev/null
}

SHOWSTATUS()
{
clear
if [ -f ${CronFile} ]       # If we have a CronFile at all (we should)
then # cat the crontab to grep for autoback then grep for 1st char #= "#"
  if [ ! -d ${RABTEMPDIR} ]   # If we don't have the tmp directory
	then
    mkdir ${RABTEMPDIR} >/dev/null 2>&1
    FixPermiss ${RABTEMPDIR} >/dev/null 2>&1
  fi

# cat crontab | look for rautobak | ensure it isn't commented out
  cat ${CronFile} | grep $RABDIR/rautobak | grep -v "^#" > $TempFile 

  if [ -s $TempFile ]   # If we have a crontab entry written to tempfile
  then
		echo "Current Date/Time is `date`"
    Count=`cat $TempFile | wc -l`   # See how many entries we have
    RABStatus             # Show the status of the Remote Auto Backup
    cat $TempFile | 
	  while read Minute Hour Date Month Days dummy
    do
	    Count=`expr $Count + 1`
	    Length=`expr "$Hour" : ".*"`
      [ $Length -eq 1 ] && Hour="0$Hour" # Set 0-9 to 00-09
	    Length=`expr "$Minute" : ".*"`
      [ $Length -eq 1 ] && Minute="0$Minute" # Set 0-9 to 00-09

	    echo "Auto Backup is set for $Hour:$Minute\c"
	    case "$Days" in
	      "*")	echo " Every Day." ;;
	      "1-5")	echo " Monday - Friday." ;;
	      "2-6")	echo " Tuesday - Saturday." ;;
	      "0,6") echo " Saturday & Sunday." ;;
	      "0,1") echo " Sunday & Monday." ;;
	      "0") echo " Sunday Only." ;;
	      "1") echo " Monday Only." ;;
	      "2") echo " Tuesday Only." ;;
	      "3") echo " Wednesday Only." ;;
	      "4") echo " Thursday Only." ;;
	      "5") echo " Friday Only." ;;
	      "6") echo " Saturday Only." ;;
	      *)	echo "."
	    esac
    done
    rm ${TempFile}
  else  # We didn't find any lines that weren't commented out with rautoback
    echo "\n\nCron is not set to do an Auto Backup."
  fi
else
  echo "\n\nYou must be root to view when remote autobackups happen\n\n"
fi
waitkey
}

while :
do
  Clear
	Home
  Date=`date +%D`
  echo "$Date    Remote Auto Backup Menu - $Version\n"
	echo "1. Display Remote Auto Backup Status\c"
	FlagOnlyROOT

	echo "2. Set Backup Time(s)\c"
	FlagOnlyROOT

	echo "3. View Last Complete Backup"   # Anyone can run these
	echo "4. View Backup Log"             # In case a helper checks each morning

	echo "5. Edit ftppath\c"
	FlagOnlyROOT

	echo "6. Edit ftpfiles\c"
	FlagOnlyROOT

	echo "7. Edit RNotify \c"
	FlagOnlyROOT

	echo "8. Run a manual backup now\c"
	FlagOnlyROOT


  echo "\n99. Exit\n"

  GetSelection
  case $selection in

1)    # Show the status of the remote autobackup
if [ $IsROOT = 0 ]
then
	echo "\n\nFunction Only Available for user 'root'"
	waitkey
	continue
fi
SHOWSTATUS
;;     # End Option 1


2)    # Edit crontab for only the rautobak entries
if [ $IsROOT = 0 ]
then
	echo "\n\nFunction Only Available for user 'root'"
	waitkey
	continue
fi
$RABINSTALL
;;     # End Options 2

3)    # View information about the last backup
echo
Ask 1 "Summary output? [q/Y/n] " # default is YES

if [ $Answer -eq 1 ]    # if they want summary output
then
	if [ ! -f ${RABSTAT} ]   # If we don't have a rautobak.stat in ${RABDIR}
	then
    echo "\nNo summary file available. \c"
  else
		clear
		echo "*** Summary of last Backup ***\n"
		cat ${RABSTAT} | pg
	fi
	waitkey
fi

if [ $Answer -eq 0 ]     # if they want detail output
then
  if [ ! -f $RABOUT ]      # if we have a detail backup log (rautobak.out)
  then
	  echo "\n\nNo detail log file available\n\n"
  else
		clear
		echo "*** Detail of last Backup ***\n"
	  cat ${RABOUT} | pg
  fi
	waitkey
fi
;;   # End Option 3

4)    # Show the Start/Stop times of the last so many backups
if [ ! -f ${RABLOG} ]    # if we have the file rautobak.log
then
	echo "\n\nNo start/stop log file available\n\n"
else
	clear
	echo "*** Backup Log ***\n"
	cat ${RABLOG}
	echo "\n *** End Log ***  \c"
fi
waitkey
;;      # End Option 4

5)    # Edit the Paths and Target Directories
if [ $IsROOT = 0 ]
then
	echo "\n\nFunction Only Available for user 'root'"
	waitkey
	continue
fi
vi ${RABFTPPATH}
;;      # End Option 5

6)    # Edit the tar filenames and files to tar to it
if [ $IsROOT = 0 ]
then
	echo "\n\nFunction Only Available for user 'root'"
	waitkey
	continue
fi
vi ${RABFTPFILES}
;;      # End Option 6

7)    # Edit the RNotify file
if [ $IsROOT = 0 ]
then
	echo "\n\nFunction Only Available for user 'root'"
	waitkey
	continue
fi
vi ${RABNOTIFY}
;;      # End Option 7

8)    # Run a backup right now
if [ $IsROOT = 0 ]
then
	echo "\n\nFunction Only Available for user 'root'"
	waitkey
	continue
fi
$RAUTOBAK
;;      # End Option 8


99)   # They want to exit the menu
  exit 0 
;;      # End Option 99

*)    # For any other selection
  echo "Invalid Entry"
	waitkey
;;      # End Option *)

esac

done
