#!/bin/sh -e

# Source debconf library.
. /usr/share/debconf/confmodule

# Read debconf answer
db_get laptop-netconf/active

# Set active flag
if [ "$RET" = "true" ]; then
  ACTIVE=yes
else
  ACTIVE=no
fi

# Update configuration file
if [ -f /etc/default/laptop-netconf ]; then
  sed "s/^ACTIVE=\"\?.*\"\?/ACTIVE=$ACTIVE/" </etc/default/laptop-netconf >/etc/default/laptop-netconf.new && mv -f /etc/default/laptop-netconf.new /etc/default/laptop-netconf
else
  cat <<EOF >/etc/default/laptop-netconf
# set to "no" to deactivate laptop-netconf
# set to "yes" to activate laptop-netconf
ACTIVE=$ACTIVE
EOF
fi

# Check for old config files and backup if necessary
if [ "$ACTIVE" = "yes" ]; then
  if [ -f /etc/resolv.conf ] && [ ! -L /etc/resolv.conf ]; then
    cp -a --backup=existing /etc/resolv.conf /etc/resolv.conf.bak
  fi
  if [ -f /etc/network/interfaces ] && [ ! -L /etc/network/interfaces ]; then
    cp -a --backup=existing /etc/network/interfaces /etc/network/interfaces.bak
  fi
fi

#DEBHELPER#
