#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Program to create GParted live, based on Debian Live helper.

# load config files
. /live-hook-dir/drbl.conf
. /live-hook-dir/ocs-live.conf
# load functions
. /live-hook-dir/ocs-live-hook-functions 

#
locale_to_keep="C.UTF-8 en_US.UTF-8"

# create a dummy /etc/fstab in Live CD so that nis/yp won't complain
touch /etc/fstab

# Before localepurge, save gparted locales. We will put it back in the end (after any apt action)
echo "Backup gparted locales..."
( cd /usr/share/locale 
  find -iname "gparted*" -print | xargs tar -cf /gparted-locale.tar
)
# clean some locales
set_localepurge
localepurge

# append the module so that it will be loaded, then gparted can grow filesystem
# Ref: http://gparted.sourceforge.net/features.php
append_mod_in_etc_modules

# set root passwd, I do not like root without passwd.
set_root_passwd

# We have to create account casper, otherwise there is some problem. See ocs-live-hook-functions for more details.
create_account_casper

# If mlterm is found with xfce installed, set default x-terminal-emulator as mlterm. Since xterm is not as good as mlterm in Traditional Chinese font.
if dpkg -L mlterm &>/dev/null; then
  echo "Set mlterm as default x-terminal-emulator."
  update-alternatives --set x-terminal-emulator /usr/bin/mlterm
  # change the fg/bg color
  perl -pi -e "s/^.*fg_color=.*/fg_color=white/g" /etc/mlterm/main
  perl -pi -e "s/^.*bg_color=.*/bg_color=black/g" /etc/mlterm/main
fi

# since ssh services is on, and casper is known for the whole world, we have to block it.
block_all_clients_by_tcpwrapper

# put some desktop icons in the account casper.
auto_login_id="casper"
auto_login_id_home="/home/casper"

