#!/bin/bash
#
# run this script to create a LiveCD in /tmp/live_data_1234
#
# Author: Tomas M. <http://www.linux-live.org>

export PATH=.:./tools:../tools:/usr/sbin:/usr/bin:/sbin:/bin:/

CHANGEDIR="`dirname \`readlink -f $0\``"
echo "Changing current directory to $CHANGEDIR"
cd $CHANGEDIR

. liblinuxlive || exit 1
. ./.config || exit 1

# only root can continue, because only root can read all files from your system
allow_only_root

VMKLIVEDIR="/root/.vmklive"
HOMEDIRSIZE=`du -s /home | awk '{print $1}'`
BINDIRSIZE=`du -s /bin | awk '{print $1}'`
DEVDIRSIZE=`du -s /dev | awk '{print $1}'`
ETCDIRSIZE=`du -s /etc | awk '{print $1}'`
LIBDIRSIZE=`du -s /lib | awk '{print $1}'`
OPTDIRSIZE=`du -s /opt | awk '{print $1}'`
ROOTDIRSIZE=`du -s /root | awk '{print $1}'`
SBINDIRSIZE=`du -s /sbin | awk '{print $1}'`
USRDIRSIZE=`du -s /usr | awk '{print $1}'`
VARDIRSIZE=`du -s /var | awk '{print $1}'`
TOTALDIRSIZE=`expr $HOMEDIRSIZE + $BINDIRSIZE + $DEVDIRSIZE + $ETCDIRSIZE + $LIBDIRSIZE + $OPTDIRSIZE + $ROOTDIRSIZE + $SBINDIRSIZE + $USRDIRSIZE + $VARDIRSIZE`
echo "size=\"$TOTALDIRSIZE\"" > $VMKLIVEDIR/installsize
sleep 3

