#!/bin/bash
####
# Created by TheProz.com in assosciation with Neoverve
####

### Configurable VARS ###
REQUIREDVER=3.5.20-10			# The version of webppliance required for the updates
ENSIMRPMLOC=ftp://ftp.ensim.com/download/webppliance/linux/Pro/3.5.20/errata/errata-fileutils/lwp-errata-3.5.20-9-2.tar.gz
RESTARTSERVICES="webppliance"  #whitespace seperated list of services to restart"
MAINTMODE=1					# 0 for off 1 for on
APPLVER=`/usr/bin/getapplversion`	# The version of webppliance

clear
echo "created by:"
echo "TheProz.com with help from Neoverve"
echo "All Hail Neoverve!!!"
echo "===================================="
echo


# check for root
if [ ! $UID = "0" ]; then
        echo -e "You must be root to run this script.\nExiting"
        exit 1
fi

# check for required version
if [ ! $APPLVER = $REQUIREDVER ]; then
        echo "you must be running $REQUIREDVER to install this eratta."
        echo "you are currently running $APPLVER"
        echo "Exiting..."
        exit 1
fi


echo "WARNING: We take no responsibility for this update"
echo "We are not affiliated with Ensim and will not be "
echo "responsible for anything that happens to your system."
echo
echo "This update has been tested but it has not been tested"
echo "on your machine so by using this, you take responsibility"
echo "for anything that happens."
echo
echo "press enter to continue"
read stall

### create .setup.sh script ###
touch .setup.sh
echo "mkdir EnsimErrataRPMs" >> .setup.sh
echo "cd EnsimErrataRPMs" >> .setup.sh
echo "/usr/bin/wget $ENSIMRPMLOC" >> .setup.sh
echo "/bin/tar -zxvf lwp-errata-3.5.20-9-2.tar.gz" >> .setup.sh
echo "/bin/rpm -Fvh *.rpm" >> .setup.sh
for i in $RESTARTSERVICES; do
	echo "/etc/rc.d/init.d/$i restart" >> .setup.sh
	echo "echo \"Restarting Service: $i\"" >> .setup.sh
	echo "sleep 5" >> .setup.sh
done
echo "echo \"Upgrade Complete\"" >> .setup.sh
echo "rm -f \$0" >> .setup.sh


if [ $MAINTMODE = 1 ]; then
	schedule="y"
	echo "This update will cause all off your sites to go into maintenance mode, would you like to schedule this for off hours?"
	echo -n "[$schedule]: "
	read temp
	if [ ! -z "$temp" ]; then
		schedule="$temp"
	fi
fi

if [ $schedule = "y" ]; then
        time="22:00"
        echo "When would you like to schedule this? (hh:mm)"
        echo -n "[$time] :"
        read temp
        if [ ! -z "$temp" ]; then
		time="$temp"
        fi
        echo "sh `pwd`/.setup.sh" | at $time
else
        sh .setup.sh
fi




