#!/bin/bash
### BEGIN INIT INFO
# Provides:          wicdautodetect
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:
# Should-Stop:
# X-Start-Before:    wicd
# X-Stop-After:      wicd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: autodetect interfaces for wicd
# Description:       this script should autodetect the interfaces and set them
#
### END INIT INFO
# Policy Info:  http://wiki.debian.org/LSBInitScripts
# also check /etc/init.d/skeleton

# Author: Thanatermesis


# Defaults
#PIDDIR=/var/run/wicdautodetect
#wicdautodetectPID=$PIDDIR/wicdautodetect.pid
#DESC="autodetection of interfaces for wicd"
SCRIPTNAME=/etc/init.d/wicdautodetect


# Reads config file (will override defaults above)
[ -r /etc/default/wicdautodetect ] && . /etc/default/wicdautodetect

#. /lib/lsb/init-functions

_CMDLINE="$(cat /proc/cmdline)"

do_start(){
    local is_modified line file match

    if ! echo ${_CMDLINE} | LC_ALL=C grep -Fqs "boot=live"
    then
        exit 0
    fi
    if echo "${_CMDLINE}" | LC_ALL=C grep -Fq debug ; then
        set -x
    fi

    if [[ -x "/etc/init.d/wicd" ]] ; then

        #log_daemon_msg "Starting $(basename wicdautodetect)"

        # Make sure we have our PIDDIR, even if it's on a tmpfs
        #install -o root -g root -m 755 -d $$PIDDIR

        #log_progress_msg "detecting"

        # only configure if is not configured yet (persistence)
        match="$( LC_ALL=C grep -q "^wireless_interface =" /etc/wicd/manager-settings.conf | sed -e 's|^.* =||g' | sort -u | tail -1 )"
        # remove extra leading blank chars
        read -r match <<< "$match"

        # set wireless interface
        if [[ -z "$match" ]] ; then
            while read -ru 3 line
            do
                if ifup --force "$line" ; then
                    sed -i "s|^wireless_interface =.*$|wireless_interface = ${line}|" /etc/wicd/manager-settings.conf
                    is_modified=1
                fi
            done 3<<< "$( iwconfig 2>&1 | LC_ALL=C grep "^[[:alpha:]]" | LC_ALL=C grep -vi "no wireless extensions" | LC_ALL=C grep -E "(802.11|ESSID)" | awk '{print $1}' )"
        fi


        # only configure if is not configured yet (persistence)
        match="$( LC_ALL=C grep -q "^wired_interface =" /etc/wicd/manager-settings.conf | sed -e 's|^.* =||g' | sort -u | tail -1 )"
        # remove extra leading blank chars
        read -r match <<< "$match"

        # set wired interface
        if [[ -z "$match" ]] ; then
            # set cable/wired interfaces
            for file in /sys/class/net/eth*
            do
                if [[ -e "$file" ]] ; then
                    if ifup --force "${file##*/}" ; then
                        sed -i "s|^wired_interface =.*$|wired_interface = ${file##*/}|" /etc/wicd/manager-settings.conf
                        is_modified=1
                    fi
                fi
            done
        fi


        # other interesting settings:
        if ((is_modified)) ; then
            sed -i "s|^always_show_wired_interface =.*$|always_show_wired_interface = True|" /etc/wicd/manager-settings.conf
            sed -i "s|^prefer_wired =.*$|prefer_wired = True|" /etc/wicd/manager-settings.conf
            sed -i "s|^wired_connect_mode =.*$|wired_connect_mode = 3|" /etc/wicd/manager-settings.conf
        fi

    fi

    #if ! start-stop-daemon --start --quiet --oknodo --exec /usr/sbin/wicdautodetectd -- -D
    #then
        #log_end_msg 1
        #exit 1
    #fi

    # mark done for systemd
    echo "$0" >> /tmp/.live-processes-finished


    #log_progress_msg "completed"
    true
    #log_end_msg 0
}

do_stop(){
    #log_daemon_msg "Shutting down computer"
    #log_progress_msg "wicd"
    true
}

case "$1" in
    "")
        echo "Error: wicdautodetect should be called with the 'start' argument." >&2
        ;;
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart|reload|force-reload)
        #0 stop
        #0 start
        ;;
    *)
        echo "Usage: wicdautodetect [start|stop]" >&2
        exit 3
        ;;
esac


