#!/bin/bash

. /usr/lib/pm-utils/functions

[[ -f /usr/lib/network/network ]] || exit $NA
. /usr/lib/network/network
. /etc/conf.d/netcfg

WIRELESS_INTERFACE=${WIRELESS_INTERFACE:-wlan0}

auto_resume()
{
    if checkyesno ${RESTOREPROFILES:-no}; then
        all_resume
        sleep 2
    fi
    restartservice net-auto-wireless
    restartservice net-auto-wired
    restartservice net-profiles
}

daemon_suspend_all()
{
    stopservice net-auto-wireless
    stopservice net-auto-wired
    stopservice net-profiles
}

case "$1" in
    hibernate|suspend_hybrid|suspend)
        report_notice "suspending all interfaces..."
        daemon_suspend_all
        if checkyesno "${RESTOREPROFILES:-no}"; then        
            interface_suspend all
        fi
        ;;
    thaw|resume)
        if [ -f "$STATE_DIR/interface/${WIRELESS_INTERFACE}" ]; then
            report_notice "resuming all interfaces..."
            auto_resume ${WIRELESS_INTERFACE}
        else
            report_notice "resuming all interfaces except wireless..."
            all_resume ${WIRELESS_INTERFACE}
        fi
        ;;
    radio_off)
        report_notice "suspending wireless interface..."
        interface_suspend ${WIRELESS_INTERFACE} no
        set_iface disabled ${WIRELESS_INTERFACE}
        bring_interface forcedown ${WIRELESS_INTERFACE}
        ;;
    radio_on)
        report_notice "resuming wireless interface..."
        auto_resume ${WIRELESS_INTERFACE}
        if [ -x /etc/pm/power.d/??wifi ]; then
            /usr/bin/on_ac_power    # this is in pm-utils
            case $? in
                0)  # on ac
                    /etc/pm/power.d/??wifi false
                    ;;
                1)  # on battery
                    /etc/pm/power.d/??wifi true
                    ;;
            esac
        fi
        ;;
    *)
        ;;
esac

exit $?

# vim: ft=sh ts=4 et sw=4:
