#!/bin/sh

# set_vap.sh for NON-openwrt systems
# Simplified from package_repos/dy_base-files/lib/dyiot/bin/set_vap.sh

export PATH=/opt/lnxall_app/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin

help() {
    cat <<_HELP_
Setup virtual AP.
Usage: $0 {set|stop} {ap|sta} [-ssid SSID] [-key PASSWORD ] [-name NAME ][-B]
        -ssid SSID # Only the AP and station modes are valid
        -key PASSWORD # Must be greater than 8 characters
        -name NAME# Virtual AP section named, vapNM n:0~1 M:0
        -B # Enable base64 decode for SSID and MESH ID 
Example:
    $0 set ap -name vap00 -B -ssid MTIzMjEzCg== -key password 
    $0 stop -name vap10 
_HELP_
}

if [ $# -lt 3 ] ; then 
    help
    exit 1
fi

ACTION=
case "$1" in
    set) ACTION="$1" ;; 
    stop) ACTION="$1" ;;
    *) help; exit 1 ;;
esac
shift 1

KEY=
SSID=
NAME=
MODE=
BASE64=
NO_RESTART=

disable_vap() {
uci batch << EOF
set wireless.$NAME.disabled='1'
commit wireless
EOF
}

if [ "$ACTION" = "stop" ] ; then
    case $1 in
    -name)
        NAME="$2" ; shift 2
        disable_vap ; exit ;;
    *) : what the fuck ;;
    esac
fi

case "$1" in
    ap) MODE="$1" ; shift 1 ;;
    sta) MODE="$1" ; shift 1 ;;
    *) help ; exit 1 ;;
esac

while [ -n "$1" ]; do
    case $1 in
        -name) NAME="$2" ; shift 2 ;;
        -ssid) SSID="$2" ; shift 2 ;;
        -key) KEY="$2" ; shift 2 ;;
        -mesh_id) SSID="$2" ; shift 2 ;;
        -B) BASE64='1' ; shift 1 ;;
        -S) NO_RESTART='1' ; shift 1 ;;
        --) shift ; break ;;
        -*) help ; exit 1 ;;
        *) break ;;
    esac
done

SHORTN="$(echo ${NAME} | cut -c1-4)"
if [ "${SHORTN}" = 'vap0' ] ; then
    NAME='radio0'
elif [ "${SHORTN}" = 'vap1' ] ; then
    NAME="radio1"
else
    help 
    exit 1
fi
unset SHORTN

len=$(echo "$KEY" | wc -c)
if [ $len -lt 9 ] ; then
    echo "-key size too short" 1>&2
    exit 1
fi

if [ "$BASE64" = "1" ] ; then
    SSID="$(echo "$SSID" | base64 -d)"
    echo "ssid = $SSID"
fi

set_metric() {
uci batch << EOF
set network.wifiwan.metric="$1"
set network.wifiwan.disabled="0"
commit network
EOF
}

disable_wifiwan() {
uci batch << EOF
set network.wifiwan.disabled="1"
commit network
EOF
}

set_ap_mode() {
uci batch << EOF
delete wireless.radio0
delete wireless.radio1
set wireless."$NAME"='wifi-iface'
set wireless."$NAME".mode="$MODE"
set wireless."$NAME".key="$KEY"
set wireless."$NAME".ssid="$SSID"
set wireless."$NAME".encryption="psk-mixed"
set wireless."$NAME".network="lan"
set wireless."$NAME".disabled="0"
commit wireless
EOF
}

set_sta_mode() {
uci batch << EOF
delete wireless.radio0
delete wireless.radio1
set wireless."$NAME"='wifi-iface'
set wireless.$NAME.device="$RADIO"
set wireless.$NAME.mode="$MODE"
set wireless.$NAME.key="$KEY"
set wireless.$NAME.ssid="$SSID"
set wireless.$NAME.encryption="psk-mixed"
set wireless.$NAME.network="wifiwan"
set wireless.$NAME.disabled="0"
commit wireless
EOF
}

case "$MODE" in
    ap) set_ap_mode ; disable_wifiwan ;;
    sta) set_sta_mode ; set_metric 10 ;;
    *) help ; exit 1 ;;
esac

[ -z "$NO_RESTART" ] && nothup netuci
exit 0
