#!/bin/bash
#
###############################################################
#
# This will build the CD iso image in /tmp
# 
# it will execute:
# - ./customize-livecd.sh
# - ./runme.sh
# - ./restore-system.sh
#
# will take the configuration from
# - ./livecd.conf
#
# Urs Beyerle, PSI
#
###############################################################

# source livecd.conf
. livecd.conf

###############################################################

function usage() {

   ## Usage
   # ----------------------------------------------------------

   cat <<EOF

   Options:

    -h:         print this screen
    -psi:       customize for PSI Live CD
    -sl:        customize for SL Live CD

   Optional Options:

    -mini:      build a MINI-CD ROM
    -dvd:       build a Live DVD
    
    Additionally Options for MINI-CD:

    -nomove:    do NOT move to /mini to save diskspace 
    -rootlogin: directly login as root


EOF

}

### read options from command-line
PSI=""
OPTIONS=$@

while [ $# -gt 0 ]; do

    case "$1" in
       -h)
            usage; exit;;
       -psi)
            PSI="-psi"; shift; continue;;
       -sl)
            SL="-sl"; shift; continue;;
       -mini)
            MINI="-mini"; shift; continue;;
       -nomove)
            NOMOVE="-nomove"; shift; continue;;
       -rootlogin)
            ROOTLOGIN="-rootlogin"; shift; continue;;
       -dvd)
            DVD="-dvd"; shift; continue;;
       *)
            usage; exit;;
    esac

done

### only build for SL or PSI
if [ ! $PSI ] && [ ! $SL ]; then
    usage 
    exit
fi

### check if I run inside my directory
if [ ! -x $( basename $0 ) ]; then
    echo "Please run $( basename $0 ) within its directory"
    exit 1
fi

### add extra boot options
BOOT_OPTIONS="$BOOT_OPTIONS $EXTRA_BOOT_OPTIONS"

### add option "psi" for PSI
[ $PSI ] && BOOT_OPTIONS="psi $BOOT_OPTIONS"

ARCH=$( /bin/arch )
[ "$ARCH" != "x86_64" ] && ARCH=i686

if [ -e /etc/redhat-release ]; then
    RELEASE=$( cat /etc/redhat-release )
else
    RELEASE="my"
fi

### write my version to /etc/livecd-release
my_pwd=$( pwd )
my_version=${my_pwd##*/}
echo $my_version > /etc/livecd-release

### Check for SMP kernel and set SMP variable
SMP=""
if [ -e /boot/vmlinuz-${KERNEL}smp ]; then
    # if you change SMP="s", you have also to change isolinux.cfg!
    # SMP can only be one character in lenght!
    export SMP="s"
fi

### start
echo
echo "--------------------------------------------"
if [ $DVD ]; then 
    echo "Build of LiveDVD started"
else
    echo "Build of LiveCD started"
fi
echo "Script version:      $my_version"
echo "Kernel version:      $KERNEL"
[ $SMP ] && echo "SMP Kernel version:  ${KERNEL}smp"
echo "--------------------------------------------"

[ $PSI ]  && echo "**** Build for PSI ****"
[ $SL ]   && echo "**** Build for SL ****"
[ $MINI ] && echo "**** Build for MINI-CD ****"
echo "--------------------------------------------"


# download livecd web page (for documentation)
echo "Try to download latest livecd documentation"

rm -rf /tmp/doc
mkdir -p /tmp/doc
wget --timeout=3 --quiet -nH --mirror -P /tmp/doc http://linux.web.psi.ch/livecd/
wget --timeout=3 --quiet -nH --mirror -P /tmp/doc http://linux.web.psi.ch/livecd/layout.css
if [ -d /tmp/doc/livecd ]; then
    echo "done."
else
    echo "download failed - not critical ;-)"
fi


# clean up build script directory
echo "--------------------------------------------"
echo "Remove all *~ files in $( pwd )"
find . | grep "~$" | while read f; do rm -f "$f"; done
echo "done."
echo "--------------------------------------------"


echo
echo "--------------------------------------------"
echo "Run ./customize-livecd.sh $PSI $DVD"
echo "--------------------------------------------"
./customize-livecd.sh $PSI $DVD $MINI


if [ $MINI ]; then
    echo "--------------------------------------------"
    echo "Run ./mini-livecd.sh $PSI $ROOTLOGIN $NOMOVE"
    echo "--------------------------------------------"
    ./mini-livecd.sh $PSI $ROOTLOGIN $NOMOVE
fi

if [ -d /tmp/doc/livecd ]; then
    echo
    echo "--------------------------------------------"
    echo "Create /usr/share/doc/livecd"
    echo "--------------------------------------------"
    mkdir -p /usr/share/doc/HTML
    cp -a customize/HTML/index.html /usr/share/doc/HTML/index.html    
    rm -rf /usr/share/doc/livecd
    cp -a /tmp/doc/livecd /usr/share/doc/
    echo "done."
    echo
fi

echo
echo "--------------------------------------------"
echo "cd linux-live.sl4"
echo "--------------------------------------------"

cd linux-live.sl4

echo "Set kernel boot options in cd-root/isolinux.cfg"
# for lable linux
sed -i "s|append l initrd=boot/initrd.gz init=linuxrc .*|append l initrd=boot/initrd.gz init=linuxrc $BOOT_OPTIONS|" cd-root/isolinux.cfg
# for lable linuxsmp
sed -i "s|append l initrd=boot/initrd${SMP}.gz init=linuxrc .*|append l initrd=boot/initrd${SMP}.gz init=linuxrc $BOOT_OPTIONS|" cd-root/isolinux.cfg
# for lable failsafe
sed -i "s|append f initrd=boot/initrd.gz init=linuxrc .*|append f initrd=boot/initrd.gz init=linuxrc $BOOT_OPTIONS $FAILSAFE_BOOT_OPTIONS|" cd-root/isolinux.cfg

echo "Set 'Welcome ... to' in splash.cfg"
sed -i "s|DVD|CD|" cd-root/boot/splash.cfg

if [ $PSI ]; then
    sed -i "s|Welcome to.* LiveCD.*|Welcome to PSI SL4 LiveCD|" cd-root/boot/splash.cfg
else
    sed -i "s|Welcome to.* LiveCD.*|Welcome to $RELEASE|" cd-root/boot/splash.cfg
fi

sed -i "s|LiveCD 64bit|LiveCD|" cd-root/boot/splash.cfg
if [ "$ARCH" = "x86_64" ]; then
    sed -i "s|LiveCD|LiveCD 64bit|" cd-root/boot/splash.cfg
fi

if [ $DVD ]; then
    sed -i "s|CD|DVD|" cd-root/boot/splash.cfg
fi

echo "Set KERNEL=$KERNEL in config"
sed -i "s|^KERNEL=.*|KERNEL=$KERNEL|" config

echo "Set RAM0SIZE=$RAM0SIZE in config"
sed -i "s|^RAM0SIZE=.*|RAM0SIZE=$RAM0SIZE|" config

echo "Set ramdisk_size=$RAMDISK_SIZE in cd-root/boot/DOS/config"
sed -i "s|^ramdisk_size=.*|ramdisk_size=$RAMDISK_SIZE|" cd-root/boot/DOS/config

echo
echo "--------------------------------------------"
echo "Run ./runme.sh"
echo "--------------------------------------------"
./runme.sh

echo
echo "--------------------------------------------"
echo "Now fix things to make this system bootable"
echo "--------------------------------------------"
cd ..
./restore-system.sh $PSI


echo "--------------------------------------------"
echo "End of Build LiveCD" 
echo "See /tmp for the ISO image"
echo "--------------------------------------------"
