#!/bin/sh
# Bacula Updater by Juan Luis Francés
# Veloxia Network, S.L.
# November 2004
# This script downloads .gz, compile and upgrade Bacula on Red Hat 9
#
# You can view upgrade log at /tmp/bupdater.log
#
# Prerequisites:
# wget
# tar
# gzip
#
# Params:
# bupdater.sh [URL]
#
# - [URL]	This is the valid URL where to download bacula.tgz. Ex. http://192.168.0.2/bacula-1.34.tar.gz
#
# Version: 1.3RH9

VERSION="1.3RH9"
TMP=/tmp
LOG=$TMP/bupdater.log
FILE=$(echo $1 | sed -r s/.+[\/]//)
DIR=$(echo $1 | sed -r s/.+[\/]// | sed s/.tar.gz//)
PID=$(pidof -s bacula-fd)



deltemp() {
	echo "`date`: Deleting temp files...." >> $LOG
	rm -f $TMP/$FILE >> $LOG 2>>$LOG
	
	if [ $? != 0 ]; then
		echo "`date`: ERROR. Fail removing $TMP/$FILE" >> $LOG
		echo "Exiting." >> $LOG
		exit 1
	else
		echo "`date`: Delete file $TMP/$FILE successful." >> $LOG
	fi

	rm -rf $TMP/$DIR >> $LOG 2>>$LOG

	if [ $? != 0 ]; then
		echo "`date`: ERROR. Fail removing dir $TMP/$DIR" >> $LOG
		echo "Exiting." >> $LOG
		exit 1
	else
		echo "`date`: Delete file $TMP/$DIR successful." >> $LOG
	fi

}

startfd() {
	echo "`date`: Starting bacula-fd again..." >> $LOG
	bacula-fd -c /etc/bacula/bacula-fd.conf >/dev/null 2>>$LOG
	if [ $? != 0 ];then
		echo "`date`: ERROR. Failed to start daemon.">> $LOG
		echo "Exiting." >> $LOG
		exit 1
	else
		echo "`date`: Successfully started." >> $LOG
	fi
}


if [ $# != 1 ]; then
	echo "Usage: $0 URL"
	exit 1	
fi


if [ ! -d $TMP ]; then
	echo "ERROR: Temporal dir not found. We need it!"
	exit 1
fi

if [ ! -f $TMP/bupdater.log ]; then
	touch $LOG
else
	echo "" >> $LOG
	echo "" >> $LOG
fi

echo "* Bupdater $VERSION starting on `date`" >> $LOG
echo "`date`: Downloading source..." >> $LOG
cd $TMP
wget $1  2>/dev/null >>$LOG

if [ $? != 0 ]; then
	echo "`date`: ERROR. Wget failed to download file!" >> $LOG
	echo "Exiting." >> $LOG
	exit 1
else
	echo "`date`: Download successful." >> $LOG
fi

# Check process
if [ ! -z $PID ]; then
	echo "`date`: bacula-fd is loaded. Killing process..." >> $LOG
	kill $PID
	if [ $? != 0 ];then
		echo "`date`: ERROR. Failed to kill bacula-fd process!" >> $LOG
		echo "Exiting."
		exit 1
	else
		echo "`date`: Successfully killed." >> $LOG
	fi
fi



echo "`date`: Decompressing file $TMP/$FILE ..." >> $LOG
tar zxvfp $FILE >>/dev/null 2>> $LOG

if [ $? != 0 ]; then
	echo "`date`: ERROR. Failed decompressing file!" >> $LOG
	startfd
	echo "Exiting." >> $LOG
	exit 1
else
	echo "`date`: Decompress successful." >> $LOG
fi


echo "`date`: Running configure..." >> $LOG
cd $TMP/$DIR
./configure --enable-client-only --bindir=/usr/local/bin --sbindir=/usr/local/sbin >/dev/null 2>> $LOG
if [ $? != 0 ]; then
	echo "`date`: ERROR. Configure failed." >> $LOG
	startfd
	deltemp
	echo "Exiting." >> $LOG
	exit 1
else
	echo "`date`: Configure successful." >> $LOG
fi

echo "`date`: Running make..." >> $LOG
make >/dev/null 2>>$LOG
if [ $? != 0 ]; then
	echo "`date`: ERROR. make failed." >> $LOG
	deltemp
	startfd
	echo "Exiting." >> $LOG
	exit 1
else
	echo "`date`: Program made." >> $LOG
fi


echo "`date`: Running make install..." >> $LOG
make install >/dev/null 2>>$LOG
if [ $? != 0 ]; then
	echo "`date`: ERROR. make install failed." >> $LOG
	deltemp
	startfd
	echo "Exiting." >> $LOG
	exit 1
else
	echo "`date`: Program installed." >> $LOG
fi

echo "`date`: Running make install autoscripts..." >> $LOG
make install-autostart-fd >/dev/null 2>>$LOG
if [ $? != 0 ]; then
	echo "`date`: ERROR. make install-autostart-fd failed." >> $LOG
	deltemp
	startfd
	echo "Exiting." >> $LOG
	exit 1
else
	echo "`date`: Autoscripts installed." >> $LOG
fi


deltemp
startfd
	
echo "`date`: Successfully upgraded BACULA with file $FILE" >> $LOG
