#!/bin/sh

#
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#   A wrapper of apt-get for drbl client template
#   right now only 'install' 'update' 'remove' are provided
#
#   2003/11/23, use "deb" way to upgrade drbl

imageroot=/var/lib/diskless/default/root
hostroot=/var/lib/diskless/default

# 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

# do_debconf
do_debconf() {
  frontend="$(echo "$1" | awk -F, '{ print $1; }')"
  priority="$(echo "$1" | awk -F, '{ print $2; }')"
  case "$frontend" in
    "readline")       itf="2" ;;
    "dialog")         itf="1" ;;
    "noninteractive") itf="5" ;;
    *)                echo "invalid frontend: $1 .. Abort!" ; abort ;;
  esac
  case "$priority" in
    "critical") pri="1" ;;
    "high")     pri="2" ;;
    "medium")   pri="3" ;;
    "low")      pri="4" ;;
    *)          pri="3"  ;;
  esac
  cat <<-EOF > $imageroot/root/autodebconf
#!/bin/sh
# \\
exec expect "\$0" \${1+"\$@"}
spawn /usr/sbin/dpkg-reconfigure --frontend=readline debconf
expect "What interface should be used for configuring packages?"
send "$itf\r"
#expect "Ignore questions with a priority less than.."
expect "See only questions that are of what priority and higher?"
send "$pri\r"
expect "Show all old questions again and again?"
send "no\r"
expect eof
EOF
  chmod 755 $imageroot/root/autodebconf
  chroot $imageroot /root/autodebconf
  rm -f $imageroot/root/autodebconf
}

# abort
abort() {
  do_debconf "dialog,medium"
  exit 0
}

# main
command="";
command_option=""
packages_name=""
debconf_frontend=0

while [ ! -z "$1" ]; do
  case "$1" in
    "install"|"remove"|"update") command="$1" ;;
    -*|--*) 
       case "$1" in
        "--frontend") shift; do_debconf "$1" ; debconf_frontend=1 ;;
        *) command_option="$command_option $1" ;;
       esac
       ;;
    *) 
       if [ -z "$command" ]; then 
         ## invalid options
         echo "invalid options: $1 .. Abort!"
         abort
       else
         ## package_name
         case "$1" in 
           "drbl")      
              apt-get update
              apt-get -y install drbl-patch
              if [ -f /opt/drbl/sbin/drbl-patch ]; then
                /opt/drbl/sbin/drbl-patch
              fi
              ;;
            *) packages_name="$packages_name $1" ;;
         esac
       fi
       ;;
  esac
  shift
done
if [ "$packages_name" = "" ]; then exit; fi

## function: task_install
## $1 command_option
## $2 packages_name
task_install() {

  command_option="$1"
  packages_name="$2"

  # chroot - install the package
  cat <<-EOF > $imageroot/root/drbl-install.sh
apt-get clean
apt-get update
apt-get $command_option install $packages_name
EOF
  chmod 755 $imageroot/root/drbl-install.sh
  mount -t proc proc $imageroot/proc
  chroot $imageroot /root/drbl-install.sh
  umount $imageroot/proc
  rm -f $imageroot/root/drbl-install.sh

  # mkdir 
  for package in `ls $imageroot/var/cache/apt/archives/*.deb 2> /dev/null`
  do
    for file in `dpkg-deb -c $package | awk '{ print $6; }' | grep -e "^./etc/"`
    do
     if [ -d $imageroot/$file ]; then
       for host in `ls $hostroot`
       do
         if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ];
         then
	       mkdir -p $hostroot/$host/$file
	     fi
       done
     fi
    done
    for file in `dpkg-deb -c $package | awk '{ print $6; }' | grep -e "^./var/"`
    do
     if [ -d $imageroot/$file ]; then
       for host in `ls $hostroot`
       do
        if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ];
        then
          mkdir -p $hostroot/$host/$file
	    fi
       done
     fi
    done
  done
  # copy files
  for package in `ls $imageroot/var/cache/apt/archives/*.deb 2> /dev/null`
  do
    for file in `dpkg-deb -c $package | awk '{ print $6; }' | grep -e "^./etc/"`
    do
      if [ ! -d $imageroot/$file ]; then
        for host in `ls $hostroot`
        do
          if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ];
          then
	        cp -a $imageroot/$file $hostroot/$host/$file
	      fi
        done
      fi
    done
    for file in `dpkg-deb -c $package | awk '{ print $6; }' | grep -e "^./var/"`
    do
      if [ ! -d $imageroot/$file ]; then
        for host in `ls $hostroot`
        do
          if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ];
          then
            cp -a $imageroot/$file $hostroot/$host/$file
          fi
        done
      fi
    done
  done

  # some services, such as nis, ntpdate will generate a file in /etc/default
  # how to handle this problem ?? 
  # Blake, 2004/02/16, not a good idea, just try to copy new one in
  # /etc/default to all the clients
  for etc_default in `ls $imageroot/etc/default`; do
    for host in `ls $hostroot`; do
      if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ]; then
        if [ ! -e $hostroot/$host/etc/default/$etc_default ]; then
          cp -a $imageroot/etc/default/$etc_default $hostroot/$host/etc/default/
        fi
      fi
    done
  done
  # file in rcS.d
  for etc_rcS in `ls $imageroot/etc/rcS.d`; do
    for host in `ls $hostroot`; do
      if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ]; then
        if [ ! -e $hostroot/$host/etc/rcS.d/$etc_rcS ]; then
          cp -a $imageroot/etc/rcS.d/$etc_rcS $hostroot/$host/etc/rcS.d/
        fi
      fi
    done
  done
  # file in rc2.d 
  for etc_rc2 in `ls $imageroot/etc/rc2.d`; do
    for host in `ls $hostroot`; do
      if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ]; then
        if [ ! -e $hostroot/$host/etc/rc2.d/$etc_rc2 ]; then
          cp -a $imageroot/etc/rc2.d/$etc_rc2 $hostroot/$host/etc/rc2.d/
        fi
      fi
    done
  done   
  # file in alternatives
  for etc_alternatives in `ls $imageroot/etc/alternatives`; do
    for host in `ls $hostroot`; do
      if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ]; then
        if [ ! -e $hostroot/$host/etc/alternatives/$etc_alternatives ]; then
          cp -a $imageroot/etc/alternatives/$etc_alternatives $hostroot/$host/etc/alternatives/
        fi
      fi
    done
  done
}

