#!/usr/bin/ksh
#
# wanpipe preinst
########################################
set -o errexit
OLD_CONF_FILES=FALSE

if [[ -n "$2" ]] ; then
   VERS=$2
   UPSTREAM_REL=${VERS%%-*} 
   DEBIAN_REL=${VERS##*-}

   if [ "$UPSTREAM_REL" = "2.0.1" ]
   then
      OLD_CONF_FILES=TRUE
   elif [ "$UPSTREAM_REL" = "2.0.3" ]
   then
      if (( DEBIAN_REL < 3 ))
      then
         OLD_CONF_FILES=TRUE
      fi
   fi   
fi

if [[ "$1" = "upgrade" && "$OLD_CONF_FILES" = "TRUE" ]] ||
   [[ "$1" = "install" && "$OLD_CONF_FILES" = "TRUE" ]]
then
   NEW_CONFDIR=/etc/wanpipe
   mkdir -p /etc/wanpipe/interfaces
   [ -f /etc/router.conf ] && cp -p /etc/router.conf /etc/wanpipe/router.conf
   [ -f /etc/router.cf ] && cp -p /etc/router.cf /etc/wanpipe/router.cf
   for file in $(ls /var/wanpipe)
   do
      cfile=/var/wanpipe/${file}
      [ -f $cfile ] && cp -p $cfile /etc/wanpipe/interfaces
   done
fi

exit 0
