#!/bin/sh

# Author: Blake, Kuo-Lien Huang (blake@nchc.gov.tw)
# License: GPL
# Description:
#  2003/01/29 Blake, Kuo-Lien Huang (blake@nchc.gov.tw)
#    modified from knx_hdinstall
#    following are the information about knx_hdinstall
#
#    Knoppix Installation auf Festplatte
#    knx-hdinstall 0.29test
#    7/2002 Christian Perle, perle@itm.tu-clausthal.de
#
#  2003/04/28 Blake, Kuo-Lien Huang (blake@nchc.gov.tw)
#    local en support
#
TARGET="/tmp/drblcd"
backtitle="drbl wizard"
title="drblcd-hdinstall"
kernel_version=2.4.25

# must be root
ID=`id -u`
if [ "$ID" != "0" ]; then
  echo "You must be root to use this script."
  echo "Use sudo to enable it for users."
  exit 1
fi

# Blake, 2004/03/15
# locale
#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
NETCONFIG=0
LOCALE="zh_TW.Big5"
case "$1" in
  "--netconfig")
    NETCONFIG=1
    shift
    ;;
  "--locale")
    shift
    LOCALE=$1
    shift
    ;;
  "--debian-only")
    export DEBIAN_ONLY=1
    export ONLY_DEBIAN=1
    ;;
esac
LC_ALL=$LOCALE
export LC_ALL=$LOCALE

# text or x-window
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
    LOCALE="C"
    ;;
esac

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

# target
TR="/mnt/target"
TMP="/tmp/hdinst.tmp.$$"

# function: set_non_interactive
set_non_interactive() {

  imageroot="$1"
  noninteractive="$2"

  chroot $imageroot apt-get -y install expect

  # non_interactive
  if [ "$noninteractive" = "true" ]; then

    cp setdebconf-v0.9 $imageroot/root

    cat <<-EOF > $imageroot/etc/apt/apt.conf
APT 
{
  // Options for apt-get
  Get 
  {
     Download-Only "false";
     Assume-Yes "true";
     Fix-Broken "true";
  };

  Immediate-Configure "false";
  Force-LoopBreak "true";
};

// Options for the downloading routines
Acquire
{
  Retries "0";
};

// Things that effect the APT dselect method
DSelect 
{
  Clean "auto";   // always|auto|prompt|never
};

DPkg 
{
  // Probably don't want to use force-downgrade..
  Options {"--force-overwrite";}
}
EOF

    cat <<-EOF > $imageroot/root/debconf_noninteractive
#!/bin/sh
# \\
exec expect "\$0" \${1+"\$@"}
spawn /usr/sbin/dpkg-reconfigure --frontend=readline debconf
expect "What interface should be used for configuring packages?"
send "5\r"
#expect "Ignore questions with a priority less than.."
expect "See only questions that are of what priority and higher?"
send "1\r"
expect "Show all old questions again and again?"
send "no\r"
expect eof
EOF
    chmod 755 $imageroot/root/debconf_noninteractive
    chroot $imageroot /root/debconf_noninteractive
    rm -rf $imageroot/root/debconf_noninteractive

  # interactive (dialog)
  else

    cat <<-EOF > $imageroot/etc/apt/apt.conf
DPkg 
{
  Options { "--force-overwrite"; }
}
EOF

    cat <<-EOF > $imageroot/root/debconf_dialog
#!/bin/sh
# \\
exec expect "\$0" \${1+"\$@"}
spawn /usr/sbin/dpkg-reconfigure --frontend=readline debconf
expect "What interface should be used for configuring packages?"
send "1\r"
#expect "Ignore questions with a priority less than.."
expect "See only questions that are of what priority and higher?"
send "3\r"
expect "Show all old questions again and again?"
send "no\r"
expect eof
EOF
    chmod 755 $imageroot/root/debconf_dialog
    chroot $imageroot /root/debconf_dialog
    rm -f $imageroot/root/debconf_dialog
  fi
}

# function: copy_to
copy_to() {
  copy_src="$1"
  copy_dest="$2"

  echo -n "[$copy_src ==> $copy_dest] "
  /usr/bin/rsync -a -v $copy_src $copy_dest 2> /dev/null > $TMP
  echo $(tail -n 1 $TMP)
}

# function: confiure_network
configure_network() {

  local TARGET=$1

  # hostname
  DEFHNAME="drbl"
  MSG_BIG5="пJӾW(FQDN):"
  MSG_EN="Please input the name (FQDN) of the machine:"
  if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
  else MSG=$MSG_EN; fi
  $DIA --backtitle "$backtitle" --title "$title" \
    --inputbox "$MSG" 12 60 $DEFHNAME 2> $TMP
  HNAME="`cat $TMP | cut -d"." -f1`"
  FQDN="`cat $TMP`"
  [ -z "$HNAME" ] && HNAME=$DEFNAME
  echo "$HNAME" > $TARGET/etc/hostname
  echo "$HNAME" > $TARGET/etc/mailname
  echo "127.0.0.1	localhost	localhost.localdomain" > $TARGET/etc/hosts
  cp $TMP $TARGET/etc/FQDN
  # setup hostname of webmin
  /usr/bin/perl -p -i -e "s/host=.*/host=$HNAME/" $TARGET/etc/webmin/miniserv.conf
  if [ "$LOCALE" = "zh_TW.Big5" ]; then
    echo "lang=zh_TW.Big5" >> $TARGET/etc/webmin/config
  fi
  ##
  /bin/hostname $HNAME

  NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}')"
  DONAMECONF="1"
  GW=""
  if [ "$NETDEVICES" = "" ]; then return; fi
  ##
  ## Blake, 2004/02/06, use alias to setup outgoing interfaces
  ##
  MSG_BIG5="\nzO_nϥ Alias ӳ]w~su ?\n\n\
oOzƱz~dPɯ]wIPpUϥ"
  MSG_EN="\nDo you want to use 'Alias' to setup the outgoing interface ?\n\n\
This is used when you want to setup two IP on your outgoing interface"
  if [ "$LOCALE" = "zh_TW.Big5"  ]; then MSG="$MSG_BIG5"
  else MSG="$MSG_EN"; fi
  $DIA --backtitle "$backtitle" --title "$title" --yesno "$MSG" 12 60
  USEALIAS=$?

  while [ $USEALIAS -eq 0 ]; do

    OUTDEV=""

    MSG_BIG5="\nzQϥ DHCP  BOOTP ӳ]w~su?\n\n\
oubzϰ DHCP  BOOTP Aɤ~@"
    MSG_EN="Do you want to use DHCP or BOOTP to automatically configure outgoing link ?\n\n\
You'll need a DHCP or BOOT server in the local network for this to work"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title (Alias)" --yesno "$MSG" 12 60
    USEDHCP=$?
    ## use dhcp
    if [ $USEDHCP -eq 0 ]; then
      MSG_BIG5="\n}lչ~su, o|O@Ǯɶ..."
      MSG_EN="\nTesting outgoing network link. This will take few miniutes..."
      if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
      else MSG=$MSG_EN; fi
      $DIA --backtitle "$backtitle" --title "$title (Alias)" \
       --msgbox "$MSG" 8 60
      ### testing using dhcp
      for DEVICE in $NETDEVICES; do
        echo -n "Testing $DEVICE..."
        /sbin/dhclient $DEVICE
        IP=`ifconfig $DEVICE | grep "inet addr" | cut -d: -f2 | cut -d' ' -f1`
        if [ "$IP" != "" ]; then 
           OUTDEV=$DEVICE
           GW=$DEVICE 
           echo "found."
           break 
        fi
        /sbin/ifconfig $DEVICE down
        echo "not found."
      done 
      ###
    else

      DOCONFIG=1
      while [ $DOCONFIG -eq 1 ]; do

        MSG_BIG5="пJ~IP}:"
        MSG_EN="IP address:"
        if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
        else MSG=$MSG_EN; fi
        $DIA --backtitle "$backtitle" --title "$title (Alias)" \
          --inputbox "$MSG" 12 60 "" 2> $TMP
        IP="`cat $TMP`"
        if [ "$IP" = "" ]; then break; fi

        MSG_BIG5="пJBn(netmask):"
        MSG_EN="Netmask:"
        if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
        else MSG=$MSG_EN; fi
        $DIA --backtitle "$backtitle" --title "$title (Alias)" \
          --inputbox "$MSG" 12 60 "255.255.255.0" 2> $TMP
        NETMASK="`cat $TMP`"
        #$DIA --backtitle "$backtitle" --title "$title" \
        #  --inputbox "пJ}:" 12 60 "" 2> $TMP
        #NETWORK="`cat $TMP`"
        NETWORK="`netmask $IP/$NETMASK | sed -e 's/ //g' | awk -F/ '{ print $1; }'`"

        MSG_BIG5="пJs}(broadcast):"
        MSG_EN="Broadcast:"
        if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
        else MSG=$MSG_EN; fi
        #BROADCAST="`echo "$NETWORK" | awk -F\. '{ print $1"."$2"."$3".255"; }'`"
        BROADCAST="`netmask -r $IP/$NETMASK | awk '{ print $1; }' | cut -d- -f2`"
        $DIA --backtitle "$backtitle" --title "$title (Alias)" \
          --inputbox "$MSG" 12 60 "$BROADCAST" 2> $TMP
        BROADCAST="`cat $TMP`"

        MSG_BIG5="пJhD}(gateway):"
        MSG_EN="Gateway:"
        if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
        else MSG=$MSG_EN; fi
        $DIA --backtitle "$backtitle" --title "$title (Alias)" \
          --inputbox "$MSG" 12 60 "" 2> $TMP
        GATEWAY="`cat $TMP`"

        # make sure it is right ...
        MSG_BIG5="HU]wO_T ?\n\n\
IP}: $IP\n\
Bn(netmask): $NETMASK\n\
s}(broadcast): $BROADCAST\n\
hD}(gateway): $GATEWAY"
        MSG_EN="Is the following configuration correct ?\n\n\
IP: $IP\n\
Netmask: $NETMASK\n\
Broadcast: $BROADCAST\n\
Gateway: $GATEWAY"
        if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
        else MSG=$MSG_EN; fi
        $DIA --backtitle "$backtitle" --title "$title (Alias)" \
          --yesno "$MSG" 12 60

        DOCONFIG=$?
      done
      if [ "$IP" = "" -o "$NETMASK" = "" -o "$GATEWAY" = "" ]; then OUTDEV=""; break; fi
 
      ## testing or not
      MSG_BIG5="\nzO_nչ~su, o|O@Ǯɶ..."
      MSG_EN="\nWould you like to esting outgoing network link. This will take few miniutes..."
      if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
      else MSG=$MSG_EN; fi
      $DIA --backtitle "$backtitle" --title "$title (Alias)" \
        --yesno "$MSG" 8 60
      DO_TESTLINK=$? 

      if [ $DO_TESTLINK -eq 0 ]; then
        for DEVICE in $NETDEVICES; do
          echo -n "Testing $DEVICE..."
          /sbin/ifconfig $DEVICE $IP netmask $NETMASK up 2> /dev/null 2>&1
          if ping -c 2 $GATEWAY >/dev/null 2>&1 ; then
            OUTDEV=$DEVICE
            GW=$DEVICE
            echo "found."
            break
          fi
          /sbin/ifconfig $DEVICE down
          echo "not found."
        done
      else
        DEVICELIST_BIG5=""
        DEVICELIST_EN=""
        NUMDEVICE=0
        for DEVICE in $NETDEVICES; do 
          DEVICELIST_BIG5="$DEVICELIST_BIG5 ${DEVICE} d_${DEVICE##eth} off"
          DEVICELIST_EN="$DEVICELIST_EN ${DEVICE} card_${DEVICE##eth} off"
          NUMDEVICE=`expr $NUMDEVICE + 1`
        done

        DEVICELIST=""
        if [ "$LOCALE" = "zh_TW.Big5" ]; then DEVICELIST=$DEVICELIST_BIG5
        else DEVICELIST=$DEVICELIST_EN; fi

        > $TMP
        MSG_BIG5="пܨϥ Alias ]w~su~d:"
        MSG_EN="Please choose the outgoing network card:"
        if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
        else MSG=$MSG_EN; fi
        $DIA --backtitle "$backtitle" --title "$title (Alias)" \
          --radiolist "$MSG" 16 60 $NUMDEVICE $DEVICELIST 2>$TMP
        OUTDEV="`cat $TMP`"
        if [ "$OUTDEV" != "" ]; then GW=$DEVICE; fi
      fi
      ##
    fi

    ## OUTDEV
    if [ "$OUTDEV" = "" ]; then 
      MSG_BIG5="\nS~suΪ̬Oz]w~\n\n
