#!/bin/sh -e

action="$1"

# fixup after ppp-connect removed (unless this is an abort-* action) 
expr "${action}" : abort- >/dev/null || {
  if [ -f /etc/ppp/ppp-connect ] ; then
    echo
    echo "The file /etc/ppp/ppp-connect is no longer part of the ppp"
    echo -n "package, do you want to remove it [y/n]: "
    read answer
    if [ "$answer" = y ] ; then
      rm /etc/ppp/ppp-connect
    fi
  fi
}

# fixup after old update-rc.d
if [ -f /etc/rc2.d/K20ppp ] ; then
  echo Fixing up start/stop scripts
  rm -f /etc/rc2.d/K20ppp
  rm -f /etc/rc3.d/K20ppp
  rm -f /etc/rc4.d/K20ppp
  rm -f /etc/rc5.d/K20ppp
fi

# get rid of old /etc/cron.daily/ppp
test -f /etc/cron.daily/ppp && rm -f /etc/cron.daily/ppp

# install /etc/ppp/pap-secrets and /etc/ppp/chap-secrets from examples,
# if necessary
for i in pap-secrets chap-secrets; do
  test -f /etc/ppp/$i || install -m 600 /usr/share/doc/ppp/examples/$i /etc/ppp
done
test -f /etc/ppp/peers/provider || \
  install -g dip -m 640 /usr/share/doc/ppp/examples/provider.peer /etc/ppp/peers/provider
test -f /etc/chatscripts/provider || \
  install -g dip -m 640 /usr/share/doc/ppp/examples/provider.chatscript /etc/chatscripts/provider

# setup hostname in /etc/ppp/pap-secrets and /etc/ppp/options.ttyXX
HOST="`hostname`"
for i in /etc/ppp/pap-secrets /etc/ppp/options.ttyXX; do
  if grep -q hostname $i; then
    tmp=$i.tmp-$$
    cp $i $tmp
    sed -e "s/hostname/$HOST/g" <$tmp  >$i
    rm $tmp
  fi
done

if [ -f /etc/ppp.options_out -a -f /etc/ppp.chatscript ]; then
  echo "Converting old dialup setup to new setup"
  mv /etc/ppp.chatscript /etc/chatscripts/provider
  X=`cat /etc/ppp.options_out`
  cat >/etc/ppp/peers/provider <<-!EOF!
	# The chatscript (be sure to edit that file too)
	connect "/usr/sbin/chat -v -f /etc/chatscripts/provider"
	$X
!EOF!
  rm /etc/ppp.options_out
  chmod 640 /etc/chatscripts/provider /etc/ppp/peers/provider
  chgrp dip /etc/chatscripts/provider /etc/ppp/peers/provider
fi

if [ -f /var/log/ppp.log ]; then
  chown root.adm /var/log/ppp.log
  chmod 640 /var/log/ppp.log
fi
if [ -f /var/log/ppp-connect-errors ]; then
  chown root.adm /var/log/ppp-connect-errors
  chmod 640 /var/log/ppp-connect-errors
fi

# suidregister doesn't like non-existent files
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/ppp/pap-secrets root root 0600 || true
elif [ -e /etc/ppp/pap-secrets ]; then
        chown root.dip /etc/ppp/pap-secrets
        chmod 0600 /etc/ppp/pap-secrets
fi
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/ppp/chap-secrets root root 0600 || true
elif [ -e /etc/ppp/chap-secrets ]; then
        chown root.dip /etc/ppp/chap-secrets
        chmod 0600 /etc/ppp/chap-secrets
fi
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/ppp/peers/provider root dip 0640 || true
elif [ -e /etc/ppp/peers/provider ]; then
        chown root.dip /etc/ppp/peers/provider
        chmod 0640 /etc/ppp/peers/provider
fi
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/chatscripts/provider root dip 0640 || true
elif [ -e /etc/chatscripts/provider ]; then
        chown root.dip /etc/chatscripts/provider
        chmod 0640 /etc/chatscripts/provider
fi

if [ "$1" = "configure" ] && [ -n "$2" ] && dpkg --compare-versions $2 lt 2.3.9 ; then
  if [ -f /usr/share/debconf/confmodule ]; then # debconf exists
    # source debconf library
    . /usr/share/debconf/confmodule
    # display the maxfail note
    db_get ppp/maxfail
  else # debconf doesn't exist, revert to old type of note
    cat <<END >&2
 If you used the \`persist' option in your pppd settings, you will notice
 that pppd will not redial infinitely anymore, instead it will redial
 ten times, and quit.

 A new \`maxfail' option was introduced to change this. To get the old
 behaviour, you will have to add \`maxfail 0' to your pppd settings.

 Details about this can be found in /usr/share/doc/ppp/README.Debian.gz
 file and in pppd(8) manual page.

 Please press <enter> to continue...
END
    read KEYPRESS
  fi # debconf or not
fi # configure before 2.3.9

# Automatically added by dh_installdocs
if [ "$1" = "configure" ]; then
	if [ -d /usr/doc -a ! -e /usr/doc/ppp -a -d /usr/share/doc/ppp ]; then
		ln -sf ../share/doc/ppp /usr/doc/ppp
	fi
fi
# End automatically added section
# Automatically added by dh_installinit
update-rc.d ppp start 14 2 3 4 5 . stop 14 0 1 6 . >/dev/null
if [ "$1" = "configure" ]; then
	if [ -z "$2" -o "$2" = "<unknown>" ]; then
		/etc/init.d/ppp start
	fi
fi
# End automatically added section
# Automatically added by dh_suidregister
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /usr/sbin/pppd root dip 04750
elif [ -e /usr/sbin/pppd ]; then
        chown root.dip /usr/sbin/pppd
        chmod 04750 /usr/sbin/pppd
fi
# End automatically added section
# Automatically added by dh_suidregister
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/ppp root dip 0750
elif [ -e /etc/ppp ]; then
        chown root.dip /etc/ppp
        chmod 0750 /etc/ppp
fi
# End automatically added section
# Automatically added by dh_suidregister
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/ppp/peers root dip 02750
elif [ -e /etc/ppp/peers ]; then
        chown root.dip /etc/ppp/peers
        chmod 02750 /etc/ppp/peers
fi
# End automatically added section
# Automatically added by dh_suidregister
if command -v suidregister >/dev/null 2>&1 && [ -e /etc/suid.conf ]; then
        suidregister -s ppp /etc/chatscripts root dip 02750
elif [ -e /etc/chatscripts ]; then
        chown root.dip /etc/chatscripts
        chmod 02750 /etc/chatscripts
fi
# End automatically added section

