#!/bin/sh
#the script 3builddistro has built it in sandbox3/build
#the quirky build will be in sandbox4.
#100912 changed name of quirky-kernel to 'kernel.qky', appended id-string. simplified puppy filenames.
#110422 DISTRO_VERSION variable now has dotted format. note, also now using full dotted version# in puppy filenames.
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#130226 update.

#. ./DISTRO_SPECS
. ./sandbox3/rootfs-complete/etc/DISTRO_SPECS #100912 want DISTRO_IDSTRING, created by 3builddistro.
#...also has DISTRO_PUPPYSFS, DISTRO_ZDRVSFS, DISTRO_DEVXSFS simplified puppy filenames.
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.

if [ "`echo -n "$DISTRO_VERSION" | grep '\.'`" = "" ];then #110422
 DIGIT1="`echo "$DISTRO_VERSION" | cut -b 1`"
 DIGIT2="`echo "$DISTRO_VERSION" | cut -b 2`"
 DIGIT3="`echo "$DISTRO_VERSION" | cut -b 3`"
 RIGHTVER="${DIGIT1}.${DIGIT2}.${DIGIT3}"
 [ "$DIGIT3" = "0" ] && RIGHTVER="${DIGIT1}.${DIGIT2}"
else
 RIGHTVER="$DISTRO_VERSION"
fi

#use xorriso if it is installed (see also functions4puppy)...
CDRECORD='cdrecord'
MKISOFS='mkisofs'
if [ "`which xorriso`" != "" ];then
 CDRECORD='xorrecord'
 MKISOFS='xorrisofs' #growisofs reads this variable.
fi
export CDRECORD MKISOFS


#what kernel was chosen?...
KERNVER="`find sandbox3/rootfs-complete/lib/modules -maxdepth 1 -type d -name '[23]*' | head -n 1 | rev | cut -f 1 -d '/' | rev`"
if [ ! -f /usr/src/linux-${KERNVER}/Kbuild ];then
 echo
 echo "Linux kernel source version ${KERNVER} is required."
 echo "If you are running from live-CD or frugal install, download the"
 echo "source SFS file from puppylinux.com/sources, choose it in the"
 echo "BootManager, then reboot. Quiting script..."
 exit
fi

#it must have 'INITRD' in the filename... 130226 no
#DOTCONFIG="`find sandbox3/rootfs-complete/etc/modules -maxdepth 1 -type f -name "DOTconfig*" | grep 'INITRD'`"
DOTCONFIG="`find sandbox3/rootfs-complete/etc/modules -maxdepth 1 -type f -name 'DOTconfig*' | head -n 1`"

#130226 bung this in...
sed -i -e 's%^CONFIG_INITRAMFS_SOURCE=.*%CONFIG_INITRAMFS_SOURCE="initrd.cpio"%' $DOTCONFIG

PREBUILD='yes'
[ "$DOTCONFIG" = "" ] && PREBUILD='no'
[ ! -f sandbox3/build/initrd.gz ] && PREBUILD='no'
[ ! -f sandbox3/build/${DISTRO_PUPPYSFS} ] && PREBUILD='no'
if [ "$PREBUILD" = "no" ];then
 echo "The needed files are missing from sandbox3/build"
 echo 'The previous scripts 0* - 3* have to be run first.'
 echo "Quiting script..."
 exit
fi

if [ ! -e /usr/bin/gcc ];then
 echo
 echo "The 'devx' SFS file must be loaded. Quiting script..."
 exit
fi

if [ "`grep 'initrd.cpio' $DOTCONFIG`" = "" ];then
 echo
 echo "Looking in $DOTCONFIG"
 echo "The kernel does not have a correct configuration for building Quirky."
 echo 'It needs this line: CONFIG_INITRAMFS_SOURCE="initrd.cpio"'
 echo "You will need to run through the build again, choosing a correct kernel."
 echo "Quiting script..."
 exit
fi

echo
echo "The Linux kernel has to be compiled, and this requires a lot of space."
echo "The free-space applet in the tray must be showing at least 2.8G free."
echo "If not, and you are running from live-CD or frugal, go to the Utility menu"
echo "and increase the size of the save-file, then reboot. Repeat if necessary."
echo -n "Type ENTER to continue (CTRL-C to quit): "
read keepgoing

#build initrd.cpio...
echo
echo "Building initrd.cpio..."
[ -f /usr/src/linux-${KERNVER}/initrd.cpio ] && mv -f /usr/src/linux-${KERNVER}/initrd.cpio /tmp/
rm -rf sandbox4 2>/dev/null
mkdir sandbox4
cp -a sandbox3/build/initrd.gz sandbox4/
sync
cd sandbox4
gunzip initrd.gz
mkdir initrd-tree
cd initrd-tree/
cat ../initrd | ../../boot/cpio -i -d -m
sync
cp ../../sandbox3/build/${DISTRO_PUPPYSFS} ./
[ -f ../../sandbox3/build/${DISTRO_ZDRVSFS} ] && cp ../../sandbox3/build/${DISTRO_ZDRVSFS} ./
sync
#now put it together again...
rm -f ../initrd.cpio
find . | ../../boot/cpio -o -H newc > ../initrd.cpio
sync
cd ..
#copy it to the kernel source...
cp -f initrd.cpio /usr/src/linux-${KERNVER}/
cd ..
echo "...initrd.cpio should now be at /usr/src/linux-${KERNVER}"
echo -n "Press ENTER to continue: "
read keepgoing