zO_ns]w Alias ?"
      MSG_EN="\nNo outgoing network link or Incorrect network configuration.\n\n
Do you want to do it again ?"
      if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
      else MSG=$MSG_EN; fi
      $DIA --backtitle "$backtitle" --title "$title (Alias)" --yesno "$MSG" 12 60
      USEALIAS=$?
    else
      USEALIAS=1
      ## Blake, 2004/02/06, use networking-alias & /etc/network/interfaces.alias 
      if [ $USEDHCP -eq 0 ]; then
        echo -e "iface $OUTDEV:1 inet dhcp\n" >> $TARGET/etc/network/interfaces.alias
        # the DNS should be setuped via DHCP
        DONAMECONF="0"
      else
        cat <<-EOF >> $TARGET/etc/network/interfaces.alias
iface $OUTDEV:1 inet static
  address $IP
  netmask $NETMASK
  network $NETWORK
  broadcast $BROADCAST
  gateway $GATEWAY
EOF
        # Blake, 2004/04/20, hosts
        if [ "$FQDN" != "$HNAME" ]; then
          echo "$IP $FQDN $HNAME" >> $TARGET/etc/hosts
        else
          echo "$IP $HNAME" >> $TARGET/etc/hosts
        fi

        ##
        ALIAS_DEV="$OUTDEV:1"
        ALIAS_IP=$IP
        ALIAS_NETMASK=$NETMASK
        ALIAS_BROADCAST=$BROADCAST
        ALIAS_GATEWAY=$GATEWAY
      fi
    fi
  done

  # ip address
  cat <<-EOF > $TARGET/etc/network/interfaces
auto lo
iface lo inet loopback

EOF
  NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}')"
  DEVICELIST_BIG5=""
  DEVICELIST_EN=""
  NUMDEVICE=0
  for DEVICE in $NETDEVICES 
  do 
    DEVICELIST_BIG5="$DEVICELIST_BIG5 ${DEVICE} d_${DEVICE##eth} off"
    DEVICELIST_EN="$DEVICELIST_EN ${DEVICE} card_${DEVICE##eth} off"
    NUMDEVICE=`expr $NUMDEVICE + 1`
  done

  DEVICELIST=""
  if [ "$LOCALE" = "zh_TW.Big5" ]; then DEVICELIST=$DEVICELIST_BIG5
  else DEVICELIST=$DEVICELIST_EN; fi

  #DONAMECONF="1"
  if [ "$DEVICELIST" = "" ]; then DONAMECONF="0"; fi

  #GW=""
  while [ "$DEVICELIST" != "" ]; do
    > $TMP
    MSG_BIG5="пܱzn]wd:"
    MSG_EN="Please choose the network card to setup:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --radiolist "$MSG" 16 60 $NUMDEVICE $DEVICELIST 2>$TMP
    DEVCHOICE="`cat $TMP`"
    # doesn't choose any one, just break it
    if [ "$DEVCHOICE" = "" ]; then break; fi

    # Blake, 2004/07/21, 鷺nϥΩTw IP
    if [ $USEALIAS -eq 0 ]; then
      # Blake, 2004/01/09, dhcp support
      MSG_BIG5="\nzQϥ DHCP  BOOTP ӳ]woid($DEVCHOICE)?\n\n\
oubzϰ DHCP  BOOTP Aɤ~@,\n
uAΩ]wDRBLDebian~su"
      MSG_EN="Do you want to use DHCP or BOOTP to automatically configure this interface ($DEVCHOICE)?\n\n\
You'll need a DHCP or BOOT server in the local network for this to work,\n
Only use for outgoing network interface configuration"
      if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
      else MSG=$MSG_EN; fi
      $DIA --backtitle "$backtitle" --title "$title" \
        --yesno "$MSG" 12 60
      USEDHCP=$?
    else
      ## ] USEALIAS=1 ܹ~wg]wnF, ҥH..ثe]wO鷺
      USEDHCP=1
    fi

    if [ $USEDHCP -eq 0 ]; then
      #/sbin/dhclient $DEVCHOICE
      #IP=`/sbin/ifconfig $DEVCHOICE | cut -d: -f2 | cut -d' ' -f1`
      #NETMASK=`/sbin/ifconfig $DEVCHOICE | cut -d: -f4 | cut -d' ' -f1`
      #BROADCAST=`/sbin/ifconfig $DEVCHOICE | cut -d: -f3 | cut -d' ' -f1`
      #NETWORK=`netmask $IP/$NETMASK | sed -e 's/ //g' | awk -F/ '{ print $1; }'`
      #if [ "$GW" = "" ]; then
      #  GATEWAY=`/sbin/route -n | grep -e "^0.0.0.0" | awk '{ print $2; }'`
      #  if [ "$GATEWAY" != "" ]; then GW=$DEVCHOICE; fi
      #fi
      #if [ "$IP" = "" ]; then
      #  MSG_BIG5="\nS DHCP  BOOTP A\n\nФʳ]woid($DEVCHOICE)"
      #  MSG_EN="No DHCP or BOOTP server found!\n\nPlease configure the interface ($DEVCHOICE) manually"
      #  $DIA --backtitle "$backtitle" --title "$title" \
      #    --msgbox "$MSG" 12 60
      #else
        cat <<-EOF >> $TARGET/etc/network/interfaces
