#!/bin/bash  

version="1.1.0"
###########################TO DO#####################################
#		Accept parameters
#		Make Diff and CHK optional
#		save error ouput on copy,kill
#####################################################################

#usage funtion: prints prog name and option(s)
usage() {

	echo "Usage: `basename $0` -i include-conf-file [-qvl loop-seconds -w loop-seconds] [-V]"
	echo "-i include-conf file (first parameter, full path)"
	echo "-q quiet mode"
	echo "-v verbose mode"
	echo "-f force update even if destination is newer"
	echo "-s force update even if destination is same"
	echo "-l loop-time-in-seconds (0 for no loop)"
	echo "-u Only use the URL to retreive the file"
	echo "-w wait-on-bad loop-time-in-seconds (0 for no loop)"
	echo "-V Report version number and exit"
	echo " EXAMPLE:"
	echo " `basename $0` -i /etc/myupdate -vfl 120"
	exit 60;
}
 
version () {

echo "`basename $0` version: $version"
exit 0
}


exitold="`printf %d $exitold`"
[[ -n "$exitold" ]] && (( exitold )) || exitold=10

exitsame="`printf %d $exitsame`"
[[ -n "$exitsame" ]] && (( exitsame )) || exitsame=11

#getopts to parse cmd-line params and perform actions

while getopts i:fsl:vVuqw:  opt; do
 	if [ $opt = "i" ]; then	HAVEINCLUDE="yes"; fi
	case "$opt" in  
	i) . $OPTARG;; #read include file that contains variables
	f) forceold="Yes";;
	s) forcesame="Yes";;
	l) PROGLOOP=$OPTARG;;
	q) quiet="-q";;
	v) quiet= ;;
	V) version ;;
	w) waitonbad=$OPTARG;;
	u) URLONLY="yes";;
	*) usage;;
	esac;
done

if [ $HAVEINCLUDE != "yes" ] ; then
	echo "MUST have an include file."
	usage; 

fi


if [ $# -lt "2" ]; then
	usage;
fi

checkperms() {

#check permisions on file destinations
if [[ "$1" == "" ]]; then return 1; fi

if [[ -a $1 ]] && [[ -w $1 ]]; then
	return
fi
if [[ -d `dirname $1` ]] && [[ -w `dirname $1` ]]; then
	return
fi
	return 1
}


checkperms $TMP$FILE || ( echo "No permission to write $TMP$FILE" ; exit 62)
checkperms $CONF || ( echo "No permission to write $CONF" ; exit 63)
	

#functionize the update's retreival


function url_location ()
{
	if [[ "$URLONLY" == "yes" ]]; then

		echo "$URL -O $TMP$FILE"
	else
		echo $URL$FILE
	fi
}

function updatefile () {

if which wget >/dev/null 2>/dev/null; then

	if [[ "$USER" != "" ]]; then
		wget   --no-check-certificate -N $quiet  --http-user=$USER --http-password=$PASS -P $TMP `url_location` ;
	else
		wget -N $quiet  -P $TMP `url_location` ;
	fi
	if [[ "$?" != "0" ]]; then
		echo "wget failed"
		exit 64;
	fi


else

	echo "Cannot find required program: wget"
	exit 61;

fi;
}

#functionize check syntax routine

function checkupdate () {

if [ $CHK ]; then

	$CHK $CHKPRM ;
	return $?

else

	return 0;

fi ;

}

function messageout() {

if ! [ $quiet ]; then
	echo $* ;
fi

}

#functionize diff routine

function diffupdate () {

if  [ $DIFF  ]; then

DIFFOUTPUT=`$DIFF -uN $DIFFOPTS $TMP$FILE $CONF`;
if [ $? -eq "1" ]; then return 0 ; else return 1; fi
else

return 0;

fi;
}

#functionize actual hard work

function runupdate () {
#start the ball rolling!

checkperms
updatefile

if [ $CONF -ot $TMP$FILE ] || [[ $forceold == "Yes" ]];			#is the conf file newer than the update?


then 

	diffupdate 
	if [[ "$?" != "0" ]] && [[ "$forcesame" != "yes" ]]; then
		[[ "$notify_good" ]] && echo -e "New file is the same. Not updating" |\
		 mail -s "Good Stuff BOSS on $FILE" $notify_good #notify
		if (( exitsame > 0 )); then
			exit `printf %d "$exitsame"`
		fi
		return 101
	fi
		

	OUTPUT=`checkupdate`				#check syntax validity
	retval=$?				#save return value


	if [ $retval -gt "0" ]; then	#OhOh

		echo "FAILURE!"
		[[ "$notify_bad" ]] && echo "$OUTPUT" | mail -s "Bad Stuff BOSS on $FILE" $notify_bad #notify
	
		if [ $waitonbad -gt "0" ]; then
			until [ $retval -eq "0" ]; do	#Loop to get a new good one				
				sleep $waitonbad 
				updatefile
				checkupdate
				retval=$? ;
					
			done;
			
		else

			echo "Bad Update found: Aborting"
			exit 2;

		fi;
	

	fi
	
	
	#copy the update to destination
	cpout=`cp $TMP$FILE $CONF`		
	retval=$?

	if [ $retval -gt "0" ]; then	#OhOh

		[[ "$notify_bad" ]] && echo $cpout | mail -s "Bad Stuff BOSS on copying $CONF" $notify_bad
		exit 3; 

	fi

	
	if [ "$KILLCMD" ] ; then
		$KILLCMD
		retval=$? ;
	else
		if [ $PIDFILE ]; then
			pid=`cat $PIDFILE`		#grab the pid
			killout=`kill -SIGHUP $pid`	#send the reload signal
			retval=$?;
		fi
	fi

	if [ $retval -gt "0" ] ; then	#We are screwed!

		[[ "$notify_bad" ]] && echo $killout | mail -s "Bad Stuff BOSS on HUP $pid from $PIDFILE" $notify_bad
		exit 4; 
		
	fi
	messageout "$PROG confile $CONF was updated from `url_location`"
	[[ "$notify_good" ]] &&\
	echo -e "$PROG confile $CONF was updated from `url_location`\n${DIFFOUTPUT}\n${OUTPUT}"  |\
	mail -s "Good Stuff BOSS on $PROG" $notify_good;

else

	

	messageout  "$CONF is newer than `url_location`, not updating";
	if [[ "$exitold" != "" ]]; then
		exit `printf %d "$exitold"`
	fi
	


fi;

}

if [ $PROGLOOP -gt "0" ]; then

progloops="1"

while : ; do	
runupdate
updateret=$?
messageout  "Loop Number: $progloops"
progloops=$(( $progloops + 1 ))
sleep $PROGLOOP ;
done;
else

messageout 'Running Update!'

runupdate ;
updateret=$?

fi

if [[ "$updateret" == "101" ]] && [[ "$forcesame" != "yes" ]]; then exit 1; fi

#Done!

exit 0

