#!/bin/sh
#
# (c) 2006, 2007	Holger Levsen <debian@layer-acht.org>
# (c) 2007 		    Petter Reinholdtsen <pere@hungry.com>
# (c) 2007, 2008    Andreas Putzo <andreas@putzo.net>
#
# Licended under GPL2
#
# Build script for Debian Gis Live CD.
# Based on build script for Debian Edu live CD.
#
# Build depends: live-helper tasksel
#
# Tested using live-helper version 1.0.0-2
#
##############################################################################

# Exit on errors
set -e

# Config options. See also lh_config call below.
verbose=1
build_dir=build
publish_dir=$(pwd)/pub
logfile=`mktemp`
today="`date +%Y%m%d%H%M`"
lockfile="/var/lock/gislive-build"
mirror=http://ftp.de.debian.org/debian
dist=lenny
# package (-lists) to install
package_list_dir=$(pwd)/packages
# scripts to execute in the chroot
hook_dir=$(pwd)/hooks
# Files to copy into the chroot to /
misc_files_dir=$(pwd)/files
publisher="Debian GIS Project"
live_username="gis"
live_hostname="Debian-GIS"
# CD boot image
#splashpng=foo.png

export LH_PACKAGES_LISTS="kde debian-gis base base2"

# helper functions
msg_info() {
  echo info: $@
}

msg_error() {
  echo error: $@
}

at_exit() {
    rm $lockfile
    if [ -f $logfile ] ; then
    	grep -v ^
 $logfile > $publish_dir/gislive-${today}.log
	    chmod 644 $publish_dir/gislive-${today}.log
    	rm $logfile
    fi
    cd -
}

####
trap at_exit EXIT
if [ "$verbose" ]; then
    tail -f $logfile &
fi    

# Some steps need to run as root. Use sudo unless already root
if [ root != "$(whoami)" ] ; then
    SUDO=sudo
fi

# check lockfile
if [ -f "$lockfile" ] ; then
	echo $lockfile exists, aborting
	exit 1
fi
$SUDO echo $$ > $lockfile

# check publishdir
if [ ! -d "$publish_dir" ] ; then
    msg_error "$publish_dir does not exist, aborting."
    exit 1
fi

# cleanup from previous builds
[ -d "$build_dir" ] || mkdir -p $build_dir
cd $build_dir
if lh_clean --all; then
    msg_info cleaning old build dir succeeded
    rm -rf config
else
    msg_error generating configuration failed
#    exit 1
fi

# configure live-helper
# Rewrite volume name to get it short enought when dist=squeeze.
export LH_BOOTAPPEND_LIVE="quiet splash"
if /usr/bin/lh_config \
    --apt aptitude \
    --bootappend "quiet splash" \
    --mirror-bootstrap "$mirror" \
    --mirror-binary "$mirror" \
    --distribution "$dist" \
    --categories 'main' \
    --apt-recommends disabled \
    --union-filesystem aufs \
    --linux-packages 'linux-image-2.6 aufs-modules-2.6 squashfs-modules-2.6' \
    --iso-publisher "$publisher" \
    --iso-volume "DebGisLive $dist (DATE)" \
    --username "$live_username" \
    --hostname "$live_hostname" \
    --verbose

#    --keyring-packages debian-archive-keyring \
then
  msg_info generating configuration succeeded
else
  msg_error generating configuration failed
  exit 1
fi

# copy hooks
if [ -d "$hook_dir" ]; then
    for hook in $(ls "$hook_dir"); do
        cp -v $hook_dir/$hook config/chroot_local-hooks
    done
fi

# copy package lists
if [ -d "$package_list_dir" ]; then
    for list in $(ls "$package_list_dir"); do
        cp -v $package_list_dir/$list config/chroot_local-packageslists
    done
fi

# copy misc files 
if [ -d "$misc_files_dir" ]; then
    ( cd "$misc_files_dir" && find . ! \( -type d -a -name ".svn" -prune \) ) | while read file; do
        if [ -d "$misc_files_dir/$file" ]; then
            mkdir -p config/chroot_local-includes/$file
        elif [ -f "$misc_files_dir/$file" ]; then
            cp -v $misc_files_dir/$file config/chroot_local-includes/$file
        fi            
    done
fi

# prepare chroot
if $SUDO /usr/bin/lh_bootstrap >> $logfile 2>&1; then
    msg_info "bootstrap succeeded."
else
    msg_error "bootstrap failed."
    exit 1
fi

if $SUDO /usr/bin/lh_chroot >> $logfile 2>&1; then
    msg_info "creating chroot succeeded."
else
    msg_error "creating chroot failed."
    exit 1
fi    

# Generate RLE from a splash image PNG
# Color index 0 is background, and index 7 is foreground.  Set to
# black and white respecively
if [ -f "$splashpng" ] ; then
    pngtopnm < $splashpng | ppmquant 16 2>/dev/null | \
	ppmtolss16 "#ffffff=7" "#000000=0"  2>/dev/null \
	> config/binary_syslinux/splash.rle
fi

# call lh_binary to build the iso image and publish the result
if $SUDO /usr/bin/lh_binary >> $logfile 2>&1; then
  msg_info "build succeeded."
else
  msg_error "build failed."
  exit 1
fi
if [ -f binary.iso ] ; then
  mv binary.iso $publish_dir/gislive-${dist}-${today}.iso
  md5sum $publish_dir/gislive-${dist}-${today}.iso > $publish_dir/gislive-${dist}-${today}.md5sum
  msg_info successfully build live-cd.
else 
  echo "#########################################################"
  echo 
  echo "An error occured, .iso was not created :-("
  echo 
  if [ ! "$verbose" ]; then
      echo "30 last lines of the make-live output:"
      echo
      tail -30 $logfile
  fi          
fi 

# cleanup old images and logs
find $publish_dir -mtime +1 !  -name "gislive-*00.???" -exec rm {} \;
find $publish_dir -mtime +2 !  -name "gislive-1600.???" -exec rm {} \;
find $publish_dir -mtime +14 ! -name "gislive-0400.???" -exec rm {} \;