auto $DEVCHOICE
iface $DEVCHOICE inet dhcp

EOF
        # Blake, 2004/01/06, the GATEWAY should be setuped via DHCP
        GW=$DEVCHOICE
        # Blake, 2004/01/06, the DNS should be setuped via DHCP
        DONAMECONF="0"

        # remove $DEVCHOICE form $DEVICELIST
        >$TMP
        echo "$DEVICELIST" | awk -v D=$DEVCHOICE '{ for(i=1;i<=NF;i+=3) if($i!=D) print $i " " $(i+1) " " $(i+2) " "; }' > $TMP
        DEVICELIST="`cat $TMP`"
        NUMDEVICE=`expr $NUMDEVICE - 1`

        continue
      #fi
    fi

    # do configure
    MSG_BIG5="пJIP}:"
    MSG_EN="IP address:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --inputbox "$MSG" 12 60 "" 2> $TMP
    IP="`cat $TMP`"

    MSG_BIG5="пJBn(netmask):"
    MSG_EN="Netmask:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --inputbox "$MSG" 12 60 "255.255.255.0" 2> $TMP
    NETMASK="`cat $TMP`"
    #$DIA --backtitle "$backtitle" --title "$title" \
    #  --inputbox "пJ}:" 12 60 "" 2> $TMP
    #NETWORK="`cat $TMP`"
    NETWORK="`netmask $IP/$NETMASK | sed -e 's/ //g' | awk -F/ '{ print $1; }'`"

    MSG_BIG5="пJs}(broadcast):"
    MSG_EN="Broadcast:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    #BROADCAST="`echo "$NETWORK" | awk -F\. '{ print $1"."$2"."$3".255"; }'`"
    BROADCAST="`netmask -r $IP/$NETMASK | awk '{ print $1; }' | cut -d- -f2`"
    $DIA --backtitle "$backtitle" --title "$title" \
      --inputbox "$MSG" 12 60 "$BROADCAST" 2> $TMP
    BROADCAST="`cat $TMP`"

    if [ "$GW" = "" ]; then
      MSG_BIG5="пJhD}(gateway):"
      MSG_EN="Gateway:"
      if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
      else MSG=$MSG_EN; fi
      $DIA --backtitle "$backtitle" --title "$title" \
        --inputbox "$MSG" 12 60 "" 2> $TMP
      GATEWAY="`cat $TMP`"
      if [ "$GATEWAY" != "" ]; then GW="$DEVCHOICE"; fi
      if [ "$FQDN" != "$HNAME" ]; then
        cat <<-EOF >> $TARGET/etc/hosts
$IP $FQDN $HNAME
EOF
      else
        cat <<-EOF >> $TARGET/etc/hosts
$IP $HNAME
EOF
      fi
    else
      GATEWAY=""
    fi 

    # make sure it is right..
    MSG_BIG5="HU]wO_T ?\n\n\
IP}: $IP\n\
Bn(netmask): $NETMASK\n\
s}(broadcast): $BROADCAST"
    MSG_EN="Is the following configuration correct ?\n\n\
IP: $IP\n\
Netmask: $NETMASK\n\
Broadcast: $BROADCAST\n"
    if [ "$GATEWAY" != "" ]; then
      MSG_BIG5="$MSG_BIG5\nhD}(gateway): $GATEWAY"
      MSG_EN="$MSG_EN\nGateway: $GATEWAY"
    fi
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --yesno "$MSG" 12 60
    if [ $? -eq 1 ]; then 
      if [ "$GATEWAY" != "" ]; then GW=""; fi
      continue 
    fi

    cat <<-EOF >> $TARGET/etc/network/interfaces
auto $DEVCHOICE
iface $DEVCHOICE inet static
  address $IP
  netmask $NETMASK
  network $NETWORK
  broadcast $BROADCAST
EOF

    if [ "$GATEWAY" != "" ]; then 
      echo "  gateway $GATEWAY" >> $TARGET/etc/network/interfaces
    fi
    echo >> $TARGET/etc/network/interfaces
    cat <<-EOF >> $TARGET/etc/networks
localnet $NETWORK
EOF

    # dhcpd.conf
    mkdir -p $TARGET/etc/dhcp3
    cat <<-EOF >> $TARGET/etc/dhcp3/dhcpd.conf
subnet $NETWORK netmask $NETMASK {
  option broadcast-address $BROADCAST;
  option subnet-mask $NETMASK;
}
EOF

    # remove $DEVCHOICE form $DEVICELIST
    >$TMP
    echo "$DEVICELIST" | awk -v D=$DEVCHOICE '{ for(i=1;i<=NF;i+=3) if($i!=D) print $i " " $(i+1) " " $(i+2) " "; }' > $TMP
    DEVICELIST="`cat $TMP`"
    NUMDEVICE=`expr $NUMDEVICE - 1`
  done

  # name server
  if [ "$DONAMECONF" = "1" ]; then 
    >$TMP
    MSG_BIG5="пJW٦A(DNS)}:"
    MSG_EN="DNS Server:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
    --inputbox "$MSG" 12 60 "" 2>$TMP
    NAMESERV="`cat $TMP`"
    if [ "$NAMESERV" != "" ]; then
      echo "nameserver $NAMESERV" > $TARGET/etc/resolv.conf
    fi
  fi

  # HNAME in hosts ?
  if [ "$(wc -l $TARGET/etc/hosts | cut -d' ' -f1)" = "1" ]; then
    echo "127.0.0.1	localhost	localhost.localdomain	$HNAME" > $TARGET/etc/hosts
  fi

  # IPv6 capable hosts
  cat <<-EOF >> $TARGET/etc/hosts

# The following lines are desirable for IPv6 capable hosts
# (added automatically by netbase upgrade)

::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet                                            
ff00::0 ip6-mcastprefix                                         
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
EOF

}

