#!/bin/sh

if [ -z "$IFS_IFNAME" ] || [ -z "$IFS_RUNDIR" ]; then
    echo "This script should not be run manually." 1>&2
    exit "$IFS_RC_ERROR"
fi

DAEMON_VERBOSITY=

if [ "$IFS_VERBOSE" = 1 ]; then
    DAEMON_VERBOSITY="-v"
    set -x
fi

WPA_SUP_BIN="/sbin/wpa_supplicant"
WPA_SUP_PNAME="wpa_supplicant"
WPA_SUP_PIDFILE="${IFS_RUNDIR}/wpa_supplicant.pid"
WPA_SUP_CONF="${IFS_ARG_CONF:=/etc/wpa_supplicant.conf}"

start_wpa () {
    # save config file checksum
    sha256sum "$WPA_SUP_CONF" > "${IFS_RUNDIR}/wpa_supplicant.conf.sha256"

    # start wpa_supplicant
    start-stop-daemon --start --oknodo $DAEMON_VERBOSITY \
        --name "$WPA_SUP_PNAME" --startas "$WPA_SUP_BIN" --pidfile "$WPA_SUP_PIDFILE" \
        -- -s -B -P "$WPA_SUP_PIDFILE" -i "$IFS_IFNAME" -c "$WPA_SUP_CONF"
    rc="$?"

    if [ "$rc" = 0 ]; then
        # save current wrapper config
        cp "${IFS_RUNDIR}/wrapper.sh" "${IFS_RUNDIR}/wrapper.sh.active"
    fi

    return $rc
}

stop_wpa () {
    # stop wpa_supplicant
    start-stop-daemon --stop --oknodo $DAEMON_VERBOSITY \
            --exec "$WPA_SUP_BIN" --pidfile "$WPA_SUP_PIDFILE"
    rc="$?"

    # cleanup pid file
    if [ -f "$WPA_SUP_PIDFILE" ]; then
            rm -f "$WPA_SUP_PIDFILE"
    fi

    return $rc
}

case "$1" in
    start)
        # check if pid file exists
        if [ -f "$WPA_SUP_PIDFILE" ]; then
                # check if wpa_supplicant is still running
                if start-stop-daemon --stop --quiet --signal 0 \
                        --exec "$WPA_SUP_BIN" --pidfile "$WPA_SUP_PIDFILE"; then

                        # check if wrapper has changed
                        if cmp -s "${IFS_RUNDIR}/wrapper.sh" "${IFS_RUNDIR}/wrapper.sh.active"; then
                            do_start=n

                            # reconfigure if config file has changed
                            if ! sha256sum --check --status "${IFS_RUNDIR}/wpa_supplicant.conf.sha256" 2> /dev/null; then
                                wpa_cli "-i$IFS_IFNAME" reconfigure
                            fi
                        else
                            do_start=r
                        fi
                else
                        rm -f "$WPA_SUP_PIDFILE"
                        do_start=y
                fi
        else
            do_start=y
        fi

        # nothing todo
        if [ "$do_start" = "n" ]; then
            exit "$IFS_RC_OK"
        fi

        # (re)start wpa_supplicant
        if [ "$do_start" = "r" ]; then
            stop_wpa
        fi
        start_wpa

        # set return code
        case "$?" in
            0)
                if [ "$do_start" = "r" ]; then
                    exit "$IFS_RC_CHANGED"
                else
                    exit "$IFS_RC_STARTED"
                fi
                ;;
            1)
                exit "$IFS_RC_OK"
                ;;
            *)
                exit "$IFS_RC_ERROR"
                ;;
        esac
        ;;
    check-start)
        # check if pid file exists
        if [ -f "$WPA_SUP_PIDFILE" ]; then
                # check if wpa_supplicant is still running
                if start-stop-daemon --stop --quiet --signal 0 \
                        --exec "$WPA_SUP_BIN" --pidfile "$WPA_SUP_PIDFILE"; then

                        # check if wrapper has changed
                        if cmp -s "${IFS_RUNDIR}/wrapper.sh" "${IFS_RUNDIR}/wrapper.sh.active"; then
                            # reconfigure if config file has changed
                            if ! sha256sum --check --status "${IFS_RUNDIR}/wpa_supplicant.conf.sha256" 2> /dev/null; then
                                exit "$IFS_RC_CHANGED"
                            else
                                exit "$IFS_RC_OK"
                            fi
                        else
                            exit "$IFS_RC_CHANGED"
                        fi
                else
                        rm -f "$WPA_SUP_PIDFILE"
                        exit "$IFS_RC_STARTED"
                fi
        else
            exit "$IFS_RC_STARTED"
        fi
        ;;

    stop)
        stop_wpa

        # set return code
        case "$?" in
            0)
                exit "$IFS_RC_STOPPED"
                ;;
            1)
                exit "$IFS_RC_OK"
                ;;
            *)
                exit "$IFS_RC_ERROR"
                ;;
        esac
        ;;

    check-stop)
        # check if wpa_supplicant is still running
        if start-stop-daemon --stop --quiet --signal 0 \
                --exec "$WPA_SUP_BIN" --pidfile "$WPA_SUP_PIDFILE"; then
                exit "$IFS_RC_STOPPED"
        else
                exit "$IFS_RC_OK"
        fi
        ;;
    *)
        logger -s -t ifstate-hook -- "unhandled action '$1'"
        ;;
esac
