#!/bin/bash
#
#       Copyright (C) MOXA Inc. All rights reserved.
#       Copyright (C) 2015-2016 Wes Huang  <Wes.Huang@moxa.com>
#       Copyright (C) 2017      Aeluin Chen  <aeluin.chen@moxa.com>
#
#       This software is distributed under the terms of the
#       MOXA License.  See the file COPYING-MOXA for details.
#
#       wifi_mgmt
#               wifi management.
#


#--------- initial Global variable ---------

PKG="moxa-wifi-utils"
VERSION="1.8.0"

PROG=$0
CONF_DIR=/etc/${PKG}
RUN_DIR=/var/run/${PKG}
CONF_FILE=${CONF_DIR}/wifi_mgmt.conf
IW_SCAN_AWK=${CONF_DIR}/iw-scan.awk
WPACLI_SCAN_AWK=${CONF_DIR}/wpacli-scan.awk

_lock() {
        local fd=${1:-$LOCK_FD}
        local lock_file=${2:-$LOCK_FILE}
        local wait_timeout=${3:-30}


        # create lock file
        eval "exec $fd>$lock_file"

        # acquier the lock
        if [ -z "${wait_timeout}" ]; then
                flock -n $fd
        else
                flock -w ${wait_timeout} $fd
        fi
        [ $? -eq 0 ] || \
                 { >&2 echo "Error: ${PROG} is busy, please " \
                        "wait a moment and try again."; \
                        _exit 60; }
        return 0
}

_unlock() {
        eval "flock -u ${1:-$LOCK_FD}"
}

_init_interface () {
	# update:
	#    INTERFACE
	# usage:
	#    _init_interface ${CONF_FILE}
	# ex:
	#    _init_interface /etc/moxa-wifi-utils/wifi_mgmt.conf
	local conf_file=$1
	local interface
	if [ -f ${conf_file} ] && cat ${conf_file} | grep wlan* > /dev/null  ;then
		interface=$(cat ${conf_file})
	else
		interface="wlan0"
		echo wlan0 > ${conf_file}
	fi
	INTERFACE=${interface}
}

_interface_from_slot () {
	# update:
	#    INTERFACE
	# usage:
	#    _interface_from_slot ${SLOT}
	# ex:
	#    _interface_from_slot 3
	local slot=$1
	local interface
	if [ ! -z $(which mx_module_control) ]; then
		interface=$(mx_module_control list | grep "slot\[${slot}\]" | grep "wlan" | awk 'END {print $NF}')
		if [ -z ${interface} ]; then
			echo "Error: Module not found."
			exit 1
		fi
	fi
	if [ -z ${interface} ]; then
		interface="wlan$(( ${slot}-1 ))"
	fi
	INTERFACE=${interface}
}

_slot_from_interface () {
	# update:
	#    SLOT
	# usage:
	#    _slot_from_interface ${INTERFACE}
	# ex:
	#    _slot_from_interface wlan0
	local interface=$1
	local slot
	if [ ! -z $(which mx_module_control) ]; then
		slot=$(mx_module_control list | grep ${interface} | sed 's/slot\[\([0-9]\)\].*/\1/')
		if [ -z ${slot} ]; then
			echo "Error: Module not found."
			exit 1
		fi
	fi
	if [ -z "${slot}" ]; then
		slot=$(($(echo ${interface} | sed -e 's/wlan//g')+1))
	fi
	SLOT=${slot}
}

_init_wpa_supplicant () {
	# update:
	#    WPA_SUPPLICANT
	# usage:
	#    _init_wpa_supplicant ${INTERFACE}
	# ex:
	#    _init_wpa_supplicant wlan0
	local interface=$1
	WPA_SUPPLICANT="wpa_supplicant -i ${interface}"
}

_init_wpacli () {
	# update:
	#    WPACLI
	# usage:
	#    _init_wpacli ${INTERFACE}
	# ex:
	#    _init_wpacli wlan0
	local interface=$1
	WPACLI="wpa_cli -i ${interface}"
}

