
BINARY_OUTNAME=drblpush-$$.zip

usage() {
  echo "Usage:" 
  echo "  $0                   setup DRBL"
  echo "  $0 --force           setup DRBL force"
  echo "  $0 --auto            setup DRBL automatically"
  #echo "  $0 --add             add DRBL Client automatically"
  echo "  $0 --drblconf        show the DRBL configuration"
  echo "  $0 --initrd-only [kernel version]  generate the network boot kernel"
  echo "  $0 --collect-mac     collect mac address"
  echo "  $0 --deploy          deploy the DRBL client"
  echo "  $0 --help            this message"
  exit 0
}

print_choice() {
  for eth in `ls drblpush.$$`
  do
    if [ "$eth" = "drbl_config" ]; then continue; fi
    eth=${eth/macadr-/}
    eth=${eth/\.txt/}
    echo "$eth    : view the MAC address of DRBL clients through $eth"
    echo "s_$eth  : save the MAC address to the file macadr-$eth.txt"

    # 2003/08/15 "edit_public_ip"
    imageroot="/var/lib/diskless/default/root"
    ip=$(/sbin/ifconfig $eth | awk '/inet/{ print $2; }' | cut -d: -f2)
    netmask=$(/sbin/ifconfig $eth | awk '/Mask/{ print $4; }' | cut -d: -f2)
    network=$(/usr/bin/netmask $ip/$netmask | sed "s/ //g" | cut -d/ -f1)
    if [ -f $imageroot/etc/diskless-image/public_ip.$network ]; then
      echo "ep_$eth : edit public ip of DRBL clients through $eth"
    fi
  done
  echo "Q(q)   : quit"
  echo -n "Enter your choice [Q]: "
}

## main
case "$1" in
  ### usage
  "-h"|"--help") usage ;;
  ### deploy the DRBL clients
  "--deploy")
    if [ -f /opt/drbl/sbin/drbl_deploy.sh ]; then
      /opt/drbl/sbin/drbl_deploy.sh
    else
      echo "You should configure Penguinzilla (DRBL) first"
      echo "Use the command: \"$0\" instead."
    fi
    exit 0
    ;;
  ### show drbl configuration
  "--drblconf")
    if [ -f /opt/drbl/sbin/drbl_deploy.sh ]; then
      /opt/drbl/sbin/drbl_deploy.sh macadr
      # out 
      eth=$(/sbin/route -n | grep -e "^0.0.0.0" | awk '{ print $8 }')
      echo 
      echo "The sketch diagram of DRBL configuration"
      echo "     +---------------+"
      echo "     |  DRBL Server  |"
      echo "     |               +-- OUT ($eth)"    
      # drbl
      for eth in `ls drblpush.$$`
      do
        if [ "$eth" = "drbl_config" ]; then continue; fi
        eth=${eth/macadr-/}
        eth=${eth/\.txt/}
        numclients=`wc -l drblpush.$$/macadr-$eth.txt | awk '{ print $1 }'`
        echo "     |               |"
        echo "     |               +-- DRBL Client ($eth): $numclients"
      done
      echo "     |               |"
      echo "     +---------------+"
      print_choice
      while read ANS; do
        if [ "$ANS" = "" -o "$ANS" = "Q" -o "$ANS" = "q" ]; then
          rm -rf drblpush.$$
          exit 0
        fi
        for eth in `ls drblpush.$$`
        do
          eth=${eth/macadr-/}
          eth=${eth/\.txt/}
          if [ "$ANS" = "s_$eth" ]; then
            cp drblpush.$$/macadr-$eth.txt .
          elif [ "$ANS" = "$eth" ]; then
            echo "===== $eth ====="
            cat drblpush.$$/macadr-$eth.txt
            echo "================"
          # 2003/08/15 edit public ip
          elif [ "$ANS" = "ep_$eth" ]; then
            imageroot="/var/lib/diskless/default/root"
            ip=$(/sbin/ifconfig $eth | awk '/inet/{ print $2; }' | cut -d: -f2)
            netmask=$(/sbin/ifconfig $eth | awk '/Mask/{ print $4; }' | cut -d: -f2)
            network=$(/usr/bin/netmask $ip/$netmask | sed "s/ //g" | cut -d/ -f1)
            if [ -f $imageroot/etc/diskless-image/public_ip.$network ]; then
              nano $imageroot/etc/diskless-iamge/public_ip.$network
            fi
          fi
        done
        print_choice
      done
    else
      echo "You should configure Penguinzilla (DRBL) first"
      echo "Use the command: \"$0\" instead."
    fi
    ;;
