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

# Function borrowed from drbl-functions because we do not have drbl/clonezilla packages in GParted live env.
is_systemd_init() {
  # Function to test if system is really using systemd by checking if /sbin/init is link to /lib/systemd/systemd.
  local real_init re_code
  if [ -x "/bin/systemctl" -a -d "/lib/systemd/system/" ]; then
    real_init="$(LC_ALL=C stat -c "%N" /sbin/init | awk -F"->" '{print $2}' | sed -r -e "s/^[[:space:]]*//g" -e "s/\`//g" -e "s/'//g")"
    if [ -n "$real_init" ]; then
      if [ "$(LC_ALL=C basename $real_init)" = "systemd" ]; then
        re_code=0
      else
        re_code=1
      fi
    else
      re_code=1
    fi
  else
    re_code=1
  fi
  return $re_code
} # end of is_systemd_init

# Overwrite the locale_to_keep in /live-hook-dir/ocs-live-hook.conf. We just keep C and en_US only. Later we will restore the gparted locales file.
locale_to_keep="C.UTF-8, en_US.UTF-8"

# Set the environment variable LIVE_CREATING_MODE so we know what we are doing
LIVE_CREATING_MODE="gparted"
export LIVE_CREATING_MODE

# Initial flag for updating initramfs
update_initramfs_flag="no"

# LIVE_CREATING_MODE will be used in download_grub_1_2_deb_for_later_use
download_grub_1_2_deb_for_later_use

#
clean_udev_persistent_net_rules

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

# Append /sbin and /usr/sbin in the system PATH 
echo "export PATH=\$PATH:/sbin:/usr/sbin" >> /etc/profile
echo "export PATH=\$PATH:/sbin:/usr/sbin" >> /etc/bash.bashrc

# 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
)

#
install_debian_extra_modules

# Some packages to be removed
for i in $pkg_2_be_removed_for_gparted_live; do
  if LC_ALL=C dpkg -L $i &>/dev/null; then
    apt-get -y --purge remove $i
  fi
done

# clean some locales
set_localepurge
localepurge

# Exclude live mount point in /etc/init.d/umountfs to avoid error messages when rebooting or halting.
exclude_umount_live_mnt_point_in_umountfs

# Turn off some services
for i in $service_2_be_off_for_gparted_live; do
  remove_service_in_system $i
done

# 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 (maybe none)
set_root_passwd

# No more use this. Let live-initramfs deal with that.
# We have to create account user, otherwise there is some problem. See ocs-live-hook-functions for more details.
# preseed_autologin_account

# If lxterminal or 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 lxterminal &>/dev/null; then
  echo "Set lxterminal as default x-terminal-emulator."
  update-alternatives --set x-terminal-emulator /usr/bin/lxterminal
elif 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 autologin account is known for the whole world, we have to block it.
block_all_clients_by_tcpwrapper

# fluxbox menu, icons, exe files and others...
cp -af /live-hook-dir/gparted/fluxbox/menu /etc/X11/fluxbox/fluxbox.menu-user
cp -af /live-hook-dir/gparted/usr /

# put some desktop icons in the root account, when client boot, we will need them.
gparted_desktop_rep="/root/gparted-live/"
mkdir -p $gparted_desktop_rep
cp -af /live-hook-dir/gparted/ideskrc $gparted_desktop_rep/ideskrc
cp -af /live-hook-dir/gparted/idesktop $gparted_desktop_rep/idesktop
# //NOTE// We can not just put apps into ~$autologin_account/.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 $gparted_desktop_rep

# Put start scripts
mkdir -p /etc/gparted-live/
cp -ar /live-hook-dir/gparted/gparted-live.d /etc/gparted-live/

# Put main gparted live start service
if is_systemd_init; then
  # Systemd
  # (1) Prepare the gparted live starting service
  # (2) Prepare autologin for tty*
  cp -af /live-hook-dir/systemd/start-gparted-live.service /lib/systemd/system/; chown root.root /lib/systemd/system/start-gparted-live.service
  ( cd /etc/systemd/system/getty.target.wants/; ln -fs /lib/systemd/system/start-gparted-live.service .)
  # Disable the booting status, otherwise the messages might be shown on the dialog menu (of keyboard/language for Clonezilla/GParted live) which is annoying.
  perl -pi -e "s/^[#]ShowStatus=.*/ShowStatus=no/g" /etc/systemd/system.conf
  for i in `seq 1 6`; do
    # tty1-6
    mkdir /etc/systemd/system/getty\@tty${i}.service.d/
    cp -af /live-hook-dir/systemd/tty-autologin-override.conf /etc/systemd/system/getty\@tty${i}.service.d/override.conf
    chown root.root /etc/systemd/system/getty\@tty${i}.service.d/override.conf
  done
  for i in `seq 0 3`; do
    # ttyS0-3
    mkdir /etc/systemd/system/serial-getty\@ttyS${i}.service.d/
    cp -af /live-hook-dir/systemd/serial-console-autologin.conf /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
    chown root.root /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
  done
  # Put the script "start-gparted-live" to start /etc/gparted/gparted.d/
  cp -af /live-hook-dir/start-gparted-live /etc/gparted-live/
elif [ -d "/etc/init" ] && dpkg -L upstart &>/dev/null; then
  # Ubuntu's upstart. We use the compatibility mode of sysv-init in upstart
  cp -af /live-hook-dir/start-gparted-live /etc/rc2.d/S99start-gparted-live