_order_from_interface () {
	# require:
	#    CONF_DIR
	# update:
	#    ORDER, WPA_CONF_FILE
	# usage:
	#    _order_from_interface ${INTERFACE}
	# ex:
	#    _order_from_interface wlan0
	ORDER=""
	local NUM_OF_INTERFACE=$(ip link | awk /wlan/{'print $2'} | wc -l)
	if [ X"${NUM_OF_INTERFACE}" == X"1" ]; then
		WPA_CONF_FILE="${CONF_DIR}/wpa_supplicant.conf"
	else
		local count=0
		for (( i=0; i<10; i++ ))
		do
			if ip link | grep "wlan$i" > /dev/null ; then
				if [ X"${INTERFACE}" == Xwlan"$i" ]; then
					if [ X"${count}" == X"0" ]; then
						ORDER=""
						WPA_CONF_FILE="${CONF_DIR}/wpa_supplicant.conf"
					else
						ORDER=${count}
						WPA_CONF_FILE="${CONF_DIR}/wpa_supplicant${ORDER}.conf"
					fi
				fi
				count=$(( ${count} + 1 ))
			fi
		done
	fi
}

#If there is no conf file, create a conf file
set_wpa () {
	if [ ! -f $WPA_CONF_FILE ]; then
		cat > $WPA_CONF_FILE <<EOF
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
update_config=1
EOF
	fi
}

#Check the password is hex or not.
ishex () {
        if [ -z "$1" ]; then
	        return 0
        fi
        case "$1" in
                *[!0-9a-fA-F]*)
                # plaintext
                return 1
                ;;
        *)
                # hexadecimal
                return 0
                ;;
        esac
}

isnum () {
	local i=0
	local IS_NUM=0
	for (( i=-150; i<99; i++ ))
	do
		if [ X"$1" == X"$i" ]; then
			IS_NUM=1;
			break
		fi
	done
	return $IS_NUM
}

# show wifi_mgmt usage list
wifi_usage () {
        echo -e "Usage:"
        echo -e "      $0 [-i <interface id>] [-s <slot id>] [OPTIONS]\n"
        echo -e "OPTIONS"
        echo -e "       -i <interface id>"
        echo -e "               Interface identifier."
        echo -e "               example: wlan0"
        echo -e "       -s <slot id>"
        echo -e "               Slot identifier, start from 1 and default value depends "
        echo -e "               on module interface."
        echo -e "               example: wlan0 may in slot 3"
        #start
	echo -e "       start Type=[type] SSID=[ssid] Password=[password]"
        echo -e "               Insert an AP information to the managed AP list and then connect to the AP."
	echo -e ""
	echo -e "               [type]        open/wep/wpa/wpa2"
	echo -e "               [ssid]        access point's SSID"
	echo -e "               [password]    access point's password"
	echo -e ""
	echo -e "               example:"
	echo -e "                   wifi_mgmt start Type=wpa SSID=moxa_ap Password=moxa"
	echo -e "                   wifi_mgmt start Type=open SSID=moxa_ap"
	echo -e "       start [network id]"
        echo -e "               Connect to AP by the managed AP list network id."
        echo -e "       start"
        echo -e "               Connect to the last time AP that was used."
        #scan
        echo -e "       scan [nowait]"
        echo -e "               Scan all the access points information."
        echo -e ""
        echo -e "               nowait        do not wait the results, only works after WiFi started."
        #scan_results
        echo -e "       scan_results"
        echo -e "               Shows all scanned access points information."
        echo -e "               Trigger \`wifi_mgmt scan\` for real time information."
        #signal
        echo -e "       signal"
        echo -e "               Show the AP's signal."
        #list
        echo -e "       list"
        echo -e "               Show the managed AP list."
        #insert
        echo -e "       insert Type=[type] SSID=[ssid] Password=[password]"
        echo -e "               Insert a new AP information to the managed AP list."
        echo -e ""
        echo -e "               [type]        open/wep/wpa/wpa2"
        echo -e "               [ssid]        access point's SSID"
        echo -e "               [password]    access point's password"
        echo -e ""
        echo -e "               example:"
        echo -e "                   wifi_mgmt insert Type=wpa SSID=moxa_ap Password=moxa"
	#delete
        echo -e "       delete [network id]"
        echo -e "               Choose an AP network id to delete which is in the managed AP list."
	#select
        echo -e "       select [network id]"
        echo -e "               Select an AP network id to connect which is in the managed AP list."
        #stop
        echo -e "       stop"
        echo -e "               Stop network."
        #status
        echo -e "       status"
        echo -e "               Query network connection status."
    	#interfaces
        echo -e "       interfaces"
        echo -e "               Show interface numbers"
    	#interface
        echo -e "       interface [num]"
        echo -e "               Switch to another wlan[num] interface."
	echo -e ""
	echo -e "               [num]    interface number"
	echo -e ""
	echo -e "               example:"
	echo -e "                   wifi_mgmt interface 0"
	echo -e "       interface"
	echo -e "               Get the current setting interface."
	#reconnect
        echo -e "       reconnect"
        echo -e "               Reconnect to the access point."
	#restart
        echo -e "       restart"
        echo -e "               Stop wifi_mgmt then start it again."
	#version
	echo -e "       version"
        echo -e "               Wifi management version."

}

