#!/bin/bash
# postinst script for dknnmconfig
#
# 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>
#        * <postinst> `abort-remove'
#        * <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

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

case "$1" in
    configure)

      db_get dknnmconfig/wifiid
	conid=$RET
      db_get dknnmconfig/wifissid 
	ssid=$RET
      db_get dknnmconfig/wifipsk
	psk=$RET
      db_get dknnmconfig/wifiifname
	ifname=$RET
    # with predictable network interface names we have to guess:
    # das Problem ist aber, dass das mit unfreier Firmware nicht funktioniert.
    if udevadm info -e|grep "=wl" | grep "ID_NET_NAME="; then 
        ifname=$(udevadm info -e|grep ID_NET_NAME=|grep '=wl' | cut -d'=' -f2)
    elif udevadm info -e|grep "=wl" | grep "ID_NET_NAME_ONBOARD="; then
        ifname=$(udevadm info -e|grep ID_NET_NAME_ONBOARD=|grep '=wl' | cut -d'=' -f2)
    elif udevadm info -e|grep "=wl" | grep "ID_NET_NAME_MAC="; then
        ifname=$(udevadm info -e|grep ID_NET_NAME_MAC=|grep '=wl' | cut -d'=' -f2)
    fi

      # secure acess for config-file
      chmod 644 /etc/nm-config/nm-config.conf
      chmod 600 /etc/nm-config/*.wifi

      sed -i -e 's|^con_id.*|con_id='$conid'|' \
       -e 's|^ssid.*|ssid='$ssid'|' \
       -e 's|^ifname=.*|ifname='$ifname'|' \
       -e 's|^psk.*|psk='$psk'|' /etc/nm-config/default.wifi

      sed -i -e 's|^default_con=.*|default_con='$conid'|' /etc/nm-config/nm-config.conf

    ;;

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

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

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

#DEBHELPER#

exit 0
