#!/bin/sh

#    This script builds palinux netinst ISO and lifimage
#    Copyright (C) 2002  Thibaut Varene <varenet@esiee.fr>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Last Modified: 2003-07-15 02:41 CEST by T. Varene
# $Id: isobuild.sh,v 1.7 2003/09/04 18:06:45 varenet Exp $

IBS_VERSION="1.0.1"

# This is the PA/Linux ESIEE Team ISO Build Script (IBS).
# This script does nothing but actually building the files.
# No update is performed.
# No check for identical files is done.
# It is assumed that all files are already present (including an ISO tree).
# Of course it is assumed that the host runs a Debian GNU/Linux properly configured.
# The various utilities have to be in the PATH.

# BRIEF SUMMARY OF THE PROCESS:
# Once the root check is done and files have been cleaned up,
# the script will backup the previous kernel .deb if any
# into a folder named 'archive-date-hour', if set up to.
# Then it will build the kernel debs, place them where needed,
# build the boot-floppies, prepare them, compress the lifimage,
# set up the b-f into the iso tree, build the ISO,
# and finally do PALO onto the ISO.
# The output is logged to a file specified at the beginning of
# the build, this file is removed at the end if the build succeeds.
# All files are released inside the $IBS_BASEDIR directory.
# (See the configuration section bellow).

# REQUIREMENTS:
# In $IBS_BASEDIR there has to be:
#   linux-2.?.tar.gz
#   patterns/boot-floppies-*/
#   patterns/kernel-image-VERSION-hppa-*/
#   patterns/Makefile.patch.pat
#   patches/
#   
# Take care to have only *ONE* version of the files,
# as all 'cd' are done using the wildcard '*' 
# You must already have created the '/archive/debian'.

# For a better understanding, here's the layout of my tree:
# $ ls -l /work/woody-chroot/newiso/
# dr-xr-xr-x    9 varenet  root         4096 Jun 22  2002 isofolder
# -rw-r--r--    1 varenet  users    37980160 Sep  3 12:31 linux-2.4.tar.gz
# drwxr-xr-x    2 root     root         4096 Jul 15 02:18 patches
# drwxr-xr-x    4 varenet  users        4096 Jul 15 02:43 patterns
# $ ls -l /work/woody-chroot/newiso/patches/
# -rw-r--r--    1 varenet  root          680 Sep  3 18:37 Makefile.patch
# $ ls -l /work/woody-chroot/newiso/patterns/
# -rw-r--r--    1 varenet  users         721 Jul 15 02:28 Makefile.patch.pat
# drwxr-xr-x   13 varenet  users        4096 Jul 15 02:10 boot-floppies-3.0.23
# drwxr-xr-x    3 varenet  root         4096 Jul 15 01:03 kernel-image-VERSION-hppa-12.1


#  CONFIGURATION SECTION   #
IBS_BACKUP=NO				# Set this to 'YES' to enable backup.
IBS_BASEDIR=/newiso	# Where all the source files lies
IBS_DEBARCHIVE=/archive/debian		# You should not need to change that
IBS_ISOFOLDER=$IBS_BASEDIR/isofolder	# Where the ISO tree stands
IBS_BFNONMAINTAINER="varenet@parisc-linux.org"	# To tag '-m' the b-f build
#    END CONFIG SECTION    #


# As usual, nothing should be changed below this line.
######################################################


# Routine to check any problem
test_exit()
{
	IBS_RETURN=$?
	if [ $IBS_RETURN -ne "0" ]; then
		echo -e "\a\nFAILURE!"
		exit $IBS_RETURN
	fi
}

# Nice stuff displayed, be user-friendly... :)
echo "$0 v.${IBS_VERSION} starting, please fasten seatbelts."

# Minimal root check (only working on debian)
echo -n "Testing we are run as root... "
dh_testroot
test_exit
echo "Done."

# Doing cleanup of potential former builds
echo -n "Cleaning up potential old files... "
cd $IBS_BASEDIR
rm -rf build
mkdir ${IBS_BASEDIR}/build
echo "Done."

# Defining constants
IBS_MINIDATE=`date +%Y%m%d`
IBS_MINIHOUR=`date +%H%M`
IBS_BFDATE=`date +%Y-%m-%d`
IBS_BUILDLOG="$IBS_BASEDIR/build/build-$IBS_MINIDATE-$IBS_MINIHOUR.log"

echo "Logging output to $IBS_BUILDLOG"