#  "--auto"|"--add"|"--force") ;;
  "--auto"|"--force") ;;
  "--initrd-only"|"--pxecfg") ;;
  "--client-switch") ;;
  "--collect-mac"|"--routing-table") ;;
  "") ;;
  *) echo "Invalid option: $0 $1"; usage ;;
esac

# install some packages before installation
apt-get -y install zip unzip > /dev/null
apt-get -y install diskless > /dev/null
apt-get -y install sysutils > /dev/null
apt-get -y install perl > /dev/null
apt-get -y install gcc > /dev/null
apt-get -y install netmask > /dev/null
apt-get -y install libc6-dev > /dev/null
apt-get -y install libpcap-dev > /dev/null

imageroot="/var/lib/diskless/default/root"
outname=$BINARY_OUTNAME
tmpdir=drblpush.$$
trap 'rm -f $outname; rm -rf $tmpdir; exit 1' HUP INT QUIT TERM
#echo "Unpacking..."
tail +$BINARY_LINENO $0 > $outname
#if [ -x /usr/bin/sum ]; then
#    echo "Checksumming..."
#
#    sum=`/usr/bin/sum $outname`
#    index=1
#    for s in $sum
#    do
#    case $index in
#    1)  sum1=$s;
#        index=2;
#        ;;
#    2)  sum2=$s;
#        index=3;
#        ;;
#    esac
#    done
#    if expr $sum1 != $BINARY_BSD_CHECKSUM1 ||
#       expr $sum2 != $BINARY_BSD_CHECKSUM2 ; then
#      echo "The download file appears to be corrupted."
#      exit 1
#    fi
#else
#    echo "Can't find /usr/bin/sum to do checksum. Continuing anyway."
#fi
#
#echo "Extracting..."

if [ -x /usr/bin/unzip ]; then
  /usr/bin/unzip $outname -d drblpush.$$ > /dev/null
  gcc -o drblpush.$$/btod drblpush.$$/btod.c -lm
  gcc -o drblpush.$$/dtob drblpush.$$/dtob.c -lm
fi

# GPL 
if [ "$GPL" = "" ]; then
  more drblpush.$$/GPL
  agreed=
  while [ x$agreed = x ]; do
    echo
    echo "Do you agreed to the above license (GPL) terms? [yes or no] "
    read reply leftover
    case $reply in
      y* | Y*)
        agreed=1;;
      n* | N*)
        echo "If you don't agree to the license you can't install this software"
        rm -f $outname
        rm -rf drblpush.$$
        exit 1;;
    esac
  done
fi

