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

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 http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

# function defining (look for "lets do the work" to find the real beginning)

grep_var () {
  # grep vor a variable assignment to variable $1 in file $2.
  # return the value that is assigned inside double quotes.
  egrep ^[[:blank:]]*$1= $2 | sed -e 's/.*"\([^#]*\)".*/\1/'
}

config_current_onboard () {
  cd /etc/
  # The script must not give an error when run multiple times.
  # 
  # Since this function is also run when the user chose to overwrite an
  # existing configuration, we destroy/restore what we find.
  if [ -L resolv.conf ]; then
    target=`readlink resolv.conf`
    rm resolv.conf
    mv $target resolv.conf
  fi
  rm -f resolv.conf.netenv-old resolv.conf.netenv-default
  # now we are in the state before netenv touched the system.
  #
  # now create the new configuration. Since we just made them ordinary files
  # in case they were symlinks, we can safely assume that they are. All other cases
  # (devices or the like) will brake anyway.
  cp resolv.conf resolv.conf.netenv-default
  rm resolv.conf
  ln -s resolv.conf.netenv-default resolv.conf
  # now we do the same for interfaces:
  cd network
  if [ -L interfaces ]; then
    target=`readlink interfaces`
    rm interfaces
    mv $target interfaces
  fi
  rm -f interfaces.netenv-old interfaces.netenv-default
  cp interfaces interfaces.netenv-default
  rm interfaces
  ln -s interfaces.netenv-default interfaces 
  # now we create the files in /etc/netenv/. First the file describing the configuration
  cat > $SETUPFILE << EOF
# created by Debian setup
netenv_id=Installation_default
export NETENV_SCRIPT=/etc/netenv/setup-default

# you can set any variable here. export it!

EOF
  # and this is the script to run that will move the symlinks
  cat > $NETENV_SCRIPT <<EOF
#!/bin/sh
# created by Debian setup

if [ -r /etc/network/interfaces.netenv-default ]; then
  mv /etc/network/interfaces /etc/network/interfaces.netenv-old
  ln -s /etc/network/interfaces.netenv-default /etc/network/interfaces
fi
if [ -r /etc/resolv.conf.netenv-default ]; then
  mv /etc/resolv.conf /etc/resolv.conf.netenv-old
  ln -s /etc/resolv.conf.netenv-default /etc/resolv.conf
fi

# you can do this with any file, e.g. /etc/printcap or XF86Config.

EOF
  # just to be sure, fix permissions
  chown 0 /etc/netenv/*
  chmod 644 /etc/netenv/*
}

config_current_pcmcia () {
  # list of variables to get from /etc/pcmcia/network.opts
  VARLIST="IF_PORT BOOTP DHCP DHCP_HOSTNAME PPPOE IPADDR NETMASK NETWORK BROADCAST GATEWAY DOMAIN SEARCH DNS1 DNS2 DNS3"
  # first, set all of them to empty values
  for VARIABLE in $VARLIST; do
    eval $VARIABLE="";
  done
  NETOPTS_TMP=`tempfile -d /tmp 2>/dev/null`
  trap 'rm -f $NETOPTS_TMP' 0 HUP INT QUIT PIPE TERM
  # write non-comment, non-empty lines with a variable assignment to temporary file
  egrep -v '^#|^$' /etc/pcmcia/network.opts | grep "=" > $NETOPTS_TMP
  # look for each variable in $VARLIST and assign it to the value found in the tempfile
  for VARIABLE in $VARLIST; do 
    eval $VARIABLE="`grep_var $VARIABLE $NETOPTS_TMP`";
  done
  rm $NETOPTS_TMP
  # now write the netenv setup file
  echo "# default configuration file, created by postinst script" > $SETUPFILE
  echo "netenv_id=installation_default" >> $SETUPFILE
  for VARIABLE in $VARLIST; do
    if [ -n "`eval echo "$"$VARIABLE`" ]; then
      # the value is not zero, so write it:
      echo "export $VARIABLE="`eval echo "$"$VARIABLE` >> $SETUPFILE
    fi
  done
}

# The sequence number  of netenv was changed after  version, to have a
# sequence number before networking and ifupdown.  It is reinserted by
# debhelper  below.
#
# In version  0.94.3-25, this statement was after  DEBHELPER tag which
# leaded  to #547394  as the  tag is  replaced by  `update-rc.d start'
# (dh_installinit).
if [ "$1" = "configure" ] && dpkg --compare-versions "$2" le "0.94.3-24" ; then
    update-rc.d -f netenv remove > /dev/null || exit $?
fi

# lets do the work

# We have to put the debhelper scripts above the hand-crafted
# part. Thus, we can disable netenv on boot time later by using
# update-rc.d.

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

#DEBHELPER#

. /usr/share/debconf/confmodule

NODE=`uname -n`
SETUPFILE=/etc/netenv/${NODE}
NETENV_SCRIPT=/etc/netenv/setup-default

case "$1" in
  configure|reconfigure)

# clean up for old bug (pre-0.82)
    find /etc/netenv \( -name "dialo*" ! -name "*-*" \) -o \( -name "netenv.tmp.*" \) -exec rm -f {} \;

# clean up temporary file of version 0.82
    if [ -f /etc/netenv/netenv ]; then 
      cat > /etc/netenv/netenv <<EOF
This file is no longer used or updated; you can
safely remove it.

Please refer to the tempfile
/var/tmp/netenv*

EOF

    fi
# remove stale file from version 0.82
    if [ -e /etc/netenv/${NODE}-default ]; then rm /etc/netenv/${NODE}-default; fi

# clean up temporary files (from version 0.94.2-2)
    find /var/tmp/ -type f -name "neten*" -atime +1 -exec rm '{}' \;

# restoring after bug #225582 (cleanup has been done in config)
    if [ -f /var/tmp/netenv_upgrade_restored-symlinks ]; then
      # if there were stale links, this means there should be links. We configure
      # with the link method
      config_current_onboard
    fi

# normal operation. Is netenv configured yet?
    db_get netenv/is_configured0
    if [ "$RET" = "false" ]; then
      # not configured, can we configure automatically? 
      db_get netenv/auto_configure
      case "$RET" in 
	"Use current settings")
	  # we can do automatic configuration. But which method?
	  db_get netenv/auto_method
	  case "$RET" in
	    pcmcia)
	      # do the configuration based on /etc/pcmcia/network.opts
	      config_current_pcmcia ;;
	    interfaces)
	      # do it based on /etc/network/interfaces
	      config_current_onboard ;;
	    *)
	      exit 1 ;;
	  esac
	  ;;
	"Disable for now")
# it's not exactly good to use update-rc.d in postinst, because it will 
# nuke any local customization. However, we only do this if there is no 
# working configuration, or the admin chose not to run his old one. This
# is kind of an emergency, and he'll have to read the docs and fiddle with
# his configuration manually, anyway.
	  update-rc.d -f netenv remove > /dev/null || exit $? ;;
	*) 
	  exit 1;;
      esac
    fi

# now clean up old, now unused debconf templates
    OLDTEMPLATES_0_94_2="netenv/howto_manual netenv/onboard_current_unreadable netenv/info_schemes netenv/howto_enable0 netenv/dialog_missing0 netenv/info_pcmcia netenv/ask_pcmcia netenv/howto_configure0 netenv/info_configure0 netenv/is_pcmcia netenv/IF_PORT netenv/BOOTP netenv/DHCP netenv/DHCP_HOSTNAME netenv/PPPOE netenv/IPADDR netenv/NETMASK netenv/NETWORK netenv/BROADCAST netenv/GATEWAY netenv/DOMAIN netenv/SEARCH netenv/DNS1 netenv/DNS2 netenv/DNS3"
    OLDTEMPLATES_0_04_3_2="netenv/cleaned_225582"
    OLDTEMPLATES="$OLDTEMPLATES_0_94_2 $OLDTEMPLATES_0_04_3_2"
    for template in $OLDTEMPLATES; do
      db_unregister netenv/$template || true
    done
    ;;

  abort-upgrade|abort-remove|abort-deconfigure)

    ;;

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

db_stop

exit 0
