#!/bin/sh

. /lib/functions.sh

SCRIPT=$0
# remember, if radio state was changed
radio_state_changed=0

_rm_cron_script()
{
    crontab -l | grep -v "$1" | sort -u | crontab -
}

set_radio_state() {
    local cfg="$1"
    local wifi_disabled="$2"

    # check if wifi-iface is a guest wifi-iface if not return
    [ "$(config_get $1 disabled)" != "1" ] &&
    [ "$(config_get $1 dvl_guest)" = "1" ] || return

    uci_set wireless "$cfg" disabled $wifi_disabled
    radio_state_changed=1
    uci_remove wireless guest_wifi starttime

}

disable_guest_wifi()
{
    _rm_cron_script "${SCRIPT} stop"

    config_foreach set_radio_state wifi-iface 1

    [ "$radio_state_changed" -eq 1 ] && { 
        uci_commit wireless
        local reload=$1
        [ -n "$reload" ] && reload=1
        [ "$reload" -eq 1 ] && reload_config # use procd to check for config changes
    }
}

usage()
{
    echo ""
    echo "$0 stop|help"
    echo ""
    echo "    UCI Config File: /etc/config/wireless"
    echo ""
    echo "    stop: Stop guest wifi."
    echo "    help: This description."
    echo ""
}

###############################################################################
# MAIN
###############################################################################

config_load wireless

case "$1" in
    stop) disable_guest_wifi "$@";;
    help|--help|-h|*) usage ;;
esac