## create_netboot_image
create_netboot_image() {

  ## pxe
  apt-get -y install syslinux
  cp /usr/lib/syslinux/pxelinux.0 /tftpboot
  mkdir -p /tftpboot/pxelinux.cfg
  cat <<-EOF > /tftpboot/pxelinux.cfg/default
default drbl
timeout 0
prompt 0
say **********************************************
say Welcome to DRBL.
say NCHC Free Software Labs, Taiwan.
say http://drbl.nchc.org.tw; http://drbl.sf.net
say **********************************************
say Boot:
say ----------------------------------------------
say (Enter) System assigned by DRBL server (default)
say (1) Linux (from DRBL server)
say (2) System in local harddrive (if available)
say (3) Memtest
say (4) Freedos
say 5 options, what's your choice ? [Enter, 1, 2, 3, 4]:

label drbl
  kernel vmlinuz
  append initrd=initrd-pxe_pciscan.img ramdisk_size=9600

label 1
  kernel vmlinuz
  append initrd=initrd-pxe_pciscan.img ramdisk_size=9600

label local
  localboot 0

label 2
  localboot 0

EOF
  ## dhcpd.conf.pxelinux.include
  cat <<-EOF > /etc/dhcp3/dhcpd.conf.pxelinux.include
if substring (option vendor-class-identifier, 0, 9) = "PXEClient" {
   filename "pxelinux.0";
}
EOF

  ## etherboot client-switch
  touch /etc/dhcp3/dhcpd.conf.client-switch.include
  chmod 600 /etc/dhcp3/dhcpd.conf.client-switch.include
 
  ## create network boot image including pxe and etherboot
  for kernel_ver in `ls $imageroot/lib/modules/`; do
    if [ -L $imageroot/lib/modules/$kernel_ver/modules.dep ]; then
      rm -f $imageroot/lib/modules/$kernel_ver/modules.*
      cp $imageroot/etc/modules_dep/$kernel_ver/modules.* $imageroot/lib/modules/$kernel_ver/
    fi
    . drblpush.$$/mkinitrd-net.sh $kernel_ver
  done
  . drblpush.$$/mkmemtest86+.sh
  . drblpush.$$/mkfreedos.sh
  . drblpush.$$/mknetinst.sh

  ## modules.dep
  mkdir -p $imageroot/etc/modules_dep
  for kernel_ver in `ls $imageroot/lib/modules`; do
    mkdir -p $imageroot/etc/modules_dep/$kernel_ver
    mv $imageroot/lib/modules/$kernel_ver/modules.* $imageroot/etc/modules_dep/$kernel_ver
    for modules_dep in `ls $imageroot/etc/modules_dep/$kernel_ver/modules.*`; do
      modules_dep=${modules_dep##/*/}
      ln -fs /etc/modules_dep/$kernel_ver/$modules_dep $imageroot/lib/modules/$kernel_ver/$modules_dep
    done
  done

  ##
  cp drblpush.$$/drbl /etc/init.d
  ln -fs ../init.d/drbl /etc/rc2.d/S18drbl
  /etc/init.d/drbl start no_routing_table
}

### the option after extracting
case "$1" in
  ## collect-mac
  "--collect-mac")
    . drblpush.$$/collect-mac.sh
    ;;
  ## routing-table
  "--routing-table")
    . drblpush.$$/setup-drbl-routing.sh $2
    ;;
  ## try to fix 2nd dhcp request failed when using acpi
  "--pxecfg")
    . drblpush.$$/pxecfg.sh $2
    ;;
  "--client-switch")
    . drblpush.$$/client-switch.sh $2
    ;;
  "--initrd-only")
    kernel_ver="$2"
    if [ "$kernel_ver" = "" ]; then kernel_ver=`uname -r`; fi
    if [ ! -d $imageroot/lib/modules/$kernel_ver ]; then
      echo "No such kernel ($kernel_ver) in Penguinzilla (DRBL) Client Template"
    else
      create_netboot_image
    fi
    ;;
  ## auto configuration
  "--auto")
    if [ ! -e /tftpboot/mkinitrd-net ]; then
      create_netboot_image
    fi
    . drblpush.$$/setup-drbl-auto.sh
    ;;
  ## auto detect new one 
#  "--add")
#    if [ ! -e /tftpboot/mkinitrd-net ]; then
#      create_netboot_image
#    fi
#    drblpush.$$/setup-drbl-add.sh
#    ;;
  ## --force or no option
  ""|"--force")
    if [ ! -e /tftpboot/mkinitrd-net ]; then
      create_netboot_image
    fi
    . drblpush.$$/setup-drbl.sh $1
    ;;
  *) echo "Invalid option: $0 $1"; usage ;;
esac

rm -f $outname
rm -rf drblpush.$$
exit 0
