#!/bin/sh 

amanda_user=amandabackup
amanda_group=disk

TMPFILE=`mktemp /tmp/deb-amanda.XXXXXXXXXXX`
if [ $? -ne 0 ]; then
	echo "Unable to mktemp!" 1>&2
	exit 1
fi
LOGDIR="/var/log/amanda"
INSTALL_LOG="${LOGDIR}/install.log"
INSTALL_ERR="${LOGDIR}/install.err"

echo "`date +'%b %e %Y %T'`: Preparing to install Amanda" >${TMPFILE}

# Check for the '${amanda_user}' user
echo "`date +'%b %e %Y %T'`: Checking for ${amanda_user} user..." >>${TMPFILE}
if [ "`id -u ${amanda_user} >/dev/null 2>&1 && echo 0 || echo 1`" != "0" ] ; then
	useradd -c "Amanda" -g ${amanda_group} -d /var/lib/amanda -s /bin/bash ${amanda_user}
	# Lock the ${amanda_user} account until admin sets password
	passwd -l ${amanda_user} >>/dev/null
	PASSWD_EXIT=$?
	if [ ${PASSWD_EXIT} -eq 0 ] ; then
		echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  The ${amanda_user} user account has been successfully created." >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  Furthermore, the account has been automatically locked for you" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  for security purposes.  Once a password for the  '${amanda_user}'" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  account has been set, the user can be unlocked by issuing" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  the following command as root.:" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  # passwd -u ${amanda_user}" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  If this is not a new installation of Amanda and you have" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  pre-existing Amanda configurations in /etc/amanda" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  you should ensure that 'dumpuser' is set to '${amanda_user}'" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  in those configurations.  Additionally, you should ensure" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  that /var/lib/amanda/.amandahosts on your client systems" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  is properly configured to allow connections for the user" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  '${amanda_user}'." >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
		PASSWD_OK=0
	else
		echo "`date +'%b %e %Y %T'`:  !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!                                                       !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  The '${amanda_user}' user account for this system has been   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  created, however the user has no password set. For   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  security purposes this account  is normally locked   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  after creation.  Unfortunately,  when locking this   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  account an error occurred.  To ensure the security   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  of your system  you should set a password  for the   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  user account '${amanda_user}' immediately!  To set  such a   !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!  password, please issue the following command.:       !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!                                                       !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!   # passwd ${amanda_user}" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!!                                                       !!!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! !!!" >>${TMPFILE}
		PASSWD_OK=1
	fi
else
	# log information about '${amanda_user}' user parameters
	echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  The Amanda backup software is configured to operate as the" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  user '${amanda_user}'.  This user exists on your system and has not" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  been modified.  To ensure that Amanda functions properly," >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  please see that the following parameters are set for that" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  user.:" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  SHELL:          /bin/bash" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  HOME:           /var/lib/amanda" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  Default group:  ${amanda_group}" >>${TMPFILE}
	echo "`date +'%b %e %Y %T'`:  Verifying ${amanda_user} user parameters :" >>${TMPFILE}
	

        if [ "`id -gn ${amanda_user}`" != "${amanda_group}" ] ; then
                echo "`date +'%b %e %Y %T'`:  !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! !!!" >>${TMPFILE}
                echo "`date +'%b %e %Y %T'`:  !!! user '${amanda_user}' is not part of the ${amanda_group} group, !!!" >>${TMPFILE}
                echo "`date +'%b %e %Y %T'`:  !!! please make sure it is corrected before start using amanda  !!!" >>${TMPFILE}
                echo "`date +'%b %e %Y %T'`:  !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! !!!" >>${TMPFILE}
        else
                echo "`date +'%b %e %Y %T'`:  Verified group name of user '${amanda_user}'" >>${TMPFILE}
        fi

	echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
	PASSWD_OK=0
fi
if [ -d /var/lib/amanda ] ; then
	echo -n "`date +'%b %e %Y %T'`:  Checking ownership of '/var/lib/amanda'... " >>${TMPFILE}
	if [ "`ls -dl /var/lib/amanda | awk '//{split($_,x); print x[3]}'`" = "${amanda_user}" ] && \
	   [ "`ls -dl /var/lib/amanda | awk '//{split($_,x); print x[4]}'`" = "${amanda_group}" ] ; then
		echo "correct." >>${TMPFILE}
		VARLIB_OK=0
	else
		echo "incorrect!" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  Please ensure that the directory '/var/lib/amanda' is owned by" >>${TMPFILE}
		echo "`date +'%b %e %Y %T'`:  the user '${amanda_user}' and group '${amanda_group}'." >>${TMPFILE}
		VARLIB_OK=1
	fi
else
	VARLIB_OK=0
fi
echo "`date +'%b %e %Y %T'`:" >>${TMPFILE}
if [ ! -e ${LOGDIR} ] ; then
	# create log directory
	mkdir -m 0750 ${LOGDIR} >>${TMPFILE} 2>&1
	chown ${amanda_user}:${amanda_group} ${LOGDIR} >>${TMPFILE} 2>&1
fi

if [ ${PASSWD_OK} -eq 1 ] || [ ${VARLIB_OK} -eq 1 ] ; then
	cat ${TMPFILE}
	cat ${TMPFILE} >>${INSTALL_ERR}
	echo "Please review '${INSTALL_ERR}' to correct errors which have prevented the Amanda installation." >&2
	echo "Amanda installation log can be found in '${INSTALL_LOG}' and errors (if any) in '${INSTALL_ERR}'."
	exit 1
else
	cat ${TMPFILE}
	cat ${TMPFILE} >>${INSTALL_LOG}
fi

echo "`date +'%b %e %Y %T'`: === Amanda installation started. ===" >${TMPFILE}

cat ${TMPFILE}
cat ${TMPFILE} >>${INSTALL_LOG}
if [ -f "${TMPFILE}" ]; then
	rm -f "${TMPFILE}"
fi