#Connect to an AP and then use dhclient to get ip.
wifi_dhclient() {
	local connect_count=0
        while [ $connect_count -lt 10 ]
        do
        	sleep 5
		$WPACLI status | grep wpa_state
                if $WPACLI status | grep COMPLETED > /dev/null ; then
			sleep 3
			if $WPACLI status | grep COMPLETED > /dev/null ; then
				break;
			fi
                fi
                connect_count=$(( $connect_count + 1 ))
        done
	if [ $connect_count -eq 10 ]; then
		echo "### Can't connect to AP ###"
		echo "1. Make sure SSID is correct"
                echo "2. Make sure password of AP is correct"
                echo "3. Make sure encryption method is the same as AP"
		wifi_stop
		exit 1
	fi
	echo "*** Get DHCP IP address from AP ***"
        dhclient $INTERFACE
        if [ x"$?" != x"0" ]; then
                echo "### Cannot get DHCP IP from AP! ###"
                echo "1. Make sure SSID is correct"
                echo "2. Make sure password of AP is correct"
                echo "3. Make sure encryption method is the same as AP"
        	echo "4. Make sure DHCP server of AP is enable"
        else
        	echo "*** Get DHCP IP from AP! ***"
        fi
}

#Check whether wpa_supplicant is running
wifi_check() {
	if [ -a /var/run/wpa_supplicant$ORDER.pid ]; then
        	echo "wifi_mgmt is already run!"
       		exit 1
        else
       		set_wpa
        	${WPA_SUPPLICANT} -c$WPA_CONF_FILE -P/var/run/wpa_supplicant$ORDER.pid -Dnl80211 -B >/dev/null 2>&1
	fi
}

#Set AP information
wifi_setting() {
	local Type=""
	local SSID=""
	local Password=""
	local NET_NUM=`wpa_cli add_network | tail -n +2`

	for args in $@
        do
        	if [ "$(echo $args | grep "Type=")" ]; then
                	Type="${args##*Type=}"
                elif [ "$(echo $args | grep "SSID=")" ]; then
                       	SSID="${args##*SSID=}"
                elif [ "$(echo $args | grep "Password=")" ]; then
                       	Password="${args##*Password=}"
                else
                       	echo "Unknown:$args"
			$WPACLI remove_network $NET_NUM &> /dev/null
			wifi_stop
			exit 1
                fi
        done

	check_list=`wifi_mgmt list | awk '{print $2}' | grep $SSID`
	if [ X"$check_list" != X"" ] && [ X"${#check_list}" == X"${#SSID}" ]; then
		echo "This SSID is alreay in the managed AP list."
		echo "Please use 'wifi_mgmt list' to check the managed AP number."
		echo "You can use 'wifi_mgmt start [network id]' to start the connection."
		echo "You also can use 'wifi_mgmt delete [network id]' to delete it from the list"
		$WPACLI remove_network $NET_NUM &> /dev/null
		wifi_list
		wifi_stop &> /dev/null
		exit 1
	fi

	LEN=${#Password}

	$WPACLI remove_network $NET_NUM &> /dev/null
	$WPACLI add_network $NET_NUM &> /dev/null
	$WPACLI set_network $NET_NUM scan_ssid 1 &> /dev/null
	$WPACLI set_network $NET_NUM ssid "\"$SSID\"" &> /dev/null
	if [ X"$Type" == X"open" ];then
        	$WPACLI set_network $NET_NUM key_mgmt NONE &> /dev/null
        elif [ X"$Type" == X"wep" ];then
        	$WPACLI set_network $NET_NUM key_mgmt NONE &> /dev/null
                ishex $Password
		if [ X"$?" == X"0" ] ;then
			case "$LEN" in
				10|26|32|58)
					$WPACLI set_network $NET_NUM wep_key0 "$Password" &> /dev/null
	        			;;
				*)
					$WPACLI set_network $NET_NUM wep_key0 "\"$Password\"" &> /dev/null
        				;;
			esac
		else
	        	$WPACLI set_network $NET_NUM wep_key0 "\"$Password\"" &> /dev/null
        	fi
	elif [ X"$Type" == X"wpa" ] || [ X"$Type" == X"wpa2" ];then
        	$WPACLI set_network $NET_NUM key_mgmt WPA-PSK &> /dev/null
	        $WPACLI set_network $NET_NUM proto "WPA WPA2 RSN" &> /dev/null
        	$WPACLI set_network $NET_NUM pairwise "TKIP CCMP" &> /dev/null
		$WPACLI set_network $NET_NUM group "TKIP CCMP" &> /dev/null
	       	ishex $Password
                if [ X"$?" == X"0" ] ;then
			case "$LEN" in
				64)
					$WPACLI set_network $NET_NUM psk "$Password" &> /dev/null
					;;
				*)
					$WPACLI set_network $NET_NUM psk "\"$Password\"" &> /dev/null
        				;;
			esac
		else
			$WPACLI set_network $NET_NUM psk "\"$Password\"" &> /dev/null
		fi
	fi
        $WPACLI save_config &> /dev/null
        $WPACLI reconfigure &> /dev/null

	return $NET_NUM
}



