#! /bin/sh
# postinst script for enbd
#
# see: dh_installdeb(1)

#PTB subroutines may fail harmlessly! We don't want set -e
#set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

#CONFFILE=/etc/enbd.conf
#INITFILE=/etc/init.d/enbd
SBINDIR=${SBINDIR-/usr/sbin}
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# this function emulates which in case /usr/bin is not all there ...
which() {
  IFS=:
  set -- $PATH
  IFS=' '
  for d in $* ; do
    if [ -x "$d/$1" ]; then
       echo "$d/$1"
       return 0
    fi
  done
  return 1
}


INETD=`which inetd` || INETD=`which xinetd` || INETD=""

maybe_add_to_file() {

      local id="$1"
      local file="$2"
      local backup_suffix="${3:-bak}"

      if [ ! -e $file ]; then
        # echo "File" $file "does not exist! Cannot modify!";
        return 1
      fi


      grep -qws "$id" $file            && return 1
      [ $file = $file.$backup_suffix ] && return 1

      [ ! -e $file.$backup_suffix ] && cp -p $file $file.$backup_suffix

      local line=""
      if read l; then
          line="$l"
      fi
      while read l; do
          line="$line
$l"
      done
      update-inetd --file "$file" --add "$line"
}

if [ -z "$2" ]; then
  BACKUP_SUFFIX=bak
else
  BACKUP_SUFFIX=bak."$2"
fi

restore_newest_prior_backup() {

   local new_version=${2#.*bak.}
   local file="$1"
   local candidates=

   if [ -z "$3" ]; then
     for candidate in $file.bak.*; do
       candidates="$candidates $candidate"
     done
   else
     candidates="$file.$3"
   fi

   for backup in $candidates; do
     if ! [ $file -nt $backup ]; then
       continue
     fi
     local suffix
     eval "suffix=\${backup#${file}.bak.}"
     if [ -n "$suffix" ] && [ "$suffix" -ge "$new_version" ]; then
       continue
     fi
     if [ -z "$latest_backup" ]; then
       latest_backup="$backup"
       continue
     fi
     if [ "$backup" -nt "$latest_backup" ]; then
       latest_backup="$backup"
       continue
     fi
   done

   if [ -z "$latest_backup" ]; then
     return 1
   fi

   mv "$latest_backup" "$file"
}

trapfn() {
      local file="$1"
      echo
      echo 'Received signal.  Aborting configuration of enbd package.'
      echo -n 'Cleaning up...'
      restore_newest_prior_backup "$file" "$BACKUP_SUFFIX"
      echo 'done.'
      echo
}

main() {

  local conffile
  local conffiles
  local signals="1 2 3 15"

  case "$1" in
    configure)

	conffile=/etc/services
        conffiles="$conffiles $conffile"
        trap "trapfn $conffiles 1>&2 2>/dev/null" $signals
#        maybe_add_to_file enbd-cstatd $conffile "$BACKUP_SUFFIX" <<EOF
#enbd-cstatd 5051/tcp # NBD client statd
#EOF
#
#        maybe_add_to_file enbd-sstatd $conffile "$BACKUP_SUFFIX" <<EOF
#enbd-sstatd 5052/tcp # NBD server statd
#EOF
	conffile=/etc/inetd.conf
        conffiles="$conffiles $conffile"
        trap "trapfn $conffiles 1>&2 2>/dev/null" $signals
        maybe_add_to_file enbd-cstatd $conffile "$BACKUP_SUFFIX" <<EOF
enbd-cstatd stream tcp nowait root $SBINDIR/enbd-cstatd enbd-cstatd
EOF
        maybe_add_to_file enbd-sstatd $conffile "$BACKUP_SUFFIX" <<EOF
enbd-sstatd stream tcp nowait root $SBINDIR/enbd-sstatd enbd-sstatd
EOF
        [ "$INETD" ] && start-stop-daemon --quiet --stop --signal 1 --oknodo --exec $INETD

	conffile=/etc/xinetd.conf
        conffiles="$conffiles $conffile"
        trap "trapfn $conffiles 1>&2 2>/dev/null" $signals
        maybe_add_to_file enbd-cstatd $conffile "$BACKUP_SUFFIX" <<EOF
service enbd-cstatd
{
  socket_type = stream
  wait        = no
  user        = root
  server      = $SBINDIR/enbd-cstatd
  disable     = yes
}
EOF
        maybe_add_to_file enbd-sstatd $conffile "$BACKUP_SUFFIX" <<EOF
service enbd-sstatd
{
  socket_type = stream
  wait        = no
  user        = root
  server      = $SBINDIR/enbd-sstatd
  disable     = yes
}
EOF
    if [ ! -c /dev/.devfsd ]; then
        if [ ! -b /dev/nda ]; then
            cd /dev && /sbin/MAKEDEV nda ndb ndc ndd >/dev/null 2>&1 || true
        fi
    fi
    trap - $signals
    [ "$INETD" ] && start-stop-daemon --quiet --stop --signal USR2 --oknodo --exec $INETD
    ;;

    disable) # for testing
	conffile=/etc/inetd.conf
	update-inetd --file $conffile --disable enbd-cstatd,enbd-sstatd
	conffile=/etc/xinetd.conf
	update-inetd --file $conffile --disable enbd-cstatd,enbd-sstatd
    ;;

    enable) # for testing
	conffile=/etc/inetd.conf
	update-inetd --file $conffile --enable enbd-cstatd,enbd-sstatd
	conffile=/etc/xinetd.conf
	update-inetd --file $conffile --enable enbd-cstatd,enbd-sstatd
    ;;

    abort-upgrade)
        # we put back the old stuff
        new_version="$2"
        restore_newest_prior_backup /etc/inetd.conf $new_version
        restore_newest_prior_backup /etc/xinetd.conf $new_version
        restore_newest_prior_backup /etc/services $new_version
    ;;
    
    abort-remove)
        in_favour="$2"
        package="$3"
        new_version="$4"
        # mama .. we have to put back the NEW stuff.
        restore_newest_prior_backup /etc/inetd.conf $new_version
        restore_newest_prior_backup /etc/xinetd.conf $new_version
        restore_newest_prior_backup /etc/services $new_version
    ;;
    
    abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
  esac
}

main $*

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
