#!/bin/bash
### BEGIN INIT INFO
# Provides:          dknnmconfig
# Required-Start:    $local_fs $syslog network-manager
# Required-Stop:     network-manager
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Setting up default wifi connection during startup
# Description:       Wifi connections defined in /etc/nm-config are setup
#                    automatically during startup. This ist for preconfigure
#                    wifi connections systemwide - without user interaction.
### END INIT INFO

# Author: Carsten Ungewitter <carsten@datenkollektiv.net>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="dknnmconfig"
NAME=dknnmconfig
SCRIPTNAME=/etc/init.d/$NAME

# Read configuration variable file if it is present
# or exit
[ -r /etc/nm-config ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions


case "$1" in
  start|restart)

# Problem: im Debian Installer existieren die wifi-Devices noch nicht
# wenn sie einen unfreien Treiber benötigen.
# Daher configurieren wir das Device bei jedem Systemstart
    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

    sed -i -e 's|^ifname=.*|ifname='$ifname'|' /etc/nm-config/default.wifi

	nmcli_command=/usr/bin/nmcli

	for i in /etc/nm-config/*.wifi; do

	con_id=""
	ssid=""
	psk=""

	. $i

	if grep ^id=$con_id /etc/NetworkManager/system-connections/*; then

	    echo "Wifi $con_id already configured"

	  else

	    echo "Setting up new default wifi connection for $con_id"
	    $nmcli_command con add con-name $con_id ifname $ifname type wifi ssid $ssid
	    $nmcli_command con modify $con_id wifi-sec.key-mgmt wpa-psk

	fi

	configured_psk=$(grep ^psk= /etc/NetworkManager/system-connections/$con_id | sed -e 's|^psk=||')
	echo confi_psk $configured_psk

	if [ "$configured_psk" == "$psk" ] ; then

	    echo "Preshared Key for wifi $con_id unchanged"

	  else

	    echo "Set psk for $con_id"
	    $nmcli_command con modify $con_id wifi-sec.psk $psk

	fi

	done

	# getting the default_id
	. /etc/nm-config/nm-config.conf
	$nmcli_command connection up $default_con
	;;

  stop)
	echo "stopping dknnmconfig"
	echo "this does nothing - just for keeping status of systemd"
	# service network-manager stop	

	;;

  status)
	status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?

	;;

  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
	exit 3
	;;
esac

:
