:
#
# aodcron (Online Diagnostics)
#

# define a function to read a line of input and screen for improper values
getvals() \
{ \
	echo; \
	echo "Enter values $1 : \c"; \
	read GETVALS ; \
	GETVALS=`echo "$GETVALS" | sed "s/[^0-9]/,/g" | \
	awk 'BEGIN{FS=","}{for(i=1;i<=NF;i++){printf("%s ",\$i"");}}'` ; \

	echo $GETVALS ; \
	GOODVALS="" ; \
	for i in $GETVALS
	do
		if [ $i -ge $2 -a $i -le $3 ]
		then
			# valid entry; add to good list
			GOODVALS="$GOODVALS $i"
		else
			# invalid entry; notify user & discard
			echo "Invalid value $i entered; discarded" ; \
		fi \
	done \
}

# define a function to read input and screen for improper values
getval() \
{ \
	echo; \
	echo "Enter value $1 : \c"; \
	read GETVAL ; \
	GETVAL=`echo "$GETVAL" | sed "s/[^0-9]//g"`
	if [ $GETVAL -lt $2 -o $GETVAL -gt $3 ]
	then
		# invalid entry; notify user, pause, & discard
		echo "Invalid value entered; input rejected" ; \
		echo "Hit <Enter> to continue \c" ; \
		read GETVAL ; \
		GETVAL=-1 ; \
	fi \
}

# define a function to insert/edit/remove values from a string of values
# seperated by commas
editline() \
{ \
field="$1"; \
nuline="$3"; \
prevline="$3"; \
minval="$4"; \
maxval="$5"; \
until [ ]
do \
	clear; \
	echo "		       	    Oldmon cron table entry editor"; \
        echo "                       ------------------------------"
	echo; \
	echo "The previous values for $field are:"; \
	echo "	$prevline"; \
	echo; \
	echo "The values for $field currently are:"; \
	echo "	$nuline"; \
	echo; \
	echo "Choose functions:"; \
	echo "	a|A) add values"; \
	echo "	r|R) remove values"; \
	echo "	x|X) exit with changes"; \
	echo "	q|Q) quit without saving changes"; \
	echo; \
	echo "	Your choice? \c"; \
	read CHOICE; \
	set echo ; \
	set verbose ; \
	case $CHOICE in 
	"a"|"A") getvals "to be added" $minval $maxval; \
		if [ "$GOODVALS" ]
		then \
			if [ "$nuline" = "*" ]
			then \
				nuline=""; \
			fi
			for GETVAL in $GOODVALS
			do
				AWKF=`cat << ?
				BEGIN{FS=","} 
				{
					put=0; 
					for(i=1;i<=NF;i++){ 
						if(put) { 
							if(\\$i+0 != $GETVAL+0) { 
								printf(",%s",\\$i); 
							} 
						} else if(\\$i+0>$GETVAL+0) { 
							if(i!=1) { 
								printf(","); 
							} 
							printf("%d,%s",$GETVAL,\\$i); 
							put=1; 
						} else if(\\$i+0<=$GETVAL+0) { 
							if(i!=1) { 
								printf(","); 
							} 
							printf("%s",\\$i); 
							if(\\$i+0==$GETVAL+0) { 
								put=1; 
							} 
						} 
					} 
					if(!put){
						if(NF>0) {
							printf(",");
						}
						printf("%d",$GETVAL);
					}
				}
?`
				nuline=`echo $nuline | awk "$AWKF"`; \
				done; \
		fi  \
		;; \
	"r"|"R") getvals "to be removed" $minval $maxval ; \
		for GETVAL in $GOODVALS
		do
		if [ $GETVAL -ne -1 ]
		then \
			if [ "$nuline" != "*" ]
			then \
				cat > $$.awk << ?
				BEGIN{FS=","} 
				{
					first=1;
					for(i=1;i<=NF;i++){ 
						if(\$i+0!=$GETVAL+0) { 
							if(!first) { 
								printf(","); 
							} 
							printf("%s",\$i); 
							first=0; 
						} 
					} 
				}
?
				nuline=`echo $nuline | awk -f $$.awk`; \
				rm -f $$.awk; \
				if [ ! "$nuline" ]
				then \
					nuline="*"; \
				fi; \
			fi; \
		fi; \
		done; \
		;; \
	"x"|"X") EDITLINE=$nuline;break ;; \
	"q"|"Q") EDITLINE=$prevline;break ;; \
esac ; \
unset echo ; \
unset verbose ; \
done \
}

# define a function to remove temporary files on signals
cleanup() \
{ \
	echo; \
	if [ -f $$.cron* ]
	then
		echo "removing temporary files $$.cron\*"; \
		rm -f $$.cron*; \
	else
		echo "no temporary files to remove"; \
	fi \
}

# check for superuser id

if [ `logname` != "root" ]
then
	echo "Must be super-user to execute the 'aodcron' routine"
	exit 1
fi

# in case of premature exit, remove temporary files
trap "cleanup;exit 1" 0 2 3 15

# let user know there will be a short delay
echo;echo "Extracting cron table information; please be patient...\c"

# extract crontab entry and remainder of crontab table into separate files
crontab -l > $$.cronlst
grep "/usr/lib/aodmon" $$.cronlst > $$.cronetr
grep -v "aodmon" $$.cronlst > $$.cronrem
oldcmd="/usr/lib/aodmon 2>&1 > /dev/null"