# Preparing all the stuff for build sequence
echo -n "Preparing the files... " | tee -a $IBS_BUILDLOG
cd $IBS_BASEDIR
tar zxOf linux-2.?.tar.gz linux-2.?/Makefile | head -10 > ${IBS_BASEDIR}/build/Makefile.orig

# Mandatory constants used everywhere
IBS_KVERSION=`grep ^VERSION ${IBS_BASEDIR}/build/Makefile.orig | head -1 | awk '{ print $3 }'`
IBS_KPATCHLEVEL=`grep ^PATCHLEVEL ${IBS_BASEDIR}/build/Makefile.orig | head -1 | awk '{ print $3 }'`
IBS_KSUBLEVEL=`grep ^SUBLEVEL ${IBS_BASEDIR}/build/Makefile.orig | head -1 | awk '{ print $3 }'`
IBS_KEXTRAVERSION=`grep ^EXTRAVERSION ${IBS_BASEDIR}/build/Makefile.orig | head -1 | awk '{ print $3 }'`
IBS_KERNMINIVERSION=${IBS_KVERSION}.${IBS_KPATCHLEVEL}.${IBS_KSUBLEVEL}
IBS_KERNISOVERSION=${IBS_KVERSION}.${IBS_KPATCHLEVEL}.${IBS_KSUBLEVEL}${IBS_KEXTRAVERSION}
IBS_KERNSRCDIR="`ls -d patterns/kernel-image-VERSION* | cut -d"/" -f2`"
IBS_KERNSRCDDIR="`echo $IBS_KERNSRCDIR | sed -e s/VERSION/$IBS_KERNMINIVERSION/`"

# Now we create package files according to the kernel version
cp -arf ${IBS_BASEDIR}/patterns/$IBS_KERNSRCDIR ${IBS_BASEDIR}/build/$IBS_KERNSRCDDIR
test_exit
for i in `find ${IBS_BASEDIR}/build/$IBS_KERNSRCDDIR -name "*.pat" -print`
do
	sed -e s/'#VERSION#'/$IBS_KERNMINIVERSION/g $i > "`echo $i | sed -e s/.pat$//`"
	rm $i
done
IBS_BFSRCDIR="`ls -d patterns/boot-floppies-* | cut -d"/" -f2`"
cp -arf ${IBS_BASEDIR}/patterns/$IBS_BFSRCDIR ${IBS_BASEDIR}/build/$IBS_SRCDIR
test_exit
for i in `find ${IBS_BASEDIR}/build/$IBS_BFSRCDIR -name "*.pat" -print`
do
	sed -e s/'#VERSION#'/$IBS_KERNMINIVERSION/g $i > "`echo $i | sed -e s/.pat$//`"
	rm $i
done