##
## netconf
##
if [ $NETCONFIG -eq 1 ]; then
  # find fstab and copy it to /tmp
  found=0
  if [ -f /tmp/fstab ]; then found=1; fi

  if [ $found -eq 0 -a -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 
    #echo "No fstab found! Abort!!" ; exit 0
    MSG_BIG5="SwФW Debian/DRBL w. ]w!"
    MSG_EN="No fstab found in your HD. Abort!"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --msgbox "$MSG" 15 40 
    exit 0
  fi

  mkdir -p $TARGET
  while read p d f x
  do
    if [ "$(echo "$p" | grep -e "^#")" != "" ]; then continue; fi
    if [ "$f" = "proc" -o "$f" = "tmpfs" -o "$f" = "swap" ]; then continue; fi
    mount $p $TARGET/$d
  done < /tmp/fstab

  # configure_network 
  configure_network $TARGET

  # 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 $TARGET/$d
  done < /tmp/fstab
  umount $TARGET
  sync

  # thank you !
  MSG_BIG5="P±z Debian/DRBL\n\n\
YD, ziHb\n\
  http://drbl.nchc.org.tw/faq \n\
T,\n\n\
Ϊ̬O email \n\
  blake@nchc.org.tw  steven@nchc.org.tw\n\n\
"
  MSG_EN="Thanks for choose Debian/DRBL\n\n\
if you have any problem, you can visit the website:\n\
  http://drbl.nchc.org.tw/faq\n\
or email to \n\
  blake@nchc.org.tw and steven@nchc.org.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
fi
##
## END of netconf
##

##
## main
##
MSG_BIG5="oӵ{|Uzw \n\
		(1)Debian\n"
if [ "$DEBIAN_ONLY" = "" -a "$ONLY_DEBIAN" = "" ]; then
  MSG_BIG5="$MSG_BIG5\
		(2)DRBL A\n\
		(3)DRBL Ȥݼ˪"
fi
MSG_EN="This program will help you to install\n\
		(1)Debian\n"
if [ "$DEBIAN_ONLY" = "" -a "$ONLY_DEBIAN" = "" ]; then
  MSG_EN="$MSG_EN\
		(2)DRBL Server\n\
		(3)DRBL Client Template"
fi
if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
else MSG=$MSG_EN; fi
$DIA --backtitle "$backtitle" --title "$title" \
  --msgbox "$MSG" 15 40

# Blake,2004/04/02, SCSI HD
for SCSI_DEV in `ls /dev/sd?`; do
  scsi_info $SCSI_DEV &> /dev/null
done

# choice disk
> $TMP
NUMHD=0
if [ -f /proc/partitions ] ; then
  while read x x x p x
  do
    case "$p" in
      hd?)
        if [ "`cat /proc/ide/$p/media`" = "disk" ] ; then
          echo "$p `tr ' ' _ </proc/ide/$p/model`" >> $TMP
          NUMHD=$[NUMHD+1]
        fi
        ;;
      sd?)
        x="`scsi_info /dev/$p | grep MODEL | tr ' ' _`"
        x=${x#*\"}
        x=${x%\"*}
        echo "$p $x" >> $TMP
        NUMHD=$[NUMHD+1]
        ;;
      *) ;;
    esac
  done < /proc/partitions
fi

HARDDISKS="`cat $TMP`"
if [ "$HARDDISKS" = "" ]; then

  MSG_BIG5="Sw, ˬdzƽu"
  MSG_EN="No Disk Detected! Please check your hardware"
  if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
  else MSG=$MSG_EN; fi
  $DIA --backtitle "$backtitle" --title "$title" \
    --msgbox "$MSG" 15 40 
  exit 0

fi
MSG_BIG5="пܱznw˪w:"
MSG_EN="Please choose the disk to install:"
if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
else MSG=$MSG_EN; fi
$DIA --backtitle "$backtitle" --title "$title" \
  --radiolist "$MSG" 16 60 $NUMHD \
  $(echo "$HARDDISKS" | while read p model ; do echo "$p" "$model" off ; done) \
  2> $TMP
HDCHOICE="`cat $TMP`"

if [ -z "$HDCHOICE" ] ; then
  MSG_BIG5="zܤ@xwо~~w"
  MSG_EN="Abort! You have to choose one disk to install!!"
  if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
  else MSG=$MSG_EN; fi
  $DIA --backtitle "$backtitle" --title "$title" \
    --msgbox "$MSG" 15 40
  rm -f $TMP
  exit 0
fi

# dma on/off
MSG_BIG5="O_n} $HDCHOICE  CDROM  DMA 䴩 ?\n\n\
} DMA w˳t׸, Oi|yw˹L{í"
MSG_EN="Enable DMA support for $HDCHOICE and CDROM ?"
if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
else MSG=$MSG_EN; fi
$DIA --backtitle "$backtitle" --title "$title" --yesno "$MSG" 8 60
if [ $? -eq 0 ]; then 
  /sbin/hdparm -d1 /dev/$HDCHOICE
  /sbin/hdparm -d1 /dev/cdrom[0-9]
fi

# auto/expert mode ?
> $TMP

if [ "$ONLY_DEBIAN" = "1" -o "$DEBIAN_ONLY" = 1 ]; then
  auto_root=40
  auto_var=15
  auto_home=40
  auto_swap=5
else
  auto_root=30
  auto_var=35
  auto_home=30
  auto_swap=5
fi

if [ "$LOCALE" = "zh_TW.Big5" ]; then
  INSTMODE_LIST="auto ۰ʼҦ on expert iҦ off"
  $DIA --backtitle "$backtitle" --title "$title" \
    --radiolist "\
пܦw˼Ҧ:\n\
  i(expert)Ҧ\n\
    ѨϥΪ̦ۦεwШåBɮרtλPؿ\n\n\
  ۰(auto)Ҧ\n\
    Ѧw˵{UϥΪ̨̾ڤU覡WwЪŶ:\n\n\
    ؿ  ΰϤjp(H%)  ɮרt\n\
     /            $auto_root%               resiserfs\n\
     /var         $auto_var%               resiserfs\n\
     /home        $auto_home%               resiserfs\n\
     swap          $auto_swap%(<=2G)         swap" 20 60 2 $INSTMODE_LIST 2> $TMP
else
  INSTMODE_LIST="auto auto_installation on expert expert_installation off"
  $DIA --backtitle "$backtitle" --title "$title" \
    --radiolist "\
Please chooe the installation method:\n\
  expert installation\n\
    disk partition, file system, and mount points are choosed by user\n\n\
  auto installation\n\
    follow the table below to install the system:\n\n\
    mount_points  partition_size(%)  file_system\n\
     /              $auto_root%               resiserfs\n\
     /var           $auto_var%               resiserfs\n\
     /home          $auto_home%               resiserfs\n\
     swap            $auto_swap%(<=2G)         swap" 20 60 2 $INSTMODE_LIST 2> $TMP
fi
INSTMODE=`cat $TMP`

if [ "$INSTMODE" = "" ]; then
  MSG_BIG5="zܦ۰ʩζiwˤ~i~w"
  MSG_EN="Abort! You have choose one method (auto or expert) for installation!!"
  if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
  else MSG=$MSG_EN; fi
  $DIA --backtitle "$backtitle" --title "$title" \
    --msgbox "$MSG" 15 40
  rm -f $TMP
  exit 0

elif [ "$INSTMODE" = "auto" ]; then
  #
  # auto mode
  #

  # boot device
  BOOTDEVICE="/dev/$HDCHOICE"

  # partition
  hd_size=`sfdisk -s /dev/$HDCHOICE`
  cylinders=`sfdisk -g /dev/$HDCHOICE | awk '{ print $2; }'`
  #swap=`expr $cylinders \* 5 / 100`
  cat <<-EOF > $TMP
$cylinders * $auto_swap / 100
quit
EOF
  swap=`bc $TMP`
  #swap_size=`expr $hd_size \* 5 / 100`
  cat <<-EOF > $TMP
$hd_size * $auto_swap / 100
quit
EOF
  swap_size=`bc $TMP`
  if [ $swap_size -gt 2000000 ]; then 
    #swap=`expr 2000000 \* $cylinders / $hd_size`
	cat <<-EOF > $TMP
2000000 * $cylinders / $hd_size
quit
EOF
    swap=`bc $TMP`
  fi
  #home=`expr $cylinders \* 30 / 100`
  cat <<-EOF > $TMP
$cylinders * $auto_home / 100
quit
EOF
  home=`bc $TMP`
  #root=`expr $cylinders \* 30 / 100`
  cat <<-EOF > $TMP
$cylinders * $auto_root / 100
quit
EOF
  root=`bc $TMP`
  #var=`expr $cylinders - $swap - $home - $root`
  cat <<-EOF > $TMP
$cylinders - $swap - $home - $root
quit
EOF
  var=`bc $TMP`

  cat <<-EOF > $TMP
0,$root,L,*
$root,$var,L
$(expr $root + $var),$home,L
$(expr $root + $var + $home),,S
EOF
  sfdisk /dev/$HDCHOICE < $TMP
  
  # filesystem
  HDCHOICE1="/dev/"$HDCHOICE"1"
  HDCHOICE2="/dev/"$HDCHOICE"2"
  HDCHOICE3="/dev/"$HDCHOICE"3"
  HDCHOICE4="/dev/"$HDCHOICE"4"
  dd if=/dev/zero of=$HDCHOICE4 bs=1k count=16 >/dev/null 2>&1
  sync
  mkswap $HDCHOICE4 > /dev/null 2>&1
  swapon $HDCHOICE4 > /dev/null 2>&1
  mkfs.reiserfs -f $HDCHOICE1 > /dev/null 2>&1
  mkfs.reiserfs -f $HDCHOICE2 > /dev/null 2>&1
  mkfs.reiserfs -f $HDCHOICE3 > /dev/null 2>&1

  # create /tmp/fstab for installation
  cat <<-EOF > /tmp/fstab
$HDCHOICE1 / reiserfs defaults,rw 0 1
$HDCHOICE2 /var reiserfs defaults,rw 0 1
$HDCHOICE3 /home reiserfs defaults,rw 0 1
$HDCHOICE4 none swap sw 0 0
none /proc/bus/usb usbfs defaults 0 0
proc /proc proc defaults 0 0
EOF

  # mount the partition for installation
  mkdir -p $TARGET
  mount -t reiserfs $HDCHOICE1 $TARGET > /dev/null 2>&1
  mkdir -p $TARGET/var
  mount -t reiserfs $HDCHOICE2 $TARGET/var > /dev/null 2>&1
  mkdir -p $TARGET/home
  mount -t reiserfs $HDCHOICE3 $TARGET/home > /dev/null 2>&1
  ROOTPART=$HDCHOICE1

  # grub or lilo
  USEGRUB=1

  # end of auto mode
else
  #
  # expert mode
  #

  # fdisk
  if [ $MODE = text ]; then
    cfdisk /dev/$HDCHOICE
  else
    xterm -bg black -fg white -e cfdisk /dev/$HDCHOICE
  fi

  # swap partition
  # choice the first one linux swap partition
  > $TMP
  SWCHOICE=`fdisk -l /dev/$HDCHOICE | grep "^/dev.*82.*Linux swap$" | awk '{print $1;}'`
  if [ "$SWCHOICE" != "" ]; then
    dd if=/dev/zero of=$SWCHOICE bs=1k count=16 >/dev/null 2>&1
    sync
    mkswap $SWCHOICE 2> $TMP
    if [ $? != 0 ] ; then
      $DIA --backtitle "$backtitle" --title "$title" \
           --msgbox "/sbin/mkswap failed:\n`tail -8 $TMP`" 15 60
      rm -f $TMP
      exit 0
    fi
    swapon $SWCHOICE >/dev/null 2>&1
    echo "$SWCHOICE none swap sw 0 0" >> /tmp/fstab
  fi
  # partition && mount point && file system
  # get the partition information
  > $TMP
  fdisk -l /dev/$HDCHOICE | grep "^/dev.*83.*Linux$" | tr -d '*' |
  while read dev x x size x
  do
    size=${size%+}
    echo $dev $[size/1024]_MB off >> $TMP
  done
  NUMPART="`wc -l $TMP | awk '{print $1;}'`"
  PART="`cat $TMP`"
  if [ "$MUMPART" = "0" ]; then
    MSG_BIG5="zbεwЮɨèSΥXType83ΰ"
    MSG_EN="Abort! No swap partition (Type 83)!!"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --msgbox "$MSG" 15 40
    rm -f $TMP
    exit 0
  fi

  MPNUM=3
  MPLIST_BIG5="/home ϥΪ̮aؿ off /usr ε{ؿ off /var ƥؿ off"
  MPLIST_EN="/home home off /usr user off /var var off"
  if [ "$LOCALE" = "zh_TW.Big5" ]; then MPLIST=$MPLIST_BIG5
  else MPLIST=$MPLIST_EN; fi
  MPCHOICE="/"
  ROOTPART=""
  while [ "$MPCHOICE" = "" -o $MPCHOICE="/" ]
  do
    # choose the mouting point
    if [ "$PART" = "" -o "$MPLIST" = "" ]; then break; fi 
    if [ "$MPCHOICE" = "" ]; then
      >$TMP
      radionum=$MPNUM
      radiolist=$MPLIST
      MSG_BIG5="пܱؿ:"
      MSG_EN="Please choose the mount points:";
      if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
      else MSG=$MSG_EN; fi
      $DIA --backtitle "$backtitle" --title "$title" \
	    --radiolist "$MSG" 20 60 $radionum $radiolist 2> $TMP
      MPCHOICE="`cat $TMP`"
      #echo $MPCHOICE
      # no more, just break it
      if [ "$MPCHOICE" = "" ]; then break; fi
	  # remove $MPCHOICE from $MPLIST
      echo "$MPLIST" | awk -v D=$MPCHOICE '{ for(i=1;i<=NF;i+=3) if($i!=D) print $i " " $(i+1) " " $(i+2) " "; }' > $TMP
      MPLIST="`cat $TMP`"
      MPNUM=`expr $NUMPART - 1`
    fi
    # choose the partition 
    >$TMP
    MSG_BIG5="пܭn $MPCHOICE ΰ:"
    MSG_EN="Please choose the partition to mount at $MPCHOICE:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --radiolist "$MSG" \
	  16 60 $NUMPART $PART 2> $TMP
    PARTCHOICE="`cat $TMP`"
    #echo $PARTCHOICE
    # remove the $PARTCHOICE from $PART
    echo "$PART" | awk -v D=$PARTCHOICE '{ if($1!=D) print $1 " " $2 " " $3 " "; }' > $TMP
    PART="`cat $TMP`"
    NUMPART=`expr $NUMPART - 1`
    #echo $NUMPART ":" $PART
    # filesystem: ext2 ext3 reiserfs xfs
    # format the partition and mount it in the target directory
    > $TMP
    radionum=5
    radiolist="ext2 ext2_fs off ext3 ext3_journalling_fs off reiserfs reiserfs_journalling_fs on xfs xfs_journalling_fs off none keep_old_fs off"
    MSG_BIG5="пܭnϥΪɮרtή榡:"
    MSG_EN="Please choose the file system:"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --radiolist "$MSG" \
	  22 60 $radionum $radiolist 2> $TMP
    FSCHOICE="`cat $TMP`" 
    [ -z "$FSCHOICE" ] && FSCHOICE="ext2"
    #echo $FSCHOICE
    case "$FSCHOICE" in
      "ext2" | "ext3" )
	    mkfs.$FSCHOICE $PARTCHOICE 2> $TMP
	    ;;
	  "reiserfs" | "xfs" )
        mkfs.$FSCHOICE -f $PARTCHOICE 2> $TMP
	    ;;
      "none")
        yes "i" | /sbin/parted /dev/$HDCHOICE p | awk -v HD=$HDCHOICE '{ print "/dev/" HD $0; }' > $TMP
        FSCHOICE="$(cat $TMP | grep "$PARTCHOICE" | awk '{ print $5; }')"
        ;;
    esac
    if [ $? != 0 ]; then
      $DIA --backtitle "$backtitle" --title "$title" \
	    --msgbox "mkfs.$FSCHOICE failed: `tail -8 $TMP`" 15 60
	  rm -f $TMP
	  continue
    fi
    mkdir -p $TARGET/$MPCHOICE 2> $TMP
    mount -t $FSCHOICE $PARTCHOICE $TARGET/$MPCHOICE 2> $TMP
    # create fstab
    echo "$PARTCHOICE $MPCHOICE $FSCHOICE defaults,rw 0 1" >> /tmp/fstab 
    # do next one  
    if [ "$MPCHOICE" = "/" ]; then ROOTPART=$PARTCHOICE; fi
    MPCHOICE=""
  done
  echo "proc /proc proc defaults 0 0" >> /tmp/fstab

  ## lilo or grub ?
  MSG_BIG5="пܭnw˪}޲z{(Boot Manager)\n\n\
Yz GRUB Ow˥, w˵{|ϥ LILO ӨN"
  MSG_EN="Please choose the boot manager\n\n