ln -sf /etc/rc.d/rc.S.live /etc/rc.d/rc.S
ln -sf /etc/rc.d/rc.K.live /etc/rc.d/rc.K
ln -sf /etc/rc.d/rc.M.live /etc/rc.d/rc.M
ln -sf /etc/rc.d/rc.local.live /etc/rc.d/rc.local
mv /etc/fstab $VMKLIVEDIR
mv /etc/mtab $VMKLIVEDIR
mv /etc/X11/xorg.conf $VMKLIVEDIR
#ln -sf /etc/X11/xorg.conf-vesa /etc/X11/xorg.conf
#cp $VMKLIVEDIR/vmklive /sbin
#cp $VMKLIVEDIR/vliveinstall /sbin
cp $VMKLIVEDIR/*.desktop /root/Desktop/
cp $VMKLIVEDIR/*.desktop /usr/share/applications/
cp $VMKLIVEDIR/*.desktop /etc/skel/Desktop/
ls /home | grep -v backupsys | grep -v ftp | grep -v tmp >$VMKLIVEDIR/hometmp.$$

. ./install

# search for kernel
VMLINUZ=$ROOT/boot/vmlinuz-$(uname -r)
#if [ -L "$VMLINUZ" ]; then VMLINUZ=`readlink -f $VMLINUZ`; fi
#echo -ne "Enter path for the kernel you'd like to use [hit enter for $VMLINUZ]: "
#read NEWKERNEL
#if [ "$NEWKERNEL" != "" ]; then VMLINUZ="$NEWKERNEL"; fi
if [ "`ls $VMLINUZ 2>>$DEBUG`" = "" ]; then echo "cannot find $VMLINUZ"; exit 1; fi

header "Creating LiveCD from your Linux"
echo "some debug information can be found in $DEBUG"

mkdir -p $CDDATA/$LIVECDNAME/{base,modules,optional,rootcopy,tools}

echo "copying cd-root to $CDDATA, using kernel from $VMLINUZ"
cp -R cd-root/boot $CDDATA
cp -R cd-root/veclinux $CDDATA
for i in isolinux syslinux; do
   cat cd-root/boot/$i/$i.cfg | sed -r "s/LABEL linux/LABEL $LIVECDNAME/" | sed -r "s/DEFAULT linux/DEFAULT $LIVECDNAME/" > $CDDATA/boot/$i/$i.cfg
done
mv $CDDATA/boot/dos/linux.bat $CDDATA/boot/dos/${LIVECDNAME:0:8}.bat
cat cd-root/boot/dos/readme.txt | sed -r "s/LINUX.BAT/"${LIVECDNAME:0:8}.bat"/" > $CDDATA/boot/dos/readme.txt

mkdir -p $CDDATA/$LIVECDNAME
cp -R cd-root/linux/* $CDDATA/$LIVECDNAME
cp tools/* $CDDATA/$LIVECDNAME/tools
cp -R DOC/* $CDDATA/$LIVECDNAME
cp $VMLINUZ $CDDATA/boot/vmlinuz
( cd $CDDATA/boot/ && ln -sf vmlinuz vmlinuz-$(uname -r) )

echo "creating initrd image..."
echo "Using kernel modules from $ROOT/$LMK"
cd initrd
./initrd_create $LIVECDNAME
if [ "$?" -ne 0 ]; then exit; fi
cd ..

cp initrd/initrd.xz $CDDATA/boot/initrd.xz
rm initrd/initrd.xz
#gunzip $CDDATA/boot/initrd.gz
#splash -s -f /etc/bootsplash/themes/current/config/bootsplash-1024x768.cfg >>$CDDATA/boot/initrd.gz
#splash -s -f /etc/bootsplash/themes/current/config/bootsplash-800x600.cfg >>$CDDATA/boot/initrd.gz
#splash -s -f /etc/bootsplash/themes/current/config/bootsplash-640x480.cfg >>$CDDATA/boot/initrd.gz

splash -s -f /etc/bootsplash/themes/current/config/bootsplash-1024x768.cfg >$CDDATA/boot/initrd.real
splash -s -f /etc/bootsplash/themes/current/config/bootsplash-800x600.cfg >>$CDDATA/boot/initrd.real
splash -s -f /etc/bootsplash/themes/current/config/bootsplash-640x480.cfg >>$CDDATA/boot/initrd.real

echo "creating compressed images..."
#for dir in $MKMOD; do
#    if [ -d $ROOT/$dir ]; then
#      echo "base/$dir.xzm ..."
#      create_module $ROOT/$dir $CDDATA/$LIVECDNAME/base/$dir.xzm -keep-as-directory
#      if [ $? -ne 0 ]; then exit; fi
#      echo
#    fi
#done
for dir in $MKMOD; do
    if [ -d $ROOT/$dir ]; then
      echo "base/$dir.xzm ..."
      echo -ne > exclude.txt
      for i in $EXCLUDE; do
         part=$(echo "x/$i" | tr -s / | sed -r "s:x/[^/]+/::")
         if [ -e "$ROOT/$dir/$part" ]; then echo "$ROOT/$dir/$part" >> exclude.txt; fi
      done
      cat exclude.txt
      create_module $ROOT/$dir $CDDATA/$LIVECDNAME/base/$dir.xzm -keep-as-directory -ef exclude.txt
      if [ $? -ne 0 ]; then exit; fi
      rm exclude.txt
      echo
    fi
done

#set the version
REL=$(cat /etc/vector-version|cut -d' ' -f1-3)
sed -i "s/VERSION/$REL LIVE/g" $CDDATA/veclinux/VINSTALL.INI
sed -i "s/KERNEL/$kern_ver/g" $CDDATA/veclinux/VINSTALL.INI
sed -i "s/DATE/$(date +%b-%d-%Y)/g" $CDDATA/veclinux/VINSTALL.INI
sed -i "s/RELID/$(date +%m%d%Y)/g" $CDDATA/veclinux/VINSTALL.INI

cd $CDDATA/$LIVECDNAME
./make_iso.sh ../../$ISONAME.iso
echo "Creating md5sum now"
cd /tmp
md5sum $ISONAME.iso > $ISONAME.md5sum

ln -sf /etc/rc.d/rc.S.real /etc/rc.d/rc.S
ln -sf /etc/rc.d/rc.K.real /etc/rc.d/rc.K
ln -sf /etc/rc.d/rc.M.real /etc/rc.d/rc.M
ln -sf /etc/rc.d/rc.local.real /etc/rc.d/rc.local
mv $VMKLIVEDIR/fstab /etc/
mv $VMKLIVEDIR/mtab /etc/
mv $VMKLIVEDIR/xorg.conf /etc/X11/

echo "--------done----------"