# fluxbox menu, icons and others...
mkdir -p $auto_login_id_home/
cp -af /live-hook-dir/gparted/fluxbox/menu /etc/X11/fluxbox/fluxbox.menu-user
cp -af /live-hook-dir/gparted/pixmaps /usr/share/pixmaps/gparted-live
cp -af /live-hook-dir/gparted/bin/* /usr/bin/
cp -af /live-hook-dir/gparted/resize-windows.txt /root/
cp -af /live-hook-dir/gparted/ideskrc $auto_login_id_home/.ideskrc
cp -af /live-hook-dir/gparted/idesktop $auto_login_id_home/.idesktop
# //NOTE// We can not just put apps into ~casper/.fluxbox, otherwise some other files won't be put by startfluxbox when fluxbox is run first time. Therefore we put apps in home dir, then after fluxbox is started, put it into .fluxbox/
cp -af /live-hook-dir/gparted/fluxbox/apps $auto_login_id_home/
  cat <<-XINITRC_END >> $auto_login_id_home/.xinitrc
#!/bin/bash
exec fluxbox &
action=""
while [ -z "\$action" ]; do
  if [ -d "\$HOME/.fluxbox" ]; then
    mv -f ~/apps \$HOME/.fluxbox/
    action="done"
  else
    echo "Waiting for \$HOME/.fluxbox to be created..."
    sleep 0.1
  fi
done
idesk &
gparted &
wait \$wmpid
XINITRC_END
# 
  cat <<-PROFILE_END >> $auto_login_id_home/.bash_profile
# Added by GParted live
# Start X in tty1 only
sudo [ "\$(tty)" = "/dev/tty1" ] && sudo startx
PROFILE_END

chown -R $auto_login_id.$auto_login_id $auto_login_id_home

# Prepare unifont for bterm
if [ -e /live-hook-dir/unifont.bgf ]; then
  mkdir -p /usr/share/unifont/
  cp -f /live-hook-dir/unifont.bgf /usr/share/unifont/
fi
# Put a service to config keyboard
cat <<-KBD_END >> /etc/rcS.d/S98kbd-conf
#!/bin/bash
dpkg-reconfigure console-data
KBD_END

chmod 755 /etc/rcS.d/S98kbd-conf

# Put a service to config language
cp -f /live-hook-dir/gparted/bin/gl-choose-lang /etc/rcS.d/S99lang-conf

# we need real /sbin/start-stop-daemon
remove_cdebootstrap-helper-diverts

# Dirty hacking: rm files
echo "Starting dirty hacking to remove files..."
unnecessary_packages="libc6-dev ncpfs libpam-ncp binutils dpkg-dev linux-libc-dev make patch wget alsa bzip2 xserver-xorg-input-wacom whiptail"
echo "Force to remove some packages ($unnecessary_packages) if installed..."
for i in $unnecessary_packages; do
 if dpkg -L $i &>/dev/null; then	   
   echo "Force to removing $i..."
   apt-get --yes --force-yes --purge remove $i
 fi
done

#
kernel_ver="$(unalias ls 2>/dev/null; ls /lib/modules/)"
# List for the file or dir to be removed:
# /lib/modules/$kernel_ver/kernel/
kdir_2_be_rm="lib net sound" 
# /lib/modules/$kernel_ver/kernel/drivers/
drv_2_be_rm="acpi atm auxdisplay clocksource connector cpufreq firewire edac mfd misc telephony isdn bluetooth infiniband isdn media net leds kvm spi w1" 
# /lib/modules/$kernel_ver/kernel/fs/
fs_2_be_rm="9p afs dlm smbfs cifs coda autofs* minix gfs* nfs* quota* exportfs lockd romfs ncpfs ocfs* udf" 
# /usr/share/fonts/X11/misc/
font_2_be_rm="*ja* *ko* *kan* *gb*"
# /usr/share/
share_dir_2_be_rm="info man locale doc-base doc"
# Remove them
for i in $kdir_2_be_rm; do
  rm -rf /lib/modules/$kernel_ver/kernel/$i
done
for i in $drv_2_be_rm; do
  rm -rf /lib/modules/$kernel_ver/kernel/drivers/$i
done
for i in $fs_2_be_rm; do
  rm -rf /lib/modules/$kernel_ver/kernel/fs/$i
done
for i in $font_2_be_rm; do
  rm -f /usr/share/fonts/X11/misc/$i
done
# Keep the parameters.txt, avoid it to be removed later.
# live-initramfs want to copy parameters.txt to live cd
[ -e /usr/share/doc/live-initramfs/parameters.txt ] && {
  cp -p /usr/share/doc/live-initramfs/parameters.txt /
}
for i in $share_dir_2_be_rm; do
  rm -rf /usr/share/$i/*
done
# put it back
[ -e /parameters.txt ] && {
  mkdir -p /usr/share/doc/live-initramfs/
  mv -f /parameters.txt /usr/share/doc/live-initramfs/
}

#
dist="$(cat /etc/debian_version)"
if [ "$dist" = "4.0" ]; then
  # etch
  sed -i 's/libxrender1, gksu/libxrender1/' /var/lib/dpkg/status
else
  # lenny
  sed -i 's/Depends: gksu, libatk1.0-0/Depends: libatk1.0-0/' /var/lib/dpkg/status
fi
echo "gparted hold" | dpkg --set-selections
#apt-get remove --purge --yes --force-yes gksu libgksu2-0

#
if [ -n "$(LANG=C apt-get --help 2>&1 | grep autoremove)" ]; then
  apt-get -y autoremove
fi

# Just clean them
# Log
find /var/log/ -type f -exec rm {} \;
# CPP
find /usr/lib/gcc/ -name "cc1" -exec rm {} \;
find /usr/bin/ -name "cpp*" -exec rm {} \;
if [ -e /usr/share/zoneinfo/UTC ]; then
  rm -f /etc/localtime # The original is a soft link file to UTC
  cp -af /usr/share/zoneinfo/UTC /etc/localtime
  rm -rf /usr/share/zoneinfo/*
fi
[ -d /usr/lib/gconv/ ] && rm -rf /usr/lib/gconv/*
[ -d /var/backups ] && rm -rf /var/backups/*
# gconf2* gconf2-common* gksu* gnome-keyring* libgconf2-4* libgksu2-0* libgnome-keyring0 python* python-minimal*
pkg_force_2_be_rm="python2.4-minimal python2.4 gksu gksudo gnome-keyring gnome-keyring-manager libgnome-keyring0 gconf2-common gconf2 libgconf2-4 libgksu2-0 libgnome-keyring0"

# ///Note/// This should be the last one after any apt-get.
# clean unnecessary backup file to save space
clean_unnecessary_backup_file_in_boot
# prevent "W: Couldn't stat source package list http://..." error message
perl -pi -e "s/^/#/" /etc/apt/sources.list
apt-get update

if dpkg -L deborphan &>/dev/null; then	   
   echo "Removing deborphan and gettext-base..."
   apt-get --yes --force-yes --purge remove deborphan gettext-base
fi

packages=$(LANG=C dpkg --list | grep -E "^rc" | awk '{print $2}')
[ -n "$packages" ] && dpkg --purge $packages

for i in $pkg_force_2_be_rm; do
  dpkg -L $i 2>/dev/null | xargs -I % bash -c "test -f % && rm -f %"
done

# Create a list of all packages:
dpkg --get-selections > /root/gparted-livecd-packages.txt

# Restore gparted locales 
echo "Restoring gparted locales..."
( cd /usr/share/locale
  tar -xf /gparted-locale.tar
  rm -f /gparted-locale.tar
)
# After this, even if live helper to use apt to install squashfs and memtest, although the gparted locales will be removed, but they are not in the directory that will be packed by mksquashfs. Therefore, we can keep those gparted locales in filesystem.squashfs.

### THE END ###
# DO NOT PUT ANY SCRIPT AFTHER THIS!!!
# kill this program before creating squashfs filesystem.
rm -rf /live-hook-dir
