#!/bin/bash
#
###############################################################
#
# Main script to build LiveCD
# 
# will execute
# - ./customize-livecd.sh
# - ./mini-livecd.sh  (if -mini set)
# - ./linux-live.sl/runme.sh
# - ./restore-system.sh
#
# will take the configuration from
# - ./livecd.conf
#
# Urs Beyerle
#
###############################################################


###############################################################
### Functions
###############################################################

function usage() {

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

   cat <<EOF

  Options:

    -h,--help   print this screen

  Optional Options:

    -psi        customize for PSI LiveCD
    -mini       build a MINI-CD (not well tested)
    -dvd        build a LiveDVD
    
  Additionally Options for MINI-CD:

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

  Configuration file:

    See livecd.conf for more configuration options !!


EOF

exit

}

###############################################################
### Definitions and settings
###############################################################

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

while [ $# -gt 0 ]; do

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

done

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

### source livecd.conf
. livecd.conf

### test input
if [ ! $OS_RELEASE ]; then
    echo "ERROR: Please define OS_RELEASE in livecd.conf"
    exit 1
fi

### show warning, if not in chroot
mounts=$( mount 2>/dev/null | grep -v proc )
if [ "$mounts" != "" ]; then
    echo; echo "It seems that you do not build in chroot."
    echo -n "Do you really want to continue (y/N)? "
    read -n 1 key
    [ "$key" != "y" ] && exit
fi

### do not run without psi option on a PSI installation
if [ -e /etc/sysconfig/psi ] && [ ! $PSI ]; then
    echo "I guess this is a PSI installation, please use option '-psi'"; echo
    exit
fi

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

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

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

### write script version to /etc/livecd-release
SCRIPT_VERSION=$( cat version 2>/dev/null )
echo $SCRIPT_VERSION > /etc/livecd-release

### Check if we have the kernel, which is defined in livecd.conf
wrong_kernel=""
rpm -q kernel | grep -q $KERNEL
[ "$?" != "0" ] && wrong_kernel="yes"
ls /lib/modules/$KERNEL >/dev/null
[ "$?" != "0" ] && wrong_kernel="yes"
if [ "$wrong_kernel" ]; then
    echo "ERROR: You don't have kernel $KERNEL installed."
    echo "       Please check your settings in livecd.conf"
    exit 1
fi

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



###############################################################
### Build LiveCD
###############################################################

### display summary
### -----------------------------------------------------------

echo; echo "--------------------------------------------"
[ $DVD ]                  && echo "Build of LiveDVD started"
[ $MINI ]                 && echo "Build of MINI LiveCD started"
[ ! $DVD ] && [ ! $MINI ] && echo "Build of LiveCD started"
echo "Script version:      $SCRIPT_VERSION"
echo "Kernel version:      $KERNEL"
[ $SMP ] && echo "SMP Kernel version:  ${KERNEL}smp"
echo "--------------------------------------------"

[ $PSI ]  && echo "**** Build for PSI ****"
[ "$OS_RELEASE" = "4" ]  && echo "**** Build for SL4 ****"
[ "$OS_RELEASE" = "5" ]  && echo "**** Build for SL5 ****"
echo "--------------------------------------------"



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

rm -rf /tmp/doc
mkdir -p /tmp/doc/livecd
wget --timeout=3 --quiet -nH --mirror -X download -P /tmp/doc/livecd http://www.livecd.ethz.ch/
wget --timeout=3 --quiet -nH --mirror -P /tmp/doc/livecd http://www.livecd.ethz.ch/layout.css
[ -e /tmp/doc/livecd/layout.css ] && echo "done." || echo "download failed - not critical ;-)"



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



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



### Create /usr/share/doc/livecd
### -----------------------------------------------------------
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



### Prepare files in linux-live.sl directory 
### -----------------------------------------------------------
echo
echo "--------------------------------------------"
echo "cd linux-live.sl"
echo "--------------------------------------------"

cd linux-live.sl

### set CHANGESFOLDER name
if [ "$CHANGESFOLDER" = "" ]; then CHANGESFOLDER=changes; fi
sed -i "s|^CHANGES=.*|CHANGES=\$MEMORY/$CHANGESFOLDER|" initrd/linuxrc

### create isolinux.cfg from isolinux.cfg.tpl
echo "Create cd-root/isolinux.cfg"
cp cd-root/isolinux.cfg.tpl cd-root/isolinux.cfg

sed -i "s|append linux|append initrd=boot/initrd.gz init=linuxrc $BOOT_OPTIONS|" cd-root/isolinux.cfg
sed -i "s|append smp|append initrd=boot/initrd${SMP}.gz init=linuxrc $BOOT_OPTIONS|" cd-root/isolinux.cfg
sed -i "s|append failsafe|append initrd=boot/initrd.gz init=linuxrc $BOOT_OPTIONS $FAILSAFE_BOOT_OPTIONS|" cd-root/isolinux.cfg

### create splash.cfg from splash.cfg.tpl
echo "Create in splash.cfg"
cp cd-root/boot/splash.cfg.tpl cd-root/boot/splash.cfg
[ -e /etc/redhat-release ] && RELEASE=$( cat /etc/redhat-release )
ARCH64=""
[ "$ARCH" = "x86_64" ] && ARCH64="64bit"

if [ $PSI ]; then
    sed -i "s|Welcome to.*|Welcome to PSI $LIVECD_OS LiveCD ${ARCH64}|" cd-root/boot/splash.cfg
else
    sed -i "s|Welcome to.*|Welcome to $RELEASE ${ARCH64}|" cd-root/boot/splash.cfg
fi
[ $DVD ]  && sed -i "s|LiveCD|LiveDVD|"    cd-root/boot/splash.cfg
[ $MINI ] && sed -i "s|LiveCD|LiveMiniCD|" cd-root/boot/splash.cfg



### Run runme.sh
### -----------------------------------------------------------
echo
echo "--------------------------------------------"
echo "Run ./runme.sh"
echo "--------------------------------------------"
./runme.sh
rm -f cd-root/boot/splash.cfg
rm -f cd-root/isolinux.cfg



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



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