# extract crontab entry and remainder of crontab table into separate string vars
cronline=`cat $$.cronetr | awk '{if (substr($1,1,1) ""!="#")printf("%s", $0);}'`
cronpref=""
cronrems=`grep "aodmon" $$.cronlst | grep -v "aodmon cron editor" | awk '{if (substr($1,1,1) ""=="#")printf("%s", $0);}'`
if [ ! "$cronline" ]
then
	cronline=`cat $$.cronetr | awk '{for(i=1;i""<length($0);i++){if(substr(\$0,i,1)~/[0-9*]/){print substr(\$0,i,length($0)-(i-1));break;}}}' | awk '{if($6=="/usr/lib/aodmon") print $0}'`
	if [ "$cronline" ]
	then
		cronrems=`echo "$cronrems" | grep -v "/usr/lib/aodmon"`
		cronpref="#"
	fi
fi
if [ `echo "$cronline" | grep -c "*"` -gt 1 ]
then
	# more than one possible entry; void them all & start from scratch
	cronline=""
	cronrems=""
	cronpref=""
fi

mins=`echo "$cronline" | awk '{print $1}'`
hrs=` echo "$cronline" | awk '{print $2}'`
mday=`echo "$cronline" | awk '{print $3}'`
mnth=`echo "$cronline" | awk '{print $4}'`
wday=`echo "$cronline" | awk '{print $5}'`
cmnd=`echo "$cronline" | awk '{for(i=6;i<=NF;i++)printf("%s ",\$i);}'`

# let user know that we're done getting the info now
echo "done"

# loop through crontab entry editor
until [ ]
do
	clear
	echo "			Online Diagnostics cron table entry editor"
	echo
	echo "crontab aodmon line:";echo "$cronpref $cronline";echo
	echo "	1) minutes [0-59]:		$mins"
	echo "	2) hours [0-23]:		$hrs"
	echo "	3) day of month [1-31]:		$mday"
	echo "	4) month [1-12]:		$mnth"
	echo "	5) day of week [0-6] (0=Sun):	$wday"
	echo
	echo "	I) insert default Online Monitor cron table entry"
	echo "	R) remove Online Monitor entry from cron table"
	echo "	D) disable cron table entry (prepend a '#' to it)"
	echo "	E) enable cron table entry (remove any preceding '#'s from it)"
	echo "	X) exit, saving the changes made during this editing session"
	echo "	Q) quit, restoring cron entry to its prior state"
	echo;echo "Consult your manual under cron(C) and crontab(C) for help";
	if [ ! "$mins" -a ! "$hrs" -a ! "$mday" -a ! "$mnth" -a ! "$wday" ]
	then
		echo
		echo "No current entry for Online Monitor routine in root cron table;"
		echo "Online Monitor routine will not be scheduled by the system."
	elif [ "$cronpref" ]
	then
		echo
		echo "Current entry for Online Monitor routine in root cron table is disabled;"
		echo "Online Monitor routine will not be scheduled by the system."
	elif [ "$mins" = "*" -a "$hrs" = "*" -a "$mday" = "*" -a "$mnth" = "*" -a "$wday" = "*" ]
	then
		echo "WARNING! setting all fields to '*' will cause the"
		echo "	Online Monitor utility to be invoked once every minute! This"
		echo "	could cause degradation of system performance"
	else
		echo;echo;echo
	fi
	echo;echo "Select field number to modify, or alpha character for course of action...\c"
	read SELECTION
	if [ ! "$mins" -a ! "$hrs" -a ! "$mday" -a ! "$mnth" -a ! "$wday" ]
	then
		mins="*"		
		hrs="*"		
		mday="*"		
		mnth="*"		
		wday="*"		
	fi


	case $SELECTION in
		"1") editline "minutes" "mins" "$mins" 0 59
			mins=$EDITLINE
			cronline="$mins $hrs $mday $mnth $wday $oldcmd"
			 ;;
		"2") editline "hours" "hrs" "$hrs" 0 23
			hrs=$EDITLINE
			cronline="$mins $hrs $mday $mnth $wday $oldcmd"
			 ;;
		"3") editline "day of the month" "mday" "$mday" 1 31
			mday=$EDITLINE
			cronline="$mins $hrs $mday $mnth $wday $oldcmd"
			 ;;
		"4") editline "month" "mnth" "$mnth" 1 12
			mnth=$EDITLINE
			cronline="$mins $hrs $mday $mnth $wday $oldcmd"
			 ;;
		"5") editline "day of the week" "wday" "$wday" 0 6
			wday=$EDITLINE
			cronline="$mins $hrs $mday $mnth $wday $oldcmd"
			 ;;
		"i"|"I" ) mins="59"
			hrs="23"
			mday="*"
			mnth="*"
			wday="*"
			cronline="59 23 * * * $oldcmd"
			 ;;
		"r"|"R" ) cronline="" ;;
		"d"|"D" ) # disable cron entry; prepend it with a "#"
			cronpref="#"
			;;
		"e"|"E" ) # enable cron entry; remove any prepended "#"
			cronpref=""
			;;
		"x"|"X" ) # exit after saving changes
			# make changes to cron table
			if [ "$cronline" ]
			then
				# add pre-existing remarks line(s), if any, to remainder file
				if [ "$cronrems" ]
				then
					echo "$cronrems" >> $$.cronrem
				fi
				echo "# aodmon cron editor entry `date`" >> $$.cronrem
				# add cron table entry
				echo "$cronpref $cronline" >> $$.cronrem
			fi
			# replace pre-existing cron table with this one
			echo;echo "new cron table :"
			echo;cat $$.cronrem;echo
			crontab $$.cronrem
			echo "cron table updated";echo
			break
			;;
		"q"|"Q" ) break ;;
	esac
done

# prepare to exit; clean-up temp files
rm -f $$.cron*
trap 0 2 3 15