If your choice is GRUB and the installation is failed, LILO will be installed"
  if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
  else MSG=$MSG_EN; fi
  $DIA --backtitle "$backtitle" --title "$title" \
    --radiolist "$MSG" \
  12 60 3 LILO lilo off GRUB grub on NONE none off 2> $TMP
  BOOTMGR=`cat $TMP` 

  USELILO=0
  USEGRUB=0
  if [ "$BOOTMGR" != "none" ]; then
    # boot device
    MSG_BIG5="пܦw $BOOTMGR "
    MSG_EN="Install $BOOTMGR at"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" \
      --radiolist "$MSG" \
    12 60 2 MBR mbr on Root_Device root_device off 2> $TMP
    BDCHOICE=`cat $TMP`
    if [ "$BDCHOICE" = "Root_Device" ]; then 
      BOOTDEVICE="$ROOTPART"
    else
      BOOTDEVICE="/dev/$HDCHOICE"
    fi

    case "$BOOTMGR" in
      "LILO") USELILO=1 ;;
      "GRUB") USEGRUB=1 ;;
    esac
  fi

  ## Blake, 2004/03/03, backup mbr of boot device
  mkdir -p $TARGET/root
  dd if=/dev/$HDCHOICE of=$TARGET/root/$HDCHOICE.mbr count=1 bs=512 

  # end of expert mode
fi

# copy everything to TARGET
# mkdir all directory in $TARGET directory
echo "Installing Debian ..."
mkdir -p $TARGET/bin $TARGET/boot $TARGET/dev $TARGET/etc $TARGET/home $TARGET/lib $TARGET/mnt $TARGET/opt $TARGET/proc $TARGET/root $TARGET/sbin $TARGET/tmp $TARGET/tftpboot $TARGET/usr $TARGET/var $TARGET/net
chmod 777 $TARGET/tmp
chmod o+t $TARGET/tmp

# 2004/03/04
#for dir in bin boot dev etc lib opt root sbin usr tftpboot
#do cp -a /$dir/* $TARGET/$dir 2> /dev/null; done
for dir in bin boot dev etc lib opt root sbin usr tftpboot
do copy_to "/$dir/*" "$TARGET/$dir" 2> /dev/null; done
copy_to "/vmlinuz*" "$TARGET/"
copy_to "/root/.profile" "$TARGET/root"
copy_to "/root/.bashrc" "$TARGET/root"
copy_to "/tftpboot/.etherboot" "$TARGET/tftpboot"
# copy var.ro to $TARGET/var 
# because there are only empty directory in /var
# (see line 798 of /usr/bin/bootcdwrite for more detail)
copy_to "/var.ro/*" "$TARGET/var"

# Blake, 2003/10/02, remove alsa configuration
if [ -f $TARGET/etc/modutils/alsa ]; then
  rm $TARGET/etc/modutils/alsa
  chroot $TARGET /sbin/update-modules
fi

