#!/bin/sh
#(c) Copyright Barry Kauler, March 14, 2014. License GPL v3 (/usr/share/doc/legal).
#build quirky as a live-cd, running in ram.
#140314 first version.
#150211 create uncompressed sfs file in initrd.cpio. humongous kernel is xz-compressed.

. ./DISTRO_SPECS
RIGHTVER="$DISTRO_VERSION"

#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 "Please install the kernel source PET."
 exit
fi

DOTCONFIG="`find sandbox3/rootfs-complete/etc/modules -maxdepth 1 -type f -name 'DOTconfig*' | head -n 1`"

PREBUILD='yes'
[ "$DOTCONFIG" = "" ] && 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

sed -i -e 's%^CONFIG_INITRAMFS_SOURCE=.*%CONFIG_INITRAMFS_SOURCE="initrd.cpio"%' $DOTCONFIG

#150211 this is what i want:
# # CONFIG_KERNEL_GZIP is not set
# # CONFIG_KERNEL_BZIP2 is not set
# # CONFIG_KERNEL_LZMA is not set
# CONFIG_KERNEL_XZ=y
# # CONFIG_KERNEL_LZO is not set
# # CONFIG_KERNEL_LZ4 is not set
#...initrd.cpio is not compressed, is inside kernel. slow bootup, but small .iso
sed -i -e 's%^CONFIG_KERNEL_GZIP=.*%# CONFIG_KERNEL_GZIP is not set%' $DOTCONFIG
sed -i -e 's%^CONFIG_KERNEL_BZIP2=.*%# CONFIG_KERNEL_BZIP2 is not set%' $DOTCONFIG
sed -i -e 's%^CONFIG_KERNEL_LZMA=.*%# CONFIG_KERNEL_LZMA is not set%' $DOTCONFIG
sed -i -e 's%^CONFIG_KERNEL_XZ=.*%# CONFIG_KERNEL_XZ is not set%' $DOTCONFIG
sed -i -e 's%^CONFIG_KERNEL_LZO=.*%# CONFIG_KERNEL_LZO is not set%' $DOTCONFIG
sed -i -e 's%^CONFIG_KERNEL_LZ4=.*%# CONFIG_KERNEL_LZ4 is not set%' $DOTCONFIG
sed -i -e 's%^# CONFIG_KERNEL_XZ .*%CONFIG_KERNEL_XZ=y%' $DOTCONFIG

if [ ! -e /usr/bin/gcc ];then
 echo
 echo "The 'devx' SFS file must be loaded. 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 -n "Type ENTER to continue (CTRL-C to quit): "
read keepgoing

#fix f.s. (as in service_pack's)...
PRE="sandbox3/rootfs-complete"
PART2FS="ext2"
tPTN1="s%.*MARK_DEV1FS_LINE.*%DEV1FS='${PART2FS}' #MARK_DEV1FS_LINE %"
sed -i -e "$tPTN1" ${PRE}/sbin/init
sed -i -e "$tPTN1" ${PRE}/etc/rc.d/rc.sysinit

#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 boot/initrd-tree sandbox4/
sync
cd sandbox4
cd initrd-tree
#150211 create uncompressed sfs file...
#mksquashfs ../../sandbox3/rootfs-complete ./q.sfs -comp xz
mksquashfs ../../sandbox3/rootfs-complete ./q.sfs -noI -noD -noF -noX -no-exports
sync
#now put it together again...
rm -f ../initrd.cpio 2> /dev/null
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
cp -a ../sandbox3/build/* build/
mv -f build/syslinux.cfg build/isolinux.cfg
sed -i -e '/^append/d' build/isolinux.cfg
cp -f /usr/src/linux-${KERNVER}/arch/x86/boot/bzImage build/vmlinuz
[ -f build/README.HTM ] && rm -f build/README.HTM
if [ -f /usr/lib/syslinux/isolinux.bin ];then #150211
 cp -f /usr/lib/syslinux/isolinux.bin build/
else
 cp -f /usr/share/syslinux/isolinux.bin build/
fi
sync

ISONAME="${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.iso"
#rm -f ./${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.iso 2> /dev/null
$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