# Expand and prepare kernel sources
cd ${IBS_BASEDIR}/build/${IBS_KERNSRCDDIR}
rm -rf linux
tar zxf ${IBS_BASEDIR}/linux-2.?.tar.gz
mv linux-2.? linux
test_exit
cd $IBS_BASEDIR
echo "EXTRAVERSION = $IBS_KEXTRAVERSION" > ${IBS_BASEDIR}/build/${IBS_KERNSRCDDIR}/linux-EXTRAVERSION
sed -e s/"#KVERSION#"/$IBS_KVERSION/ -e s/"#KPATCHLEVEL#"/$IBS_KPATCHLEVEL/ -e s/"#KSUBLEVEL#"/$IBS_KSUBLEVEL/ -e s/"#KEXTRAVERSION#"/$IBS_KEXTRAVERSION/ ${IBS_BASEDIR}/patterns/Makefile.patch.pat > ${IBS_BASEDIR}/patches/Makefile.patch
patch -s ${IBS_BASEDIR}/build/${IBS_KERNSRCDDIR}/linux/Makefile ${IBS_BASEDIR}/patches/Makefile.patch
test_exit
rm -f ${IBS_BASEDIR}/build/${IBS_KERNSRCDDIR}/linux/.version ${IBS_BASEDIR}/build/${IBS_KERNSRCDDIR}/linux/arch/parisc/debian-configs/*udeb
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

echo -e "\nKernel $IBS_KERNISOVERSION is prepared.\n" | tee -a $IBS_BUILDLOG

# Actually building the kernel stuff (now that the tree is ready)
echo -n "Building the kernel packages... " | tee -a $IBS_BUILDLOG
cd ${IBS_BASEDIR}/build/${IBS_KERNSRCDDIR}
dpkg-buildpackage >> $IBS_BUILDLOG 2>&1
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

# Preparing for b-f build
if [ $IBS_BACKUP == "YES" ]; then
	IBS_BKPDIR=$IBS_BASEDIR/backup/archive-$IBS_MINIDATE-$IBS_MINIHOUR
	echo -n "Archiving previous kernel .deb to $IBS_BKPDIR... " | tee -a $IBS_BUILDLOG
	mkdir -p $IBS_BKPDIR
	mv $IBS_DEBARCHIVE/local/kernel*.deb $IBS_BKPDIR/
	test_exit
	echo "Done." | tee -a $IBS_BUILDLOG
else
	rm -f ${IBS_DEBARCHIVE}/local/kernel-image-*.deb
	echo "NOT archiving previous kernel .deb" | tee -a $IBS_BUILDLOG
fi
echo -n "Setting up kernel .deb for boot-floppies build... " | tee -a $IBS_BUILDLOG
mv ${IBS_BASEDIR}/build/kernel-image-*-32* $IBS_DEBARCHIVE/local/
mv ${IBS_BASEDIR}/build/kernel-image-*-64* $IBS_DEBARCHIVE/local/
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

# The b-f stuff (same as kernel: assumed up to date b-f)
echo -n "Building the boot-floppies... " | tee -a $IBS_BUILDLOG
cd ${IBS_BASEDIR}/build/$IBS_BFSRCDIR
test_exit
IBS_BFVERSION=`basename $PWD | cut -c 15-`
dpkg-buildpackage -B -m"$IBS_BFNONMAINTAINER" >> $IBS_BUILDLOG 2>&1
test_exit
cd release
./bf-archive-install_*_hppa.sh
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

# Preparing the ISO tree
echo -n "Upgrading the ISO tree... " | tee -a $IBS_BUILDLOG
echo "Debian GNU/Linux 3.0 prerelease Woody - Hewlett Packard hppa-net ($IBS_MINIDATE)" > $IBS_ISOFOLDER/.disk/info
cd $IBS_ISOFOLDER/dists/woody/main/disks-hppa/
rm -rf *
IBS_BFISONAME=$IBS_BFVERSION-$IBS_BFDATE
mv ${IBS_BASEDIR}/build/${IBS_BFSRCDIR}/release $IBS_BFISONAME
test_exit
ln -s $IBS_BFISONAME current
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

# Actually creating the ISO
cd $IBS_BASEDIR
IBS_ISONAME=palinux-$IBS_KERNISOVERSION-$IBS_MINIDATE-netinst.iso
echo -n "Making the ISO: $IBS_ISONAME... " | tee -a $IBS_BUILDLOG
mkisofs -r -T -J -o $IBS_ISONAME $IBS_ISOFOLDER >> $IBS_BUILDLOG 2>&1
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

# Releasing the lifimage
cd $IBS_BASEDIR
IBS_LIFINAME=lifimage-$IBS_KERNISOVERSION-$IBS_MINIDATE.gz
echo -n "Releasing the lifimage: $IBS_LIFINAME... " | tee -a $IBS_BUILDLOG
tar zOxf ${IBS_BASEDIR}/build/bf-images_*_hppa.tar.gz lifimage | gzip -c >> $IBS_LIFINAME
echo "Done." | tee -a $IBS_BUILDLOG

# The final step: PALO
echo -n "Doing PALO on the ISO... " | tee -a $IBS_BUILDLOG
palo --commandline="0/vmlinux ramdisk_size=8192 root=/dev/ram initrd=0/ramdisk" --recoverykernel=$IBS_ISOFOLDER/dists/woody/main/disks-hppa/current/32/linux.bin --recoverykernel=$IBS_ISOFOLDER/dists/woody/main/disks-hppa/current/64/linux.bin --bootloader=$IBS_ISOFOLDER/boot/iplboot --ramdisk=$IBS_ISOFOLDER/dists/woody/main/disks-hppa/current/root.bin --init-cdrom=$IBS_ISONAME --configfile=/dev/null >> $IBS_BUILDLOG
test_exit
echo "Done." | tee -a $IBS_BUILDLOG

# Cleaning up
echo -n "Cleaning up... " | tee -a $IBS_BUILDLOG
cd $IBS_BASEDIR
rm -rf build
echo "Done."

echo -e "\a\nYour ISO/lifimage are ready !"

exit 0