# Blake, 2003/11/07
mv $TARGET/etc/init.d/modutils.orig $TARGET/etc/init.d/modutils

# configuration
# fstab
mv /tmp/fstab $TARGET/etc/

## 99xfree86-common_start
#if [ "$LOCALE" = "zh_TW.Big5" ]; then
#  echo
#else
#  echo "exec \$REALSTARTUP" > $TARGET/etc/X11/Xsession.d/99xfree86-common_start
#fi
if [ "$LOCALE" != "zh_TW.Big5" ]; then
  rm -f $TARGET/etc/X11/Xsession.d/98xfree86-common_chinese
fi
# recovery the init scripts we change when creating bootcd
perl -p -i -e "s/AutoLoginEnable=.*/AutoLoginEnable=false/" $TARGET/etc/kde3/kdm/kdmrc
if [ "$LOCALE" = "zh_TW.Big5" ]; then
  perl -p -i -e "s/#Language=.*/Language=zh_TW/" $TARGET/etc/kde3/kdm/kdmrc
  cat <<-EOF >> $TARGET/etc/kde3/system.kdeglobals

[Locale]
Country=tw
Language=zh_TW
EOF
fi
# openoffice
#if [ "$LOCALE" != "zh_TW.Big5" ]; then
#  chroot $TARGET apt-get remove openoffice.org-l10n-zh-tw
#fi

mv $TARGET/etc/kde3/kdm/Xstartup.orig $TARGET/etc/kde3/kdm/Xstartup
# remove the init scripts that bootcd used
rm -f $TARGET/etc/rcS.d/S12bootcdram.sh
rm -f $TARGET/etc/rcS.d/S13bootcdflop.sh
# discover in rcS.d (do it again after mountall.sh)
#ln -s ../init.d/discover $TARGET/etc/rcS.d/S37discover
#ln -s ../init.d/discover $TARGET/etc/rc2.d/S01discover
# remove hdparm setting
rm -f $TARGET/etc/hdparm.conf
# remove setupx setting
rm -f $TARGET/etc/init.d/XF86_DEFAULT
rm -f $TARGET/etc/init.d/XF86_DDC
# remove /opt/drbl
rm -rf $TARGET/opt/drbl
# remove /root/*
rm -rf $TARGET/root/*
# set uid on /usr/bin/sudo, /usr/bin/crontab
chmod u+s $TARGET/usr/bin/sudo
chmod u+x $TARGET/usr/bin/crontab
# chmod on /var/run/screen
chmod 775 $TARGET/var/run/screen
chown root.utmp $TARGET/var/run/screen
# chmod /dev/dsp* /dev/mixer* /dev/video* /devvbi*
chmod 666 $TARGET/dev/dsp*
chmod 666 $TARGET/dev/mixer*
chmod 666 $TARGET/dev/video*
chmod 666 $TARGET/dev/vbi*
# Blake, 2004/02/06, /etc/mtab
rm -f $TARGET/etc/mtab
touch $TARGET/etc/mtab
# ln passwod to yppasswd
#mv $TARGET/usr/bin/passwd $TARGET/usr/bin/passwd.drbl
#ln -s /usr/bin/yppasswd $TARGET/usr/bin/passwd

# root's .kde
if [ "$LOCALE" = "zh_TW.Big5" ]; then
  #cp -a /root/.kde $TARGET/root
  #cp -a /root/.drbl $TARGET/root
  cp -a /etc/skel/.drbl $TARGET/root
  chown -R root.root $TARGET/root/.drbl
  ln -fs .drbl/.kde-mac $TARGET/root/.kde
fi

# root's .profile
cat <<-EOF > $TARGET/root/.profile
# ~/.profile: executed by Bourne-compatible login shells.

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/opt/drbl/sbin:/opt/drbl/bin
export PATH
mesg n

for i in /etc/profile.d/*.sh; do
  if [ -x \$i ]; then
    . \$i
  fi
done
EOF
# root's Desktop
mkdir -p $TARGET/root/Desktop
# home.desktop
cat <<-EOF > $TARGET/root/Desktop/home.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Home
Name[af]=Huis
Name[az]=Başlanğıc
Name[be]=Хатні
Name[bg]=Домашна директория
Name[br]=Er-gÃªr
Name[bs]=Početak
Name[ca]=Inici
Name[cs]=Můj adresář
Name[da]=Hjem
Name[de]=Persönliches Verzeichnis
Name[el]=Σπίτι
Name[eo]=Hejmo
Name[es]=Personal
Name[et]=Kodukataloog
Name[eu]=Etxea
Name[fa]=خانه
Name[fi]=Koti
Name[fr]=Dossier personnel
Name[gl]=Persoal
Name[he]=בית
Name[hr]=Početak
Name[hu]=Saját könyvtár
Name[id]=Rumah
Name[is]=Heimasvæðið þitt
Name[ja]=ホーム
Name[ko]=홈
Name[lo]=ພື້ນທີ່ສ່ວນຕົວ
Name[lt]=Pradžia
Name[lv]=Mājas
Name[mk]=Дома
Name[mt]=Direttorju Personali
Name[nb]=Hjem
Name[nl]=Persoonlijke map
Name[nn]=Heim
Name[nso]=Gae
Name[oc]=Inici
Name[pl]=Katalog domowy
Name[pt]=Casa
Name[ro]=Acasă
Name[ru]=Домой
Name[se]=Ruoktu
Name[sk]=Domov
Name[sl]=Domov
Name[sr]=Korisnički direktorijum
Name[sv]=Hem
Name[ta]=¦¾¡¼ì¸õ
Name[th]=พื้นที่ส่วนตัว
Name[tr]=Başlangıç
Name[uk]=Домівка
Name[ven]=Haya
Name[xh]=Ikhaya
Name[zh_CN]=起点
Name[zh_TW]=家目錄
Name[zu]=Ikhaya
GenericName=Personal Files
GenericName[af]=Persoonlike Lêers
GenericName[az]=Şəxsi Fayllar
GenericName[be]=Пэрсанальныя файлы
GenericName[bg]=Лични файлове
GenericName[br]=RestroÃ¹ deoc'h
GenericName[bs]=Osobne datoteke
GenericName[ca]=Fitxers personals
GenericName[cs]=Osobní soubory
GenericName[da]=Personlige filer
GenericName[de]=Eigene Dateien
GenericName[el]=Προσωπικά Αρχεία
GenericName[eo]=Personaj dosieroj
GenericName[es]=Archivos personales
GenericName[et]=Isiklikud failid
GenericName[eu]=Fitxategi Pertsonalak
GenericName[fa]=پرونده‌های شخصی
GenericName[fi]=Omat tiedostot
GenericName[fr]=Fichiers personnels
GenericName[gl]=Ficheiros Persoais
GenericName[he]=קבצים אישיים
GenericName[hr]=Osobne datoteke
GenericName[hu]=személyes fájlok
GenericName[id]=File Pribadi
GenericName[is]=Skrárnar þínar
GenericName[it]=File personali
GenericName[ja]=個人のファイル
GenericName[ko]=혼자만 쓰는 파일
GenericName[lo]=ທີ່ເກັບແຟ້ມແລະເອກະສານສວ່ນຕົວຫລືອື່ນຯ
GenericName[lt]=Asmeninės Bylos
GenericName[lv]=Personālie Faili
GenericName[mk]=Лична папка
GenericName[mt]=Fajls Personali
GenericName[nb]=Personlige filer
GenericName[nl]=persoonlijke bestanden
GenericName[nn]=Personlege filer
GenericName[nso]=Difaele tsa Botho
GenericName[oc]=FiquièRs personals
GenericName[pl]=Pliki osobiste
GenericName[pt]=Ficheiros Pessoais
GenericName[pt_BR]=Arquivos Pessoais
GenericName[ro]=Fişiere personale
GenericName[ru]=Личные файлы
GenericName[se]=Iežaš fiillat
GenericName[sk]=Osobné súbory
GenericName[sl]=Osebne datoteke
GenericName[sr]=Ovaj direktorijum sadrži vaše lične fajlove
GenericName[sv]=Personliga filer
GenericName[ta]=¦º¡ó¾ì §¸¡ôÒì¸û
GenericName[th]=ที่เก็บแฟ้มและเอกสารส่วนตัว หรืออื่นๆ
GenericName[tr]=Kişisel Dosyalar
GenericName[uk]=Особисті файли
GenericName[ven]=Dzifaela dza vhune
GenericName[vi]=File cá nhân 
GenericName[xh]=Iifayile Zobuqu
GenericName[zh_CN]=个人文件
GenericName[zh_TW]=個人檔案
GenericName[zu]=Amafayela Omuntu siqu
Exec=kfmclient openProfile filemanagement
Icon=kfm_home
Type=Application
EOF
# debian.desktop
cat <<-EOF > $TARGET/root/Desktop/debian.desktop
[Desktop Entry]
Icon=deb
Type=Link
URL=file:/etc/kde3/debian/debian.html
EOF
# webmin on root's desktop
cat <<-EOF > $TARGET/root/Desktop/webmin.desktop
[Desktop Entry]
Name=webmin
Icon=/usr/share/webmin/images/webmin.xpm
Type=Application
Exec=mozilla https://127.0.0.1:10000/
EOF
# cupsys on root's desktop
cat <<-EOF > $TARGET/root/Desktop/cupsys.desktop
[Desktop Entry]
Name=cupsys
Icon=/usr/share/icons/cups-tiny.xpm
Type=Application
Exec=mozilla http://127.0.0.1:631/
EOF
# Blake, 2004/01/28
for kernel_image in `ls /usr/src/kernel-image-*.deb`; do
  kversion=$kernel_image
  kversion=${kversion##/*/}
  kversion=${kversion%%_*}
  kversion=${kversion/kernel-image-/}
  if [ -e $TARGET/lib/modules/$kversion ]; then
    rm -rf $TARGET/lib/modules/$kversion
  fi
  dpkg --force-overwrite --root $TARGET -i $kernel_image < /dev/null
  chroot $TARGET /sbin/depmod -F /boot/System.map-$kversion -a $kversion
