#!/bin/bash


# Function to notify the user via Aple Script
notify () {
    osascript -e "display notification \"$1\" with title \"Netinfo\""
}

# If called with parameter "copy", copy the second parameter to the clipboard
if [ "$1" = "copy" ]; then
  # Copy to clipboard
  echo "$2" | pbcopy
  notify "Copied $2 to clipboard"
  exit 0
fi

echo '🌐'
echo '---'

INTERFACE=$(/usr/libexec/PlistBuddy -c "Print :NetworkInterFace" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null)
INTERFACE_STAT=$( networksetup -getairportpower ${INTERFACE} | awk "{print \$4}")
if [ "X${INTERFACE_STAT}" != "XOn" ]; then
    echo "♺刷新 | refresh=true"
	echo "打开Wi-Fi | terminal=false refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/SwitchWiFiStat.sh' param1='${INTERFACE}' param2='on'"
	echo '---'
	echo "加入其他网络... | refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/ConnectTempWiFi.sh'  param1='$INTERFACE' terminal=false"
	echo "设定网络接口名称 | refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/SetWifiInterface.sh' terminal=false"
	exit 0
fi 

WIFINAME=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}')
AUTH=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ link auth/ {print substr($0, index($0, $3))}')
CHANNEL=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -c | awk '{print substr($0, index($0, $2))}')

declare -a SCAN_WIFI
SCAN_WIFI=(`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s | tail -n +2 | awk 'BEGIN{ORS=" "}  {print \$1}' | sort | uniq`)
	

declare -a CONFIG_WIFI
status=0
index=0
while [ ${status} -eq 0 ]; do
	set +e	
	ssid=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:SSID" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null )
    status=${?}
	set -e
    if [ ${status} -eq 0 ]; then	
		CONFIG_WIFI[${#CONFIG_WIFI[*]}]=${ssid}
    	index=`expr ${index} + 1`
    fi
done


echo "♺刷新 | refresh=true"
echo "关闭Wi-Fi | terminal=false refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/SwitchWiFiStat.sh' param1='${INTERFACE}' param2='off'"
echo '---'

#列出当前Wifi链接信息
echo "${WIFINAME}:${AUTH}|color=#00ff00 terminal=false refresh=true bash='$0' param1=copy param2='${WIFINAME}'"
echo "Channel: ${CHANNEL}|color=#00ff00 terminal=false bash='$0' param1=copy param2='${CHANNEL}'"
echo "Device:    ${INTERFACE} | color=#00ff00  terminal=false bash='$0' param1=copy param2='${INTERFACE}'"
    ifconfig "${INTERFACE}" | awk "/ether/ { print \"    MAC:       \" \$2 \" | color=#00ff00 terminal=false bash='$0' param1=copy param2=\" \$2 }; /inet / { print \"    IPv4:       \" \$2 \" | color=#00ff00 terminal=false bash='$0' param1=copy param2=\" \$2 };  /inet6/ { print \"    IPv6:        \" \$2 \" | color=#00ff00 terminal=false bash='$0' param1=copy param2=\" \$2 }" | sed -e 's/%utun[0-9]*//g' -e 's/%en[0-9]*//g' | sort
echo "---"


#列出当前链接Wifi
status=0
index=0
while [ ${status} -eq 0 ]; do
	set +e	
	ssid=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:SSID" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null )
    status=${?}
	set -e
    if [ ${status} -eq 0 ]; then	
    	if [ "X${WIFINAME}" == "X${ssid}" ]; then
			set +e
	    	nickname=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:NickName" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null)
			set -e
		    if [ "X${nickname}" == "X" ]; then	
        		echo "✓ ${ssid} |color=#00ff00 terminal=false bash='$0' param1=copy param2='${ssid}"
			else
				echo "✓ ${nickname}:${ssid} | color=#00ff00 terminal=false bash='$0' param1=copy param2='${ssid}"
			fi
    	fi
    	index=`expr ${index} + 1`
    fi
done

#列出已经配置Wifi
status=0
index=0
while [ ${status} -eq 0 ]; do
	set +e	
	ssid=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:SSID" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null )
    status=${?}
	set -e
    if [ ${status} -eq 0 ]; then	
    	if [ "X${WIFINAME}" != "X${ssid}" ]; then
			set +e
	    	nickname=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:NickName" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null)
	    	wpakey=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:WPAKEY" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null)
			set -e
			isinsacn=0
			for element in ${SCAN_WIFI[@]}
			do
			    if [ "X${ssid}" == "X${element}" ]; then	
					isinsacn=1
				fi
			done
			if [ ${isinsacn} -eq 0 ]; then	
		        if [ "X${nickname}" == "X" ]; then	
        	    	echo "✕ ${ssid} | bash='/usr/local/StartWiFi/StartWiFi-A.sh'  param1='$INTERFACE' param2='${ssid}' param3='${wpakey}'  terminal=false"
			    else
				    echo "✕ ${nickname}:${ssid} | bash='/usr/local/StartWiFi/StartWiFi-A.sh'  param1='$INTERFACE' param2='${ssid}' param3='${wpakey}'  terminal=false"
			    fi
			else
		        if [ "X${nickname}" == "X" ]; then	
        	    	echo "${ssid} | bash=/bin/bash param1='/usr/local/StartWiFi/StartWiFi-A.sh'  param2='$INTERFACE' param3='${ssid}' param4='${wpakey}'  terminal=false"
			    else
				    echo "${nickname}:${ssid} | bash='/usr/local/StartWiFi/StartWiFi-A.sh'  param1='$INTERFACE' param2='${ssid}' param3='${wpakey}'  terminal=false"
			    fi
			fi
    	fi
    	index=`expr ${index} + 1`
    fi
done

echo "---"
#列出所有可用Wifi

while [ ${status} -eq 0 ]; do
	set +e	
	ssid=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:SSID" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null )
    status=${?}
	set -e
    if [ ${status} -eq 0 ]; then	
    	if [ "X${WIFINAME}" == "X${ssid}" ]; then
			set +e
	    	nickname=$(/usr/libexec/PlistBuddy -c "Print :Wi-Fi:${index}:NickName" /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist 2>/dev/null)
			set -e
		    if [ "X${nickname}" == "X" ]; then	
        		echo "✓ ${ssid} |color=#00ff00 terminal=false bash='$0' param1=copy param2='${ssid}"
			else
				echo "✓ ${nickname}:${ssid} | color=#00ff00 terminal=false bash='$0' param1=copy param2='${ssid}"
			fi
    	fi
    	index=`expr ${index} + 1`
    fi
done

echo "未配置网络"
for element in ${SCAN_WIFI[@]}
do
	isinsacn=0
	for ssid in ${CONFIG_WIFI[@]}
	do
	    if [ "X${element}" == "X${ssid}" ]; then	
			isinsacn=1
		fi
	done
	
	if [ ${isinsacn} -eq 0 ]; then	
		#这儿计划调用/Users/Shared/MenubarTools/WiFiHelper/AddWiFi-A.sh
      	echo "${element} | refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/ConnectTempWiFi.sh'  param1='$INTERFACE' param2='${element}' terminal=false"
	fi
done
echo "---"

echo "加入其他网络... | refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/ConnectTempWiFi.sh'  param1='$INTERFACE' terminal=false"
echo "设定网络接口名称 | refresh=true bash='/Users/Shared/MenubarTools/WiFiHelper/SetWifiInterface.sh' terminal=false"