#Wifi start function.
wifi_start () {
	case "$#" in
		0)
			wifi_check
	                wifi_dhclient
			;;
		1)
			local IS_NUM=0
			isnum $1
			IS_NUM=$?
			if [ X"$IS_NUM" == X"1" ]; then
				wifi_check
		       		sleep 3
			        wifi_select $1
			else
				wifi_usage
			fi
			;;
		2)
			for args in $@
	                do
                        	if [ "$(echo $args | grep "Type=")" ]; then
                	                type_detect="${args##*Type=}"
        	                fi
	                done
                	if [ X"$type_detect" == X"open" ]; then
        	                wifi_check
	                        wifi_setting $1 $2
                        	NET_NUM=$?
                	        wifi_select $NET_NUM
        	        else
	                        wifi_usage
                        	exit 1
                	fi
			;;
		3)
			wifi_check
                	wifi_setting $1 $2 $3
        	        NET_NUM=$?
	                wifi_select $NET_NUM
			;;
		*)
			wifi_usage
			;;
	esac
}

#Stop the connection and close wifi_mgmt
wifi_stop() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
		$WPACLI list_networks > ${CONF_DIR}/wifi_mgmt_list$ORDER
		sed -i 's/CURRENT/LAST USED/' ${CONF_DIR}/wifi_mgmt_list$ORDER
        	dhclient -r $INTERFACE
		PID=$(cat /var/run/wpa_supplicant$ORDER.pid)
                kill -INT $PID
                rm -rf /var/run/wpa_supplicant$ORDER.pid
                echo "Stopped."
	else
                echo "Stopped."
        fi
}

#Scan all of the AP information
_wifi_scan_wpa() {
	for i in {1..30}; do
		${WPACLI} scan 2>&1 | grep -q "OK$\|FAIL-BUSY$" && break
		[ $i -eq 30 ] && return 1 || sleep 1
	done

	[ "$1" == "nowait" ] && return

	sleep 3
	for i in {1..30}; do
		networks=$(${WPACLI} scan_results) && break
		[ $i -eq 30 ] && return 1 || sleep 1
	done
	echo "${networks}" | awk 'NR>1' | \
		awk -f ${WPACLI_SCAN_AWK} | sort -k 6,6
}
_wifi_scan_iw() {
	_lock
	cat /sys/class/net/$1/carrier > /dev/null 2>&1 \
		|| (ip link set ${INTERFACE} up; sleep 1)
	iw ${INTERFACE} scan | awk -f ${IW_SCAN_AWK} | \
		sort -k 6,6
	_unlock
}
wifi_scan() {
	local ifname=$(${WPACLI} ifname 2> /dev/null)
	local networks=""

	if [ "${ifname}" == "${INTERFACE}" ]; then
		networks=$(_wifi_scan_wpa)
	else
		networks=$(_wifi_scan_iw)
	fi
	echo "bssid / frequency / signal level (dBm) / encription / eap / ssid"
	echo "${networks}"
}