elif type insserv &>/dev/null; then
  # This has to be 2nd (i.e. in elif, since ubuntu also has insserv, but it is not enabled)
  install -o root -g root -m 755 /live-hook-dir/start-gparted-live /etc/init.d/
  insserv start-gparted-live
else
  cp -af /live-hook-dir/start-gparted-live /etc/rc2.d/S99start-gparted-live
fi

# Put a link for vol_id so that GParted can use it to read linux-swap labels
if [ -e /lib/udev/vol_id ]; then
  (cd /sbin; ln -fs /lib/udev/vol_id vol_id)
fi

# we need real /sbin/start-stop-daemon
remove_start_stop_daemon_diverts

# Since we remove_start_stop_daemon_diverts, therefore we have to set it back so the rest of chroot_dpkg of live-build v3.x won't remove the real /sbin/start-stop-daemon. The reason we remove then set is because we have to keep compatable with live-build v2.x. In the future when we switched to live-build v3.x completely, both "remove_start_stop_daemon_diverts" and "set_start_stop_daemon_diverts" for ocs-live-hook can be removed. //NOTE// They can not be removed in drbl-live-hook when live-build v3.x is used in the future.
set_start_stop_daemon_diverts

# Dirty hacking: rm files
echo "Starting dirty hacking to remove files..."
# Example:
# //NOTE// list them very carefully, otherwise maybe due to dependence, required package will be removed!
# unnecessary_packages="libc6-dev ncpfs libpam-ncp binutils dpkg-dev linux-libc-dev make patch wget alsa bzip2 xserver-xorg-input-wacom whiptail"
unnecessary_packages="libgl1-mesa-dri xorg-docs-core xfonts-100dpi xfonts-75dpi xfonts-scalable"
if [ -n "$unnecessary_packages" ]; then
  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
fi

#
kernel_ver="$(unalias ls 2>/dev/null; ls /lib/modules/)"
# List for the file or dir to be removed:
# /lib/modules/$kernel_ver/kernel/
# //NOTE// /lib/modules/$kernel_ver/kernel/lib is important for Debian lenny, since isofs need that. DO NOT REMOVE IT! Thanks to Louie Chen.
#kdir_2_be_rm="sound" 
kdir_2_be_rm="" 
# /lib/modules/$kernel_ver/kernel/drivers/
#drv_2_be_rm="atm telephony isdn bluetooth infiniband isdn media leds kvm spi w1" 
drv_2_be_rm=""
# /lib/modules/$kernel_ver/kernel/fs/
# Forget about removing the fs driver... we'd better to keep them.
#fs_2_be_rm="9p afs dlm smbfs cifs coda autofs* minix gfs* nfs* quota* lockd romfs ncpfs ocfs* udf" 
fs_2_be_rm=""
# /usr/share/fonts/X11/misc/
font_2_be_rm="*ja* *ko* *kan* *gb*"
# /usr/share/
share_dir_2_be_rm="info 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

#
decide_if_use_xz_compression_for_initrd

# We might need more /lib/udev/*_id than that initramfs-tools copies. E.g. for Live CD, we need cdrom_id, which udev (>= version 146 won't copy).
copy_all_dev_id_prog_from_udev_lib

# Append extra modules included in initramfs, since some NIC modules (e.g. atl2) will not be included in initramfs, those driver is required for PXE booting. 
# Ref: https://sourceforge.net/tracker/?func=detail&atid=671650&aid=2854969&group_id=115473
if [ -e "/etc/initramfs-tools/modules" ]; then
  echo "# The following modules were added by GParted" >> /etc/initramfs-tools/modules
  for i in $extra_module_list_in_initramfs; do
    echo "$i" >> /etc/initramfs-tools/modules
  done 
  update_initramfs_flag="yes"
fi


# 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
  # Here we remove files only, but keep dirs so that later if user want to install packages, postrun won't exit because no /usr/share/man/man1, for example.
  # rm -rf /usr/share/$i/*
  find /usr/share/$i/ -type f -exec rm -f {} \;
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
# Log, clean them, but keep the file name. i.e. make the size 0, otherwise if we clean them, some daemon will complain.
find /var/log/ -type f | xargs -I % bash -c "echo -n '' > %"
# CPP
find /usr/lib/gcc/ -name "cc1" -exec rm {} \;
find /usr/bin/ -name "cpp*" -exec rm {} \;
# Forget about removing zoneinfo.... This might break live-config.
#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/*
#  # Restore UTC only to avoind live-initramfs giving error
#  cp -af /etc/localtime /usr/share/zoneinfo/UTC 
#fi
# Forget about removing gconv, since some program, e.g. mtools 4.10 need that.
# [ -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 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

# Remove grpck option "-p". This is only a workaround since the issue has not been fixed in Debian
# Ref: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=638263
remove_grpck_opt_p

if dpkg -L deborphan &>/dev/null; then	   
   echo "Removing deborphan..."
   apt-get --yes --force-yes --purge remove deborphan
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

# We have to run update-initramfs so that the new busybox/cdromid/... will be in initramfs. This should be after dirty_hacking_rm_files_for_ocs_live since some files which we do not want (e.g. plymouth) might be included in initrd.
[ "$update_initramfs_flag" = "yes" ] && update-initramfs -u

# 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
