#!/bin/bash

echo "Should I download kernel 2.6.13 for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download kernel"
   echo "*************************************************************"
   cd /usr/src
   wget -P/usr/src http://download.primat.ch/projekte/debian-livecd/linux-2.6.13.2.tar.bz2
#   wget -P/usr/src http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.13.2.tar.bz2
   tar xfvj /usr/src/linux-2.6.13.2.tar.bz2
   rm linux-2.6.13.2.tar.bz2
   ln -s /usr/src/linux-2.6.13.2 /usr/src/linux
fi

echo "#############################################################"
echo "Should I download the kernel configuration for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download kernel config"
   echo "*************************************************************"
   cd /usr/src/linux/
   wget -P/usr/src/linux/ http://download.primat.ch/projekte/debian-livecd/config-2.6.13.2v2
   mv config-2.6.13.2v2 .config
fi

echo "#############################################################"
echo "Should I download squashfs for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download squashfs"
   echo "*************************************************************"
   cd /usr/src/linux/
   wget -P/usr/src/linux/ http://download.primat.ch/projekte/debian-livecd/squashfs2.2-r2.tar.gz
   tar xfvz squashfs2.2-r2.tar.gz
fi

echo "#############################################################"
echo "Should I patch the kernel for squashfs? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* patch kernel"
   echo "*************************************************************"
   cd /usr/src/linux/
   patch -p1 < squashfs2.2-r2/linux-2.6.13/squashfs2.2-patch
   cd /usr/src/linux/squashfs2.2-r2/squashfs-tools
   make
   cp mksquashfs /usr/sbin
fi

echo "#############################################################"
echo "Would you like to configure your kernel? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* configure kernel"
   echo "*************************************************************"
   cd /usr/src/linux
   make menuconfig
fi

echo "#############################################################"
echo "Should I compile your kernel? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* compile kernel"
   echo "*************************************************************"
   cd /usr/src/linux
   make bzImage modules modules_install
fi

echo "#############################################################"
echo "Should I download unionfs for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download unionfs"
   echo "*************************************************************"
   cd /usr/src/
   wget -P/usr/src http://download.primat.ch/projekte/debian-livecd/unionfs-1.0.14.tar.gz
   tar xfvz unionfs-1.0.14.tar.gz
fi

echo "#############################################################"
echo "Should I install unionfs for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* install unionfs"
   echo "*************************************************************"
   cd /usr/src/unionfs-1.0.14
   touch fistdev.mk
   echo 'TOPINC=-I/usr/src/linux/include' > fistdev.mk
   make -C /lib/modules/2.6.13.2/build SUBDIRS=/usr/src/unionfs-1.0.14 FISTDEVMK=/usr/src/unionfs-1.0.14/fistdev.mk modules
   cp unionfs.ko /lib/modules/2.6.13.2/kernel/fs/
   echo '/lib/modules/2.6.13.2/kernel/fs/unionfs.ko:' >> /lib/modules/2.6.13.2/modules.dep
fi

echo "#############################################################"
echo "Should I install the kernel for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* install kernel"
   echo "*************************************************************"
   mkdir -p /boot/isolinux
   cd /boot/isolinux
   cp /usr/src/linux/arch/i386/boot/bzImage /boot/isolinux/vmlinuz
fi

echo "#############################################################"
echo "Should I download isolinux? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download isolinux"
   echo "*************************************************************"
   cd /boot/isolinux
   wget -P/boot/isolinux http://download.primat.ch/projekte/debian-livecd/syslinux-3.11.tar.bz2
   tar xfvj syslinux-3.11.tar.bz2
   cp syslinux-3.11/isolinux.bin /boot/isolinux/
   rm -rf syslinux-3.11
   rm syslinux-3.11.tar.bz2
fi

echo "#############################################################"
echo "Should I download the isolinux configuration? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download isolinux config"
   echo "*************************************************************"
   cd /boot/isolinux
   wget -P/boot/isolinux/ http://download.primat.ch/projekte/debian-livecd/menu.txt
   wget -P/boot/isolinux/ http://download.primat.ch/projekte/debian-livecd/isolinux.cfg
fi

echo "#############################################################"
echo "Should I now create the initrd? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* create initrd"
   echo "*************************************************************"
   touch /boot/isolinux/initrd
   dd if=/dev/zero of=/boot/isolinux/initrd bs=1024k count=16
   mke2fs /boot/isolinux/initrd
   cd /mnt
   mkdir -p /mnt/initrd
   mount -o loop /boot/isolinux/initrd /mnt/initrd
   cd /mnt/initrd
   mkdir -p etc dev lib bin proc new cdrom
   touch etc/mtab
   touch etc/fstab
fi

