#!/bin/bash

# This script is intended to be run from a cron job by a local account
# on the Debathena build server--i.e. debuild@debuild.mit.edu. The
# account should have
#
# * A GPG secret key to sign the checksums for the live CDs.
#
# * A file named $HOME/autolivebuilder.config specifying shell
#   variable assignments for:
#   - error_addr: An email address to send errors to
#   - release: The Ubuntu release to base the live CDs off of
#   - release_version: The version number of that release
#   - arch: Which architecture to use for the live CD
#   - mirror: Which Ubuntu mirror to use
#   - gpg_opts: A bash array of options to pass to gpg. This should
#     include which keys to sign the checksums files with
#   - live_dir: The directory to put the live CDs into

# Send all output to a logfile.
mkdir -p "$HOME/autolivebuilder-logs"
logfile=$HOME/autolivebuilder-logs/$(date '+%Y-%m-%d-%H').log
exec </dev/null >>$logfile 2>&1

# Set default configuration values
mirror='ubuntu.media.mit.edu'

# Read configuration; if it's not complete, do nothing.
config=$HOME/autolivebuilder.config
. $config
if (! [ -n "$error_addr" ]) || \
    (! [ -n "release" ]) || \
    (! [ -n "$release_version" ]) || \
    (! [ -n "$arch" ]) || \
    (! [ -n "${gpg_opts[*]}" ]) || \
    (! [ -n "$live_dir" ]); then
    echo "Incomplete $config; doing nothing."
    exit
fi

suppress=$HOME/autolivebuilder.suppress
if [ -e "$suppress" ]; then
    echo "$suppress exists; not running."
    exit
fi

# Make sure runs of this script do not overlap.
runfile=$HOME/autolivebuilder.running
if [ -e "$runfile" ]; then
  # A previous invocation appears to still be running.
  pid=$(cat "$runfile")
  if ! kill -0 "$pid" 2>/dev/null; then
    # Okay, it's not really running.  Perhaps the machine has
    # rebooted.  Wait for manual intervention before running again.
    echo "Stale runfile."
  else
    echo "$runfile exists; not running."
  fi
  exit
fi
echo $$ > $runfile

# Put here cleanup that should always happen
clean_up () {
    err=$?

    set +e

    touch "$suppress"
    rm "$runfile"
    /usr/sbin/sendmail -t <<EOF
From: Autolivebuilder <$error_addr>
To: Autolivebuilder <$error_addr>
Subject: Autolivebuilder failure

Autolivebuilder has experienced a failure.

Please see $logfile on $(hostname) for more details.

Autolivebuilder will stop running until $suppress is removed.
EOF

    if [ -n "$session" ]; then
	schroot -ec "$session"
    fi

    exit $err
}

trap clean_up ERR
set -e

cd $HOME/live

if ! [ -d debathena-live ]; then
    echo "E: The Debathena Live CD builder is not checked out."
    echo "E: Run 'git clone file:///mit/debathena/git/debathena-live.git' in"
    echo "E: $HOME/live"
    false
fi
pushd debathena-live
  # This will break if we ever add an epoch. So don't do that.
  version="$(dpkg-parsechangelog | sed -ne 's/^Version: //p')"
  debuild -us -uc -b
popd

iso="ubuntu-${release_version}-desktop-${arch}.iso"
wget -N "http://${mirror}/ubuntu-releases/${release_version}/$iso"

wget -N "http://${mirror}/ubuntu-releases/${release_version}/SHA1SUMS"
wget -N "http://${mirror}/ubuntu-releases/${release_version}/SHA1SUMS.gpg"

gpg --verify SHA1SUMS.gpg SHA1SUMS
grep "$iso" SHA1SUMS | sha1sum -c

session="$(schroot -bc "${release}-${arch}-sbuild")"

schroot -rc "$session" -u root -- sh -c 'apt-get update && apt-get -y dist-upgrade && apt-get -y autoremove'
schroot -rc "$session" -u root -- dpkg -i "debathena-livecd-tools_${version}_all.deb" || :
schroot -rc "$session" -u root -- apt-get install -y -f

date="$(date "+%Y%m%d")"
da_iso="ubuntu-${release_version}-debathena-${date}-${arch}.iso"
da_dvd_iso="ubuntu-${release_version}-debathena-${date}-${arch}-dvd.iso"

schroot -rc "$session" -u root -- debathena-livecd-convert "$iso" "$da_iso"
schroot -rc "$session" -u root -- debathena-livecd-convert --dvd "$iso" "$da_dvd_iso"

cp "$da_iso" "$da_dvd_iso" "${live_dir}"
sha1sum "$da_iso" >"${live_dir}/${da_iso}.sha1sum"
sha512sum "$da_iso" >"${live_dir}/${da_iso}.sha512sum"
sha1sum "$da_dvd_iso" >"${live_dir}/${da_dvd_iso}.sha1sum"
sha512sum "$da_dvd_iso" >"${live_dir}/${da_dvd_iso}.sha512sum"

gpg --batch --yes "${gpg_opts[@]}" -a -b "${live_dir}/${da_iso}.sha1sum"
gpg --batch --yes "${gpg_opts[@]}" -a -b "${live_dir}/${da_iso}.sha512sum"
gpg --batch --yes "${gpg_opts[@]}" -a -b "${live_dir}/${da_dvd_iso}.sha1sum"
gpg --batch --yes "${gpg_opts[@]}" -a -b "${live_dir}/${da_dvd_iso}.sha512sum"

kdestroy
rm -f "$runfile"
