#!/bin/sh
export DEBIAN_ONLY=1; export HAS_POSTPROCESSING=1; /opt/drblcd/drblcd-hdinstall.real

# must be root
ID=`id -u`
if [ "$ID" != "0" ]; then exit 1; fi

# locale and dialog
if [ "$1" != "" ]; then
  LOCALE=$1
  LC_ALL=$1
  export LC_ALL
else
  LOCALE=zh_TW.Big5
  LC_ALL=zh_TW.Big5
  export LC_ALL
fi

case "`tty`" in
  /ram1/dev/tty[1-8])
    MODE=text
    DIA=dialog
    ;;
  /ram1/dev/pts/*|/ram1/dev/ttyp*)
    MODE=x
    XDIALOG_HIGH_DIALOG_COMPAT=1
    export XDIALOG_HIGH_DIALOG_COMPAT
    DIA=Xdialog
    ;;
  *)
    MODE=text
    DIA=dialog
    ;;
esac

#if [ -z "$DISPLAY" ] ; then
  MODE=text
  DIA=dialog
#fi

# find fstab and copy it to /tmp
found=0
if [ -f /proc/partitions ] ; then
  while read x x x p x
  do
    if [ $found -eq 1 ]; then break; fi

    case "$p" in
      hd?)
        if [ "`cat /proc/ide/$p/media`" = "disk" ] ; then
          hd_part=`/sbin/sfdisk -l /dev/$p | grep "^\/dev\/" | awk '{print $1;}'`

          for part in $hd_part
          do
            mount $part /mnt
            if [ -f /mnt/fstab ]; then
              cp /mnt/fstab /tmp
              found=1;
            elif [ -f /mnt/etc/fstab ]; then
              cp /mnt/etc/fstab /tmp
              found=1;
            fi
            umount /mnt

            if [ $found -eq 1 ]; then break; fi
          done
        fi
        ;;
    esac
  done < /proc/partitions
fi

# mount partitions based on /tmp/fstab
if [ $found -eq 0 ]; then exit 0; fi

while read p d f x
do
  if [ "$f" = "proc" -o "$f" = "tmpfs" -o "$f" = "swap" ]; then continue; fi
  mount $p /mnt/$d
done < /tmp/fstab

# CD stuff
rm -f /mnt/etc/init.d/drbl-bootcd.sh
rm -f /mnt/etc/rcS.d/S38drbl-bootcd.sh
rm -rf /mnt/opt/drblcd /mnt/opt/drbl
rm -f /mnt/etc/kde3/kdm/kdmrc
rm -f /mnt/etc/hostname
rm -f /mnt/etc/hosts
rm -f /mnt/etc/passwd
rm -f /mnt/etc/shadow

cp /drbl/etc/kde3/kdm/kdmrc /mnt/etc/kde3/kdm/
cp /drbl/etc/hostname /mnt/etc/
cp /drbl/etc/hosts /mnt/etc/
cp /drbl/etc/passwd /mnt/etc
if [ -f /drbl/etc/shadow ]; then cp /drbl/etc/shadow /mnt/etc/; fi

for dir in `ls /home`; do
  #mkdir -p /mnt/home/$dir
  cp -aH /home/$dir /mnt/home/$dir
  ownership=`cat /etc/passwd | grep "/home/$dir" | awk -F: '{ print $3"."$4; }'`
  chown $ownership /mnt/home/$dir
  #cp -a /home/$dir/* /mnt/home/$dir
  #cp -a /home/$dir/.* /mnt/home/$dir
  rm -f /home/$dir/.kde/tmp-$(hostname)
  rm -f /home/$dir/.kde/socket-$(hostname)
done

# umount partitions based on /tmp/fstab
while read p d f x
do
  if [ "$d" = "/" ]; then continue; fi
  if [ "$f" = "proc" -o "$f" = "tmpfs" -o "$f" = "swap" ]; then continue; fi
  umount -l /mnt/$d
done < /tmp/fstab
umount /mnt
sync

# end
MSG_BIG5="P±z DRBL\n\n\
YD, ziHb\n\
  http://drbl.nchc.gov.tw/debian/misc.html \n\
T,\n\n\
Ϊ̬O email \n\
  c00hkl00@nchc.gov.tw  c00jhs00@nchc.gov.tw\n\n\
"
MSG_EN="Thanks for choose DRBL\n\n\
if you have any problem, you can visit the website:\n\
  http://drbl.nchc.gov.tw/debian/misc.html\n\
or email to \n\
  c00hkl00@nchc.gov.tw and c00jhs00@nchc.gov.tw\n\n\
"
if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
else MSG=$MSG_EN; fi
$DIA --backtitle "$backtitle" --title "$title" \
  --msgbox "$MSG" 15 60
exit 0
