#!/bin/bash
BUILD_LOG=~/public_html/BuildLive.out
ALWAYS_SEND_LOG=yes

finish() {
    [ $? != "0" ] && STATUS="Failed" || STATUS="Successful"
    if [ "$STATUS" = "Failed" ] || [ "$ALWAYS_SEND_LOG" = "yes" ]; then
    	SEND_LOG=yes
    fi
    if [ -e "$LOG" ]; then
        echo "======================= Log of livefs.sh output follows ======================="
        cat $LOG
    fi
    if [ "$SEND_LOG" = "yes" ]; then
        exec >/dev/null 2>&1
        mailx -s "LiveCD: $STATUS $STE:$arg${SUBARCH:+-$SUBARCH} build on ${ARCH}" buildd+porters < "$BUILD_LOG"
    fi
    rm -f ~/buildLiveCD.lock
    nohup ~/bin/pruneLiveCD >/dev/null 2>&1 &
}

if [ -n "$SSH_ORIGINAL_COMMAND" ]; then
    set -- $SSH_ORIGINAL_COMMAND
    [ ${1##*/} != 'BuildLiveCD' ] && echo bad command >&2 && exit 2
    shift
fi
ARCH=$(dpkg --print-architecture)
SUBARCH=""
SUBARCHARG=""
NEWSUITE="natty"
SUITES=""
PROPOSED=""
IMAGEFORMAT=""

while getopts :s:d:f:pa: name; do case $name in
    s)	SUBARCH="$OPTARG";;
    d)	NEWSUITE="$OPTARG";;
    f)	IMAGEFORMAT="-f$OPTARG";;
    p)	PROPOSED="-p";;
    a)	APT_SOURCE="-A $OPTARG";;
esac; done;
shift $((OPTIND-1))

if [ -n "$SUBARCH" ]; then
    SUBARCHARG="-s$SUBARCH"
fi

for s in $NEWSUITE; do
    if [ -d build-${s}-live/chroot-${s} ]; then SUITES="$SUITES $s"; fi
done
if [ -z "$SUITES" ]; then
    echo "No valid suites to build for" >&2 && exit 1
fi
for s in $SUITES; do
    case $s in
        dapper|edgy|feisty|gutsy|hardy)
            ARCHARG=""
            old_suite="yes"
            ;;
        *)
            ARCHARG="-a$ARCH"
            new_suite="yes"
            ;;
    esac
done
if [ "$new_suite" = "yes" ] && [ "$old_suite" = "yes" ]; then
    echo "Can't build images for suites older than intrepid and suites" >&2
    echo "greater than hardy at the same time, please use two invocations" >&2
    exit 1
fi
if (( $# == 0 )) || [ "X$1" = "Xall" ]; then
    set -- ubuntu kubuntu kubuntu-mobile kubuntu-netbook edubuntu xubuntu mythbuntu base
    if [ "$ARCH" = "i386" ]; then
        set -- ubuntu ubuntu-dvd ubuntu-lpia ubuntu-netbook kubuntu kubuntu-dvd kubuntu-mobile kubuntu-netbook edubuntu edubuntu-dvd xubuntu mythbuntu base
    fi
fi

case ${ARCH} in
    hppa|sparc|powerpc) LINUX32=linux32;;
    *) LINUX32="";;
esac

for arg in "$@"; do
    case "$arg" in
	*ubuntu|*ubuntu-dvd|kubuntu-mobile|kubuntu-netbook|ubuntu-lpia|ubuntu-mid|ubuntu-umpc|ubuntu-mobile|ubuntu-netbook|ubuntu-moblin-remix|mythbuntu|base|tocd)	;;
	*)
	    echo "bad project: $arg" >&2
	    exit 2
	    ;;
    esac
done
if [ "`echo $@ | wc -w`" -gt "1" ]; then
    for arg in "$@"; do
        $0 $arg
    done
    exit 0
fi

lockfile ~/buildLiveCD.lock || exit 1
trap "finish" 0
trap "rm -f ~/buildLiveCD.lock; exit 1" 1 2 3 15
exec > $BUILD_LOG 2>&1
set -e

for STE in $SUITES; do
    if [ -d ~/build-${STE}-live/chroot-${STE} ]; then
	sudo chroot ~/build-${STE}-live/chroot-${STE} apt-get -qq update || true
	sudo chroot ~/build-${STE}-live/chroot-${STE} apt-get -y dist-upgrade || true
    fi
done
for STE in $SUITES; do
    if cd ~/build-${STE}-live 2>/dev/null; then
	DIR=~/build-${STE}-live/chroot-${STE}/./build/
    else
	cd ~/build.live
	DIR=~/build.live/chroot-${STE}/./build/
    fi

    for arg in "$@"; do
	PUBDIR=~/public_html/LiveCD/${STE}/${arg}${SUBARCH:+-$SUBARCH}/
	mkdir -p ${PUBDIR}

	NOW=$(date +%Y%m%d)
	if [ -d ${PUBDIR}${NOW} ]; then
	    typeset -i n=1; while [ -d ${PUBDIR}${NOW}.$n ]; do n=n+1; done
	    NOW=${NOW}.$n
	fi
	NOW=${NOW}
	LOG=${PUBDIR}${NOW}/livecd-${NOW}-${ARCH}.out

	rm -f ${PUBDIR}latest
	ln -sf ${PUBDIR}${NOW} ${PUBDIR}latest
	mkdir -p ${PUBDIR}${NOW}
	if $LINUX32 sudo chroot ${DIR%/./*} sh -c "cd /${DIR#*/./} && /usr/sbin/livecd.sh ${SUBARCHARG} ${APT_SOURCE} ${PROPOSED} -d${STE} ${IMAGEFORMAT} ${ARCHARG} ${IMAGEARG} $arg" > ${LOG} 2>&1; then
	    rm -f ${PUBDIR}current
	    ln -sf ${NOW} ${PUBDIR}current

	    for file in ${DIR}livecd.*; do
		if [ -f ${file} ]; then
		    ext=${file##*.}
		    base=${file%.*}; base=${base##*/}
		    sudo chown buildd ${file}
		    sudo mv ${file} ${PUBDIR}${NOW}/${base}-${NOW}-${ARCH}.${ext}
		    ln -sf ${base}-${NOW}-${ARCH}.${ext} ${PUBDIR}${NOW}/${base}.${ext}
		fi
	    done

	    for f in ${PUBDIR}${NOW}/livecd.*.cloop-*; do 
		target=${f%-*}
		if [ ! -f ${target} ]; then
		    for fsbs in 4096:4096 1024:65536; do
			if [ -f ${target}-${fsbs} ]; then
			    ln -sf ${target##*/}-${fsbs} ${target}
			    break
			fi
		    done
		    if [ ! -f ${target} ]; then
			best=$(ls -ld ${target}-* | sort -k5,5n| awk '{print $NF; quit}')
			ln -sf ${best##*/} ${target}
		    fi
		fi
	    done
	else
	    exit 1
	fi
    done
done