#!/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
. /drbl.conf
. /ocs-live.conf
# load functions
. /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

# 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 /gparted-live-hook-dir/fluxbox/menu /etc/X11/fluxbox/fluxbox.menu-user
cp -af /gparted-live-hook-dir/pixmaps /usr/share/pixmaps/gparted-live
cp -af /gparted-live-hook-dir/bin/* /usr/bin/
cp -af /gparted-live-hook-dir/resize-windows.txt /root/
cp -af /gparted-live-hook-dir/ideskrc $auto_login_id_home/.ideskrc
cp -af /gparted-live-hook-dir/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 /gparted-live-hook-dir/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

# 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
for i in $share_dir_2_be_rm; do
  rm -rf /usr/share/$i/*
done
# 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"
for i in $pkg_force_2_be_rm; do
  dpkg -L $i 2>/dev/null | xargs -I % bash -c "test -f % && rm -f %"
done

# ///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..."
   apt-get --yes --force-yes --purge remove deborphan
fi

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

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