#now recompile the kernel...
echo
echo "The kernel has to be recompiled..."
CURRDIR="`pwd`"
cd /usr/src/linux-${KERNVER}
mv -f .config DOTconfigPREVIOUS 2>/dev/null
make clean
make mrproper
cp -f ${CURRDIR}/${DOTCONFIG} ./.config
echo
echo "A kernel configuration menu will pop up next."
echo 'Just select the <EXIT> button to quit and save configuration.'
echo -n "Press ENTER to bring up menu: "
read keepgoing
make menuconfig
echo
echo "Now compiling kernel..."
make bzImage
sync
cd $CURRDIR
echo
echo "Well, it should have been done!"

echo
echo "Now building live-CD..."
rm -rf sandbox4/build
mkdir sandbox4/build
cd sandbox4

QUIRKYKERNEL="kernel.qky" #100912

cp -a ../sandbox3/build/isolinux.bin build/

case $DISTRO_FILE_PREFIX in #091203
 quirky)
  BOOTLOGO="quirky"
  BOOTLABEL="quirky"
  REMOVELINE1='pfix=copy'
 ;;
 *)
  BOOTLOGO="puppy"
  BOOTLABEL="puppy"
  REMOVELINE1=''
 ;;
esac
blPATTERN="s%BOOTLABEL%${BOOTLABEL}%"
namePATTERN="s/DISTRO_NAME/${DISTRO_NAME}/"
verPATTERN="s/DISTRO_VERSION/${DISTRO_VERSION}/"
prefixPATTERN="s/DISTRO_FILE_PREFIX/${DISTRO_FILE_PREFIX}/"
sed -e "$namePATTERN" -e "$verPATTERN" -e "$prefixPATTERN" -e "$blPATTERN" ../boot/boot-dialog/help.msg > build/help.msg
if [ "$REMOVELINE1" != "" ];then
 grep -v "$REMOVELINE1" build/help.msg > /tmp/3builddistro-help.msg
 mv -f /tmp/3builddistro-help.msg build/help.msg
fi
cp -f ../boot/boot-dialog/boot.msg build/boot.msg
export RGBDEF=/usr/share/X11/rgb.txt
../boot/boot-dialog/textongif.sh ../boot/boot-dialog/${BOOTLOGO}.gif "$RIGHTVER" > logo.gif
../boot/boot-dialog/gif2lss logo.gif > build/logo.16

echo "default BOOTLABEL
display boot.msg
prompt 1
timeout 50

F1 boot.msg
F2 help.msg

label BOOTLABEL
kernel ${QUIRKYKERNEL}
append pmedia=cd" | sed -e "$blPATTERN" > build/isolinux.cfg
sync

cp -f /usr/src/linux-${KERNVER}/arch/x86/boot/bzImage build/${QUIRKYKERNEL}
sync
echo -n "$DISTRO_IDSTRING" >> build/${QUIRKYKERNEL} #16-char id-string appended. see 3builddistro, init.
sync
echo "See big fat kernel here:
sandbox4/build/${QUIRKYKERNEL}"

ISONAME="${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}-quirky.iso"

$MKISOFS -D -R -o ${ISONAME} -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./build/
sync
md5sum ${ISONAME} > ${ISONAME}.md5.txt
echo
echo "${ISONAME} has been created."
echo
echo "Would you like to burn it to a CD? "
echo -n "ENTER only for yes, or any printable char then ENTER not to: "
read writeitnow
if [ "$writeitnow" = "" ];then
 CDR=""
 [ -f /etc/cdburnerdevice ] && CDR="/dev/`cat /etc/cdburnerdevice`"
 [ "$CDR" = "" ] && CDR='/dev/sr0'
 CDDESCR="`probedisk | grep '|optical|' | grep "$CDR" | cut -f 3 -d '|'`"
 echo -n 'Type "y" for multisession, else just ENTER: '
 read BURNMULTI
 if [ "$BURNMULTI" = "y" ];then
  BURNMULTI="-multi -tao -pad"
 else
  BURNMULTI="-dao"
 fi
 echo "Please insert blank CD into $CDR
(which is described as: ${CDDESCR})
-- also be sure that it is unmounted."
 echo -n "Then hit ENTER key: "
 read yayburn
# $CDRECORD $BURNMULTI -data -eject -v speed=4 padsize=300k dev=ATAPI:$CDR ${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso
 $CDRECORD $BURNMULTI -data -eject -v speed=4 padsize=300k dev=$CDR ${ISONAME}
 sync
 eject $CDR
 echo "...done"
fi

###END###
