#!/bin/bash

SYS_CONF="/etc/wpa_supplicant.conf"
BOOT_CONF="/boot/wpa_supplicant.conf"
CONF="/data/etc/wpa_supplicant.conf"

LOG="/var/log/wpa_supplicant.log"
PROG="/usr/sbin/wpa_supplicant"
DRIVER=nl80211,wext

SYS_WATCH_CONF="/etc/watch.conf"
BOOT_WATCH_CONF="/boot/watch.conf"
WATCH_CONF="/data/etc/watch.conf"


test -x ${PROG} || exit 0

test -n "${OS_VERSION}" || source /etc/init.d/base

prepare_conf ${WATCH_CONF} ${SYS_WATCH_CONF} ${BOOT_WATCH_CONF}
source ${WATCH_CONF}

prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF}
test -s ${CONF} || exit 0

ssid=$(cat ${CONF} | grep ssid | grep -v scan_ssid | cut -d '"' -f 2)
test -n "${ssid}" || exit 0

test "${OS_NETWORKLESS}" == "true" && exit 0


connected() {
    ip link show dev ${OS_WLAN} 2>&1 | grep LOWER_UP &> /dev/null && return 0 || return 1
}

watch() {
    count=0
    while true; do
        sleep 5
        if connected; then
            count=0
        else
            if [[ ${count} -lt ${LINK_WATCH_TIMEOUT} ]]; then
                count=$((${count} + 5))
                logger -t wifi "disconnected"
            else
                logger -t wifi "disconnected for ${LINK_WATCH_TIMEOUT} seconds, calling panic action"
                panic_action wifi
            fi
        fi
    done
}

start() {
    test -n "${OS_COUNTRY}" && iw reg set ${OS_COUNTRY}

    msg_begin "Starting wpa_supplicant"

    # wait up to 5 seconds for interface
    count=0
    while ! ifconfig ${OS_WLAN} >/dev/null 2>&1; do
        sleep 1
        count=$((${count} + 1))
        if [[ ${count} -ge 5 ]]; then
            msg_fail "no device"
            return 1
        fi
    done
    
    module=$(basename $(readlink /sys/class/net/${OS_WLAN}/device/driver/module 2>/dev/null) 2>/dev/null)

    iwconfig ${OS_WLAN} power off &> /dev/null
    iw ${OS_WLAN} set power_save off &> /dev/null
    
    opts="-i${OS_WLAN} -c${CONF} -D${DRIVER} -B"
    if [[ ${OS_DEBUG} == "true" ]]; then
        opts+=" -dd"
    fi
    
    ${PROG} ${opts} &>> ${LOG}
    
    count=0
    while true; do
        sleep 1

        if connected; then
            break
        fi

        if [[ ${count} -gt ${LINK_WATCH_TIMEOUT} ]] || ! pidof $(basename ${PROG}) > /dev/null; then
            test -n "${module}" && msg_fail "failed (${module})"|| msg_fail
            return 1
        fi

        count=$((${count} + 1))
    done

    if [[ "${LINK_WATCH}" == "true" ]]; then
        watch &
    fi

    test -n "${module}" && msg_done "done (${module})"|| msg_done
}

stop() {
    msg_begin "Stopping wpa_supplicant"
    killall -q $(basename ${PROG})
    ps | grep wifi | grep -v $$ | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1 | xargs -r kill
    msg_done
}

case "$1" in
    start)
        start
        ;;
        
    stop)
        stop
        ;;
        
    restart)
        stop
        start
        ;;
    
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

# continue after an unsuccessfull wifi start
# as we may still have an ethernet connection available
exit 0

