
BINARY_OUTNAME=drblsrv-$$.zip

usage() {
  echo "Usage:"
  echo "  $0                   both services and client template are setuped"
  echo "  $0 --services-only   install and setup services only"
  echo "  $0 --imageroot-only  generate client template only"
  echo "  $0 --help            this message"
}

############################################################################
##                                                                         #
##   This program is free software; you can redistribute it and/or modify  #
##   it under the terms of the GNU General Public License as published by  #
##   the Free Software Foundation; either version 2 of the License, or     #
##   (at your option) any later version.                                   #
##                                                                         #
############################################################################

# install some packages before installation
apt-get -y install expect zip unzip util-linux rsync sudo > /dev/null

outname=$BINARY_OUTNAME
tmpdir=drblsrv.$$
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
#

## parse option
b_services=1
b_imageroot=1
case "$1" in
  ### usage
  "-h"|"--help") usage ; exit 0 ;;
  ### imageroot only
  "--imageroot-only") b_services=0; b_imageroot=1 ;;
  ### services only
  "--services-only") b_services=1; b_imageroot=0 ;;
  ### all
  "") b_services=1 ; b_imageroot=1 ;;
  *) echo "Invaild options $1" ; exit 0 ;;
esac

#echo "Extracting..."
if [ -x /usr/bin/unzip ]; then
  /usr/bin/unzip $outname -d drblsrv.$$ > /dev/null
else
  echo "Command '/usr/bin/unzip' not found. Please check the package you installed."
  echo "Abort!!"
  exit 0
fi

# GPL 
if [ "$GPL" = "" ]; then
  more drblsrv.$$/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 drblsrv.$$
        exit 1;;
    esac
  done
fi

#echo "Setuping..."

export LC_ALL=C
drblsrv.$$/autodebconf_readline
# imageroot
if [ $b_imageroot -eq 1 ]; then
  . drblsrv.$$/imageroot/install-imageroot.sh
  . drblsrv.$$/imageroot/setup-imageroot.sh
fi
# services
if [ $b_services -eq 1 ]; then
  for i in drblsrv.$$/services/*.sh; do
    if [ -x $i ]; then
      . $i
    fi
  done
fi
# NO_DRBLMGRD
echo "NO_DRBLMGRD=\"false\"" > /etc/default/drbl
#echo "Done."
rm -f $outname
rm -rf drblsrv.$$
exit 0