done
for kernel_headers in `ls /usr/src/kernel-headers-*.deb`; do
  dpkg --force-overwrite --root $TARGET -i $kernel_headers < /dev/null
done
for deb in `ls /usr/src/*.deb`; do
  deb=${deb##/*/}
  if [ "$(echo "$deb" | grep -e "^kernel-image-")" = "" -a \
       "$(echo "$deb" | grep -e "^kernel-headers-")" = "" ]; then
    dpkg --force-overwrite --root $TARGET -i /usr/src/$deb < /dev/null
  fi
done
chroot $TARGET /sbin/depmod -F /boot/System.map-$kernel_version -a $kernel_version

APPEND=""

## Blake, 2004/04/02, SCSI HD
case "$HDCHOICE" in sd?)
  
  for vmlinuz in `ls $TARGET/vmlinuz-*`; do
    vmlinuz=${vmlinuz##/*/}
    version=${vmlinuz/vmlinuz-/}
 
    initrd="$TARGET/tmp/initrd-$version.$$"
    initrdimg=`mktemp $TARGET/tmp/initrd-$version.img.XXXXXX`
    initrdmnt="$TARGET/tmp/initrd-$version.mnt.$$"
    mkdir -p $initrd
    mkdir -p $initrdmnt
    mkdir -p $initrd/dev
    mkdir -p $initrd/etc
    mkdir -p $initrd/bin
    mkdir -p $initrd/lib
    mkdir -p $initrd/lib/modules
    mkdir -p $initrd/proc
    mkdir -p $initrd/sysroot
    mkdir -p $initrd/initrd
    ln -s bin $initrd/sbin
    rsync -a -v /initrd/bin/* $initrd/bin &> /dev/null

    ## insert-modules
    for SCSI_MOD in `/sbin/discover --module scsi`; do
      if [ "$SCSI_MOD" = "ide-scsi" ]; then continue; fi
      /opt/drblcd/include-modules -d $initrd/lib/modules -k $version $SCSI_MOD > $initrd/bin/insert-modules
    done
    cp /lib/modules/$version/kernel/drivers/scsi/sd_mod.o $initrd/lib/modules
    echo "insmod sd_mod" >> $initrd/bin/insert-modules
    chmod 755 $initrd/bin/insert-modules

    ### linuxrc
    cat <<-EOF > $initrd/linuxrc
#!/bin/sh
PATH=/sbin:/bin

echo Busybox /linuxrc starting

echo Mounting /proc filesystem
mount -t proc none /proc

echo Inserting modules
/bin/insert-modules

echo Umounting /proc
umount /proc
EOF
    chmod 755 $initrd/linuxrc

    dd if=/dev/zero bs=1k of=$initrdimg count=$((`du -sk $initrd | cut -f1` * 7 / 6)) 2> /dev/null
    /sbin/mke2fs -q -F $initrdimg 2> /dev/null
    mount -o loop $initrdimg $initrdmnt
    cp -a $initrd/* $initrdmnt/
    mknod $initrdmnt/dev/console c 5 1
    mknod $initrdmnt/dev/null c 1 3
    mknod $initrdmnt/dev/ram b 1 1
    mknod $initrdmnt/dev/systty c 4 0
    mknod $initrdmnt/dev/random c 1 8
    mknod $initrdmnt/dev/urandom c 1 9
    for i in 1 2 3 4; do mknod $initrdmnt/dev/tty$i c 4 $i; done
    umount -d $initrdmnt ; losetup -d /dev/loop0
    gzip -9 -n -c $initrdimg > $TARGET/boot/initrd-$version.img
    ln -fs boot/initrd-$version.img $TARGET/initrd-$version.img

    # cleanup
    rm -rf $initrd
    rm -f $initrdimg
    rmdir $initrdmnt
  done

  default_kernel=`ls -al --time-style=long-iso $TARGET/vmlinuz | awk '{ print $10; }'`
  if [ "$default_kernel" != "" ]; then 
    version=${default_kernel/boot\//}
    version=${version/vmlinuz-/}
    ln -fs boot/initrd-$version.img $TARGET/initrd.img
  fi

;; esac

## Blake, 2004/02/07, lilo & grub
#USELILO=1

if [ -f /proc/partitions ] ; then
  while read x x x p x
  do
    case "$p" in
      hd?)
        if [ "`cat /proc/ide/$p/media`" = "cdrom" ] ; then
          APPEND="$p=ide-scsi $APPEND"
        fi
        ;;
      *) ;;
    esac
  done < /proc/partitions
fi

## Blake, 2004/06/04, check acpi
if [ ! -d /proc/acpi ]; then
  APPEND="$APPEND acpi=off"
fi

## try to install GRUB
if [ $USEGRUB -eq 1 ]; then
  USELILO=1
  /sbin/grub-install --root-directory=$TARGET --no-floppy $BOOTDEVICE
fi

if [ -e $TARGET/boot/grub/device.map ]; then
  # use grub
  GRUBDEV=""
  while read grubdev osdev; do
  if [ "$osdev" = "$BOOTDEVICE" ]; then
    GRUBDEV=$grubdev
    GRUBDEV=${GRUBDEV/(/}
    GRUBDEV="${GRUBDEV/)/}"
    break
  fi
  done < $TARGET/boot/grub/device.map

  if [ "$GRUBDEV" != "" ]; then

    default_kernel=`ls -al --time-style=long-iso $TARGET/vmlinuz | awk '{ print $10; }'`
    if [ "$default_kernel" = "" ]; then 
      default_kernel="vmlinuz"
      default_kernel_title="Debian"
    else
      version=${default_kernel/boot\//}
      version=${version/vmlinuz-/}
      default_kernel_title="Debian ($version)"
    fi

    cp /usr/share/icons/grub-debian.xpm.gz $TARGET/boot/grub/
    cat <<-EOF > $TARGET/boot/grub/menu.lst
default=0
timeout=10
splashimage=($GRUBDEV,0)/boot/grub/grub-debian.xpm.gz
title $default_kernel_title
  root ($GRUBDEV,0)
  kernel /$default_kernel ro root=$ROOTPART $APPEND
EOF
    if [ -f $TARGET/initrd-$version.img ]; then
      echo "  initrd /initrd-$version.img" >> $TARGET/boot/grub/menu.lst
    fi

    ## more kernels
    default_kernel=${default_kernel/boot\//}
    for vmlinuz in `ls $TARGET/vmlinuz-*`; do
     vmlinuz=${vmlinuz##/*/}
     version=${vmlinuz/vmlinuz-/}
     if [ "$vmlinuz" = "$default_kernel" ]; then continue; fi

     cat <<-EOF >> $TARGET/boot/grub/menu.lst
title Debian ($version)
  root ($GRUBDEV,0)
  kernel /boot/vmlinuz-$version ro root=$ROOTPART $APPEND
EOF
     if [ -f $TARGET/initrd-$version.img ]; then
       echo "  initrd /initrd-$version.img" >> $TARGET/boot/grub/menu.lst
     fi
    done

    ## grub install successful ..
    ## turn off lilo configuration
    USELILO=0
  fi
fi