echo "#############################################################"
echo "Should I download the initrc script? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* download initrc"
   echo "*************************************************************"
   cd /mnt/initrd
   wget -P/mnt/initrd/ http://download.primat.ch/projekte/debian-livecd/linuxrc
   chmod +x /mnt/initrd/linuxrc
fi

echo "#############################################################"
echo "Should I complete initrc configuration? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* complete initrc"
   echo "*************************************************************"
   cp /bin/cat /mnt/initrd/bin/
   cp /usr/sbin/chroot /mnt/initrd/bin/
   cp /sbin/init /mnt/initrd/bin/
   cp /bin/ls /mnt/initrd/bin/
   cp /bin/mkdir /mnt/initrd/bin
   cp /sbin/modprobe /mnt/initrd/bin
   cp /bin/mount /mnt/initrd/bin
   cp /sbin/pivot_root /mnt/initrd/bin
   cp /bin/sh /mnt/initrd/bin
   cp /bin/tar /mnt/initrd/bin
   cp /bin/umount /mnt/initrd/bin
   cp /lib/ld-linux.so.2 /mnt/initrd/lib/
   cp /lib/libacl.so.1 /mnt/initrd/lib/
   cp /lib/libattr.so.1 /mnt/initrd/lib/
   cp /lib/libblkid.so.1 /mnt/initrd/lib/
   cp /lib/libc.so.6 /mnt/initrd/lib/
   cp /lib/libdl.so.2 /mnt/initrd/lib/
   cp /lib/libncurses.so.5 /mnt/initrd/lib/
   cp /lib/libpthread.so.0 /mnt/initrd/lib/
   cp /lib/librt.so.1 /mnt/initrd/lib/
   cp /lib/libuuid.so.1 /mnt/initrd/lib/
   mkdir -p /mnt/initrd/lib/modules/2.6.13.2
   cd /mnt/initrd/lib/modules/2.6.13.2
   cp /lib/modules/2.6.13.2/modules.dep .
   mkdir -p kernel
   cd kernel
   mkdir -p fs
   cd fs
   cp /lib/modules/2.6.13.2/kernel/fs/unionfs.ko .
fi

echo "#############################################################"
echo "Should I create the device files for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* create devices"
   echo "*************************************************************"
   mknod /mnt/initrd/dev/console c 5 1
   mknod /mnt/initrd/dev/null c 1 3
   mknod /mnt/initrd/dev/hda b 3 0
   mknod /mnt/initrd/dev/hdb b 3 64
   mknod /mnt/initrd/dev/hdc b 22 0
   mknod /mnt/initrd/dev/hdd b 22 64
   mknod /mnt/initrd/dev/tty c 4 0
   mknod /mnt/initrd/dev/loop0 b 7 0
fi

echo "#############################################################"
echo "Should I unmount initrd for you? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* umount initrd"
   echo "*************************************************************"
   umount /mnt/initrd/
fi

echo "#############################################################"
echo "Should I create the mountpoints? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* create mountpoints"
   echo "*************************************************************"
   mkdir -p /new/
   mkdir -p /ramdisk
   mkdir -p /live
fi

echo "#############################################################"
echo "Should I modify the fstab? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* modify fstab"
   echo "*************************************************************"
   mv /etc/fstab /etc/fstab_hdinst
   cd /etc
   wget -P/etc/ http://download.primat.ch/projekte/debian-livecd/fstab
fi

echo "#############################################################"
echo "Should I install loginmessage? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* install loginmessage"
   echo "*************************************************************"
   cd /etc/
   rm issue
   wget -P/etc/ http://download.primat.ch/projekte/debian-livecd/issue
fi

echo "#############################################################"
echo "Should I create remaster file structure? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* create remaster structure"
   echo "*************************************************************"
   mkdir -p /root/remaster
   cd /root/remaster
   wget -P/root/remaster/ http://download.primat.ch/projekte/debian-livecd/copy_files
   wget -P/root/remaster/ http://download.primat.ch/projekte/debian-livecd/build_livecd
   mkdir -p tmp
   mkdir -p livecd
fi

echo "#############################################################"
echo "Should I clean the installation (apt-get clean)? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* clean"
   echo "*************************************************************"
   cd /usr/src/linux/
   make clean
   apt-get clean
   apt-get autoclean
   umount /proc
fi

echo "#############################################################"
echo "Should I deeply clean the installation (/usr/src/*)? y,n: "
read userinput
if test $userinput = y
then
   echo ""
   echo ""
   echo "*************************************************************"
   echo "* indepth clean"
   echo "*************************************************************"
   cd /usr/src/
   rm -rf *
   cd /usr/src/linux/
   make clean
   apt-get clean
   apt-get autoclean
   umount /proc
fi