wifi_scan_results() {
	local ifname=$(${WPACLI} ifname 2> /dev/null)

	if [ "${ifname}" == "${INTERFACE}" ]; then
		networks=$(${WPACLI} scan_results | awk 'NR>1' | \
			awk -f ${WPACLI_SCAN_AWK} | sort -k 6,6)
	else
		networks=$(_wifi_scan_iw)
	fi
	echo "bssid / frequency / signal level (dBm) / encription / eap / ssid"
	echo "${networks}"
}

#Show the AP's signal
wifi_signal() {
	local status=""
	local signal=""
	local info=$(iw ${INTERFACE} info | grep type)
	if [ x"${info}" == x"" ]; then
		echo "${INTERFACE} disconnected"
		return 0
	fi

	# WiFi AP
	status=$(echo "${info}" | cut -d' ' -f 2)
	if [ "${status}" == "AP" ] || [ "${status}" == "ap" ]; then
		echo "N/A (WiFi AP mode)"
		return 0
	fi

	# WiFi Client
	# Excellent: > -50
	# Good: > -65
	# Fair: > -80
	# Weak: > -95
	# Very Weak: < -95
	info=$(iw ${INTERFACE} link)
	status="$(echo "${info}" | head -n 1 | awk '{print $1" "$2}')"
	if [ "${status}" == "Connected to" ]; then
		#echo "connected"
		signal="$(echo "${info}" | grep "signal:" | cut -d' ' -f 2-)"
		echo "${signal} or level=${signal}"
	elif [ "${status}" == "Not connected." ]; then
		echo "N/A (disconnected)"
	fi
	return 0
}

#Show the managed AP list
wifi_list() {
        if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
        	$WPACLI list_networks
	else
		if [ -f ${CONF_DIR}/wifi_mgmt_list$ORDER ]; then
			cat ${CONF_DIR}/wifi_mgmt_list$ORDER
        	else
			echo "There is no managed ap list."
		fi
	fi
}

#Insert an AP information to the managed list
wifi_insert_managed_ap() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
		case "$#" in
			3)
				wifi_setting $1 $2 $3
				exit 1
				;;
			2)
				for args in $@
	                        do
                        	        if [ "$(echo $args | grep "Type=")" ]; then
                	                        type_detect="${args##*Type=}"
        	                        fi
	                        done
                        	if [ X"$type_detect" == X"open" ]; then
                	                wifi_setting $1 $2
        	                else
	                                wifi_usage
                                	exit 1
                        	fi
				;;
			*)
				wifi_usage
				;;
		esac
        else
                echo "Stopped."
        	echo "Please start it first."
        fi
}

#Select an AP number which is in the managed list
wifi_select() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
  		if wifi_list | awk '{print $1}' | grep $1 >/dev/null ; then
		      	dhclient -r $INTERFACE
		        $WPACLI disconnect &> /dev/null
        		$WPACLI enable_network $1 &> /dev/null
		        $WPACLI select_network $1 &> /dev/null
        		$WPACLI save_config &> /dev/null
	        	$WPACLI reconfigure &> /dev/null
	        	wifi_dhclient
		else
			echo "There is no number $1 AP information in managed AP list."
		fi
	else
                echo "Stopped."
                echo "Please use 'wifi_mgmt start [network id]' to start connection."
        fi
}

#Delete an AP information from managed ap list
wifi_delete() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
	   	echo -e "***** WARNING *****"
        	echo -n "Are you sure that you want to delete network id $1 "
	        echo -n "(y/n)"
        	read choose
	        if [ x"$choose" == x"y" ]; then
        	        $WPACLI remove_network $1 &> /dev/null
                	$WPACLI save_config &> /dev/null
	                $WPACLI reconfigure &> /dev/null
        	fi
		$WPACLI list_networks
	else
		wifi_check
	   	echo -e "***** WARNING *****"
        	echo -n "Are you sure that you want to delete network id $1 "
	        echo -n "(y/n)"
        	read choose
	        if [ x"$choose" == x"y" ]; then
        	        $WPACLI remove_network $1 &> /dev/null
                	$WPACLI save_config &> /dev/null
	                $WPACLI reconfigure &> /dev/null
        	fi
		$WPACLI list_networks
        	wifi_stop &> /dev/null
	fi
}

