#!/bin/sh

# live-webhelper - web interface to live-helper
# Copyright (C) 2007 Richard Nelson <unixabg@gmail.com>
# Copyright (C) 2007 Daniel Baumann <daniel@debian.org>
#
# live-webhelper comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
# This is free software, and you are welcome to redistribute it
# under certain conditions; see COPYING for details.

# Reading common
. /usr/share/live-webhelper/common

# Reading defaults
if [ -r /etc/default/live-webhelper ]
then
	. /etc/default/live-webhelper
else
	echo "E: /etc/default/live-webhelper missing"
	exit 1
fi

# Exit if disabled
if [ "${WEBBUILD}" != "enabled" ]
then
	exit 0
fi

# Checking lock file
if [ -f /var/lock/live-webhelper.lock ]
then
	echo "E: live-webhelper already/still running."
	exit 1
fi

# Creating lock trap
trap "test -f /var/lock/live-webhelper.lock && rm -f /var/lock/live-webhelper.lock; exit 0" 0 1 2 3 9 15

# Creating lock file
touch /var/lock/live-webhelper.lock

# Cleanup old builds: cron should be run at least once per hour to take effect
if ls "${DESTDIR}"/`date -d yesterday +%Y%m%d.%H`* > /dev/null 2>&1
then
	rm -rf "${DESTDIR}"/`date -d yesterday +%Y%m%d.%H`*

	echo "`date +%b\ %d\ %H:%M:%S` ${HOSTNAME} live-webhelper: remove web build (`date -d yesterday +%Y%m%d.%H`*)." >> /var/log/live
fi

# Ok from here spin through the live-webhelper files we have waiting to build
if ls "${TEMPDIR}"/*.build > /dev/null 2>&1
then
	for FILE in "${TEMPDIR}"/*.build
	do
		# Pull in the variables we want from the config file.
		# Pul in the build number
		BUILD=`awk -F\" '/^BUILD=/{print $2}' ${FILE}`
		# Pull in the email address
		EMAIL=`awk -F\" '/^EMAIL=/{print $2}' ${FILE}`
		
		# Drop out some build data for information if something goes wrong.
		echo "`date +%b\ %d\ %H:%M:%S` ${HOSTNAME} live-webhelper: begin web build (${BUILD})." >> /var/log/live

		# Creating build directory which also creates the config/chroot_sources folder
		mkdir -p "${TEMPDIR}"/"${BUILD}"/config/chroot_sources

		# The next two tests are for unofficial third party repositories
		if [ -n "${CUSTOM_BOOTSTRAP}" ]
		then
			echo "${CUSTOM_BOOTSTRAP}" > "${TEMPDIR}"/"${BUILD}"/config/chroot_sources/custom.bootstrap
		fi

		if [ -n "${CUSTOM_BINARY}" ]
		then
			echo "${CUSTOM_BINARY}" > "${TEMPDIR}"/"${BUILD}"/config/chroot_sources/custom.binary
		fi

		DATE_START="`date -R`"
		echo "Begin: ${DATE_START}" > "${TEMPDIR}"/"${BUILD}"/log

		# Generating image
		cd "${TEMPDIR}"/"${BUILD}"
		lh_config -c ${FILE} >> "${TEMPDIR}"/"${BUILD}"/log 2>&1
		ERRORCONFIG="${?}"
		lh_build >> "${TEMPDIR}"/"${BUILD}"/log 2>&1
		ERRORBUILD="${?}"

		DATE_END="`date -R`"
		echo "End: ${DATE_END}" >> "${TEMPDIR}"/"${BUILD}"/log

		# Creating image directory
		mkdir -p "${DESTDIR}"/"${BUILD}"

		# Creating mail
		if [ "${ERRORCONFIG}" -eq "0" ] && [ "${ERRORBUILD}" -eq "0" ]
		then
			STATUS="maybe-successful"
		else
			STATUS="maybe-failed"
		fi

		sed -e "s/BUILD/${BUILD}/g" \
		    -e "s/EMAIL/${EMAIL}/" \
		    -e "s/VERSION/${VERSION}/" \
		    -e "s/DATE_START/${DATE_START}/" \
		    -e "s/DATE_END/${DATE_END}/" \
		    -e "s/STATUS/${STATUS}/" \
		    -e "s#SERVER#${SERVER}#" \
		"${TEMPLATES}"/mail.txt > "${DESTDIR}"/"${BUILD}"/mail

		# Moving binary image
		if ls "${TEMPDIR}"/"${BUILD}"/binary.* > /dev/null 2>&1
		then
			mv "${TEMPDIR}"/"${BUILD}"/binary.* "${DESTDIR}"/"${BUILD}"
		fi

		# Moving source image
		if ls "${TEMPDIR}"/"${BUILD}"/source.* > /dev/null 2>&1
		then
			mv "${TEMPDIR}"/"${BUILD}"/source.* "${DESTDIR}"/"${BUILD}"
		fi

		# Moving build
		mv "${TEMPDIR}"/"${BUILD}".build "${DESTDIR}"/"${BUILD}"/build

		# Moving log
		mv "${TEMPDIR}"/"${BUILD}"/log "${DESTDIR}"/"${BUILD}"

		# Generating md5sum
		cd "${DESTDIR}"/"${BUILD}"
		md5sum * > md5sum
		cd "${OLDPWD}"

		# Sending mail
		cat "${DESTDIR}"/"${BUILD}"/mail | /usr/sbin/sendmail -t

		# Unmounting proc
		if [ -f "${TEMPDIR}"/"${BUILD}"/chroot/proc/version ]
		then
			umount "${TEMPDIR}"/"${BUILD}"/chroot/proc
		fi

		# Unmounting sysfs
		if [ -d "${TEMPDIR}"/"${BUILD}"/chroot/sys/kernel ]
		then
			umount "${TEMPDIR}"/${BUILD}/chroot/sys
		fi

		# Removing build directory
		rm -rf "${TEMPDIR}"/"${BUILD}"

		echo "`date +%b\ %d\ %H:%M:%S` ${HOSTNAME} live-webhelper: end web build (${BUILD}: ${STATUS})." >> /var/log/live
	done
fi