## lilo
if [ $USELILO -eq 1 ]; then
  ## lilo.conf

  default_kernel=`ls -al --time-style=long-iso $TARGET/vmlinuz | awk '{ print $10; }'`
  if [ "$default_kernel" = "" ]; then 
    default_kernel="vmlinuz"
    default_kernel_title="Debian"
  else
    version=${default_kernel/boot\//}
    version=${version/vmlinuz-/}
    default_kernel_title="$version"
  fi

  cat <<-EOF > $TARGET/etc/lilo.conf
lba32
boot=$BOOTDEVICE
root=$ROOTPART
install=/boot/boot-menu.b
map=/boot/map
#prompt
delay=10
vga=normal
default=$default_kernel_title

image=/$default_kernel
  label=$default_kernel_title
  read-only
EOF
  if [ "$APPEND" != "" ]; then
    echo "  append=\"$APPEND\"" >> $TARGET/etc/lilo.conf
  fi
  if [ -f $TARGET/initrd-$version.img ]; then
    echo "  initrd=/initrd-$version.img" >> $TARGET/etc/lilo.conf
  fi

  # Blake, 2003/12/31, more kernels
  default_kernel=${default_kernel/boot\//}
  for vmlinuz in `ls $TARGET/vmlinuz-*`; do
    vmlinuz=${vmlinuz##/*/}
    version=${vmlinuz/vmlinuz-/}
    if [ "$vmlinuz" = "$default_kernel" ]; then continue; fi
    cat <<-EOF >> $TARGET/etc/lilo.conf

image=/$vmlinuz
  label=$version
  read-only
EOF
    if [ "$APPEND" != "" ]; then
      echo "  append=\"$APPEND\"" >> $TARGET/etc/lilo.conf
    fi
    if [ -f $TARGET/initrd-$version.img ]; then
      echo "  initrd=/initrd-$version.img" >> $TARGET/etc/lilo.conf
    fi
  done

  ### execute bootloader: lilo
  lilo -r $TARGET
fi

##
## network configuration
##
MSG_BIG5="\nzO_n]wsu ?"
MSG_EN="\nDo you want to do network configuration ?"
if [ "$LOCALE" = "zh_TW.Big5"  ]; then MSG="$MSG_BIG5"
else MSG="$MSG_EN"; fi
$DIA --backtitle "$backtitle" --title "$title" --yesno "$MSG" 8 60

if [ $? -eq 0 ]; then
  configure_network $TARGET
fi

### check if the environment variable "ONLY_DEBIAN" or "DEBIAN_ONLY" is set
if [ "$ONLY_DEBIAN" = "1" -o "$DEBIAN_ONLY" = "1" ]; then

  # Blake, 2003/11/24, move to S80setup
  #cat <<-EOF > $TARGET/sbin/setup.sh
  cat <<-EOF > $TARGET/etc/rc2.d/S80setup
#!/bin/sh
echo "============= DRBL Wizard ================"
# root password
run=1
while [ \$run -eq 1 ]; do 
  echo "Please enter root password"
  passwd root
  rootpwd=\`grep root /etc/passwd | cut -d":" -f2\`
  if [ "\$rootpwd" != "" ]; then 
    run=0
  else
    echo "ERROR: The password is empty..Plase do it again!!"
  fi
done
# webmin
echo "root:x:0::" > /etc/webmin/miniserv.users
# shadowconfig on
/usr/sbin/shadowconfig on
#rm -f /sbin/setup.sh
rm -f /etc/rc2.d/S80setup
echo "=============== END ====================="
EOF
  #chmod 755 $TARGET/sbin/setup.sh
  chmod 755 $TARGET/etc/rc2.d/S80setup

  # housekeeping
  rm -rf $TARGET/opt/drblcd
  rm $TMP
  umount $TARGET/home
  umount $TARGET/usr
  umount $TARGET/var
  umount $TARGET/

  sync

  if [ "$HAS_POSTPROCESSING" = "1" ]; then
    echo
  else
    # end
    MSG_BIG5="P±z DRBL\n\n\
YD, ziHb\n\
  http://drbl.nchc.org.tw/faq \n\
T,\n\n\
Ϊ̬O email \n\
  blake@nchc.org.tw  steven@nchc.org.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.org.tw/faq\n\
or email to \n\
  blake@nchc.org.tw and steven@nchc.org.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
  fi

  exit 0
fi

##
## Install DRBL
##
echo "Installing DRBL ..."

# drbl package
mkdir -p $TARGET/opt/drbl/bin
mkdir -p $TARGET/opt/drbl/sbin

drbl_script_deb=`ls /opt/drblcd/pkg/drbl-script*.deb`
for deb in `ls /opt/drblcd/pkg/drbl*.deb`; do
  if [ "$deb" = "$drbl_script_deb" ]; then continue; fi
  dpkg --root $TARGET --install $deb
done
dpkg --root $TARGET --install $drbl_script_deb

# drbl client template
DRBLROOT=$TARGET/var/lib/diskless/default/root
mkdir -p $DRBLROOT
export GPL=0; chroot $TARGET /opt/drbl/sbin/drblsrv --imageroot-only

set_non_interactive "$DRBLROOT" "true"

# Blake, 2004/01/02
for kernel_image in `ls /usr/src/kernel-image-*.deb`; do
  kversion=$kernel_image
  kversion=${kversion##/*/}
  kversion=${kversion%%_*}
  kversion=${kversion/kernel-image-/}
  if [ -e $DRBLROOT/lib/modules/$kversion ]; then
    rm -rf $DRBLROOT/lib/modules/$kversion
  fi
  dpkg --force-overwrite --root $DRBLROOT -i $kernel_image < /dev/null
  chroot $DRBLROOT /sbin/depmod -F /boot/System.map-$kversion -a $kversion
done
for kernel_headers in `ls /usr/src/kernel-headers-*.deb`; do
  dpkg --force-overwrite --root $DRBLROOT -i $kernel_headers < /dev/null
done
for deb in `ls /usr/src/*.deb`; do
  deb=${deb##/*/}
  if [ "$(echo "$deb" | grep -e "^kernel-image-")" = "" -a \
       "$(echo "$deb" | grep -e "^kernel-headers-")" = "" ]; then
    dpkg --force-overwrite --root $DRBLROOT -i /usr/src/$deb < /dev/null
  fi
done

# the command should be executed in postinst of bcm4401, sis7019
chroot $DRBLROOT /sbin/depmod -F /boot/System.map-$kernel_version -a $kernel_version

set_non_interactive "$DRBLROOT" "false"

# drbl network boot image
cat <<-EOF > $TARGET/root/drblpush.sh
#!/bin/sh
export GPL=0; /opt/drbl/sbin/drblpush --initrd-only
EOF
chmod 755 $TARGET/root/drblpush.sh
mount -t proc proc $TARGET/proc
chroot $TARGET /root/drblpush.sh 
umount $TARGET/proc
rm -f $TARGET/root/drblpush.sh

##
## next time startup ...
##
# Blake, 2004/01/29
ln -fs ../init.d/rebuildfstab $TARGET/etc/rcS.d/S90rebuildfstab
ln -fs ../init.d/rebuildfstab $DRBLROOT/etc/rcS.d/S90rebuildfstab

cp /opt/drblcd/pkg/* $TARGET/var/cache/apt/archives
cp /opt/drblcd/pkg/* $DRBLROOT/var/cache/apt/archives
rm -rf $TARGET/opt/drblcd

#cat <<-EOF > $TARGET/sbin/setup.sh
cat <<-EOF > $TARGET/etc/rc2.d/S80setup
#!/bin/sh
echo "============= DRBL Wizard ================"
dpkg --configure -a
export GPL=0; yes "" | /opt/drbl/sbin/drblsrv --services-only
dpkg --configure -a
# root password
run=1
while [ \$run -eq 1 ]; do 
  echo "Please enter root password"
  passwd root
  rootpwd=\`grep root /etc/passwd | cut -d":" -f2\`
  if [ "\$rootpwd" != "" ]; then 
    run=0
  else
    echo "ERROR: The password is empty..Plase do it again!!"
  fi
done
cp /etc/passwd /var/lib/diskless/default/root/etc
# webmin
echo "root:x:0::" > /etc/webmin/miniserv.users
chroot /var/lib/diskless/default/root dpkg --purge webmin
# shadowconfig on
/usr/sbin/shadowconfig on
chroot /var/lib/diskless/default/root /usr/sbin/shadowconfig on
rm -f /etc/rc2.d/S80setup
echo "=============== END ====================="
EOF
#chmod 755 $TARGET/sbin/setup.sh
chmod 755 $TARGET/etc/rc2.d/S80setup

rm $TMP
umount $TARGET/home
umount $TARGET/usr
umount $TARGET/var
umount $TARGET/

sync

# end
if [ "$HAS_POSTPROCESSING" = "1" ]; then
  echo
else
  MSG_BIG5="P±z DRBL\n\n\
YD, ziHb\n\
  http://drbl.nchc.org.tw/faq \n\
T,\n\n\
Ϊ̬O email \n\
  blake@nchc.org.tw  steven@nchc.org.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.org.tw/faq\n\
or email to \n\
  blake@nchc.org.tw and steven@nchc.org.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
fi

exit 0