#Show the ap information
wifi_status() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
        	$WPACLI status
	else
                echo "Stopped."
        fi
}

wifi_interfaces() {
	ip link | grep wlan | wc -l
}

#Show the current interface or set interface
wifi_interface() {
	local set_interface=""
	local i=""
	local confirm=0

	for (( i=0; i<=10; i++ ))
	do
		if [ "X"$1 == X"$i" ]; then
			if ip link | grep wlan$i > /dev/null ; then
				echo "wlan$i" > ${CONF_FILE}
				confirm=1
				echo "Now is setting the interface as wlan$1."
			else
				echo "There is no wlan$i interface."
			fi
		fi
	done

	if [ ! X"$confirm" == X"1" ]; then
		local NUM_OF_INTERFACE=$(ip link | awk /wlan/{'print $2'} | wc -l)
		echo "There is(are) $NUM_OF_INTERFACE interface(s):"
		for (( i=0; i<$NUM_OF_INTERFACE; i++ ))
		do
			local INTERFACE_NAME=$(ip link | awk /wlan/{'print $2'} | head -n$(( $i + 1 )) | tail -n1)
			echo "${INTERFACE_NAME//:/} $([ x"${INTERFACE_NAME//:/}" == x"$INTERFACE" ] && echo '   [Current]')"
		done
	fi
}

#reconnect to the access point
wifi_reconnect() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
        	dhclient -r $INTERFACE
	        $WPACLI disconnect &> /dev/null
        	$WPACLI reconnect &> /dev/null
	        wifi_dhclient
	else
                echo "Stopped."
                echo "Please start it first."
        fi
}

wifi_slot() {
	echo ${SLOT}
}

#Close wifi_mgmt and then start it again.
wifi_restart() {
	if [ -f /var/run/wpa_supplicant$ORDER.pid ]; then
                wifi_stop
		sleep 3
		wifi_start
        else
                echo "Stopped."
                echo "Please start it first."
        fi
}

#Show wifi_mgmt version
wifi_version() {
	echo "${PKG} $VERSION"
}


# Start point
mkdir -p "${RUN_DIR}"
_init_interface ${CONF_FILE}
_slot_from_interface ${INTERFACE}

while getopts  "i:s:" flag; do
	if [ "${flag}" == "i" ]; then
		if [ "${INTERFACE}" != "$OPTARG" ]; then
			INTERFACE=$OPTARG
			_slot_from_interface ${INTERFACE}
		fi
	elif [ "${flag}" == "s" ]; then
		if [ "${SLOT}" != "$OPTARG" ]; then
			SLOT=$OPTARG
			_interface_from_slot ${SLOT}
		fi
	fi
done

_init_wpa_supplicant ${INTERFACE}
_init_wpacli ${INTERFACE}
_order_from_interface ${INTERFACE}

LOCK_FD=$((SLOT+109))
LOCK_FILE="${RUN_DIR}/${SLOT}.lock"

for i in $(seq $OPTIND 1 $#); do
	eval "var=\$$i"
	j=$(($j + 1))
	eval "arg$j=\$$i"
done

case "$arg1" in
	slot)
		wifi_slot
		;;
        start)
                wifi_start $arg2 $arg3 $arg4
                ;;
        stop)
                wifi_stop
                ;;
        scan)
                wifi_scan $arg2
                ;;
        scan_results)
                wifi_scan_results
                ;;
        list)
		wifi_list
		;;
	insert)
		wifi_insert_managed_ap $arg2 $arg3 $arg4
		;;
	select)
		IS_NUM=0
                isnum $arg2
                IS_NUM=$?
                if [ X"$IS_NUM" == "X1" ]; then
                        wifi_select $2
                else
                	wifi_usage
		fi
		;;
	delete)
		wifi_delete $arg2
		;;
	status)
                wifi_status
                ;;
	signal)
		wifi_signal
		exit $?
		;;
	interfaces)
		wifi_interfaces
		;;
	interface)
		wifi_interface $arg2
		;;
	reconnect)
		wifi_reconnect
		;;
	restart)
		wifi_restart
		;;
	version)
        	wifi_version
        	;;
    	help)
                wifi_usage
                ;;
        *)
                wifi_usage
                exit 3
                ;;
esac