##
case "$command" in
 "binstall")

   # nameserver & sources.list
   cp $imageroot/etc/resolv.conf $imageroot/etc/resolv.conf.bak
   cp /etc/resolv.conf $imageroot/etc/resolv.conf
   cp $imageroot/etc/apt/sources.list $imageroot/etc/apt/sources.list.bak
   cp /etc/apt/sources.list $imageroot/etc/apt/sources.list

   # task_install
   task_install "$command_option" "$packages_name"

   # 
   for pkg in `ls $imageroot/var/cache/apt/archives/*.deb`; do
     pkg="${pkg##*/}"
     ln -fs $imageroot/var/cache/apt/archives/$pkg /var/cache/apt/archives/$pkg
   done

   apt-get $command_option install $packages_name

   for pkg in `ls $imageroot/var/cache/apt/archives/*.deb`; do
     pkg="${pkg##*/}"
     rm -f $imageroot/var/cache/apt/archives/$pkg
   done  

   # chroot - remove the package in /var/cache/apt/archives
   mount -t proc proc $imageroot/proc
   chroot $imageroot apt-get clean
   umount $imageroot/proc

   # nameserver & sources.list 
   mv $imageroot/etc/resolv.conf.bak $imageroot/etc/resolv.conf
   mv $imageroot/etc/apt/sources.list.bak $imageroot/etc/apt/sources.list

   ;;
 "install")

   # nameserver & sources.list
   cp $imageroot/etc/resolv.conf $imageroot/etc/resolv.conf.bak
   cp /etc/resolv.conf $imageroot/etc/resolv.conf
   cp $imageroot/etc/apt/sources.list $imageroot/etc/apt/sources.list.bak
   cp /etc/apt/sources.list $imageroot/etc/apt/sources.list

   # task_install
   task_install "$command_option" "$packages_name"
  
   # chroot - remove the package in /var/cache/apt/archives
   mount -t proc proc $imageroot/proc
   chroot $imageroot apt-get clean
   umount $imageroot/proc

   # nameserver & sources.list 
   mv $imageroot/etc/resolv.conf.bak $imageroot/etc/resolv.conf
   mv $imageroot/etc/apt/sources.list.bak $imageroot/etc/apt/sources.list
   ;;
 "remove")
   for package_name in $packages_name; do 
     if [ "$(chroot $imageroot dpkg -l | grep "$package_name")" = "" ]; then continue; fi  
     # remove files
     for file in `chroot $imageroot dpkg -L $package_name | grep "/etc/"`
     do
       if [ ! -d $imageroot/$file ]; then
         for host in `ls $hostroot`
         do
           if [ "$host" != "." -a "$host" != ".." -a "$host" != "root" ];
           then
             rm -f $hostroot/$host/$file
           fi
         done
       fi
     done 
   done
   # remove the package
   mount -t proc proc $imageroot/proc
   chroot $imageroot apt-get $command_option remove $packages_name
   chroot $imageroot dpkg --purge $packages_name
   umount $imageroot/proc
   ;;
 "update")
   mount -t proc proc $imageroot/proc
   chroot $imageroot apt-get $command_option update 
   umount $imageroot/proc
   ;;
 *)
   echo "Not Yet Provided! Right now only"
   echo "  drbl-get install package_name"
   echo "  drbl-get remove package_name"
   echo "  drbl-get update"
   echo "are provided!"
   ;;
esac

[ $debconf_frontend -eq 1 ] && do_debconf "dialog"
echo "done.."
