#!/bin/sh
#
#  airport.sh, a bash script by duus made on Mon Jul 23 09:28:21 EDT 2007 
#  lives in: ~/Documents/Software/unixscripts//airport/airport.sh 
#  run binedit airport to edit.  see binedit help for more information
#
_SCRIPT_NAME=airport             
_SCRIPT_LOCATION=~/bin/             
#             
if [ "$1" = "version" ]                
	then                                 
	echo " airport by duus"          
	echo " ~/Documents/Software/unixscripts//airport/airport.sh "
	exit 1;
fi
# --- normal script here --- 
#h airport on/off/toggle
_mode=""
if [ "$1" = "help" ]
	then
	grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more           
elif [ "$1" = "on" ]
	then
	_mode=on
elif [ "$1" = "off" ]
	then 
	_mode=off
elif [ "$1" = "toggle" ]
	then
	_mode=toggle
	echo "airport toggle not completely implemented."
else
	airport help
	exit 1;
fi

#--activate and hide Internet Connect.
echo | osascript <<EOF
tell application "System Events"
	set process_list to name of every process
	if process_list does not contain "Internet Connect" then
		try
			tell application "Internet Connect" to activate
		end try
	end if
	set visible of process "Internet Connect" to false
end tell
EOF

# prep window.
echo | osascript <<EOF
tell application "System Events"
	tell process "Internet Connect"
		if not (exists window 1) then
			keystroke "n" using command down
		end if
		
		if not (exists window "Airport") then
			click button "airport" of tool bar 1 of window 1
		end if
	end tell
end tell
EOF

#turn on
if [ "$_mode" = "on"  ]
	then
	echo | osascript <<EOF
tell application "System Events"
	tell process "Internet Connect"
	if exists button "Turn Airport On" of window "Airport" then
		click button "Turn Airport On" of window "Airport"
		set airport to "on"
	end if
	end tell
end tell
EOF
# turh off
elif [ "$_mode" = "off"  ]
	then
	echo | osascript <<EOF
tell application "System Events"
	tell process "Internet Connect"
	if exists button "Turn Airport Off" of window "Airport" then
    	click button "Turn Airport Off" of window "Airport"
    	set airport to "off"
    end if
	end tell
end tell
EOF
elif [ "$_mode" = "toggle"  ]
	then
	echo | osascript <<EOF
tell application "System Events"
	tell process "Internet Connect"
	if exists button "Turn Airport Off" of window "Airport" 
		then
    	click button "Turn Airport Off" of window "Airport"
    	set airport to "off"
		else 
		if exists button "Turn Airport On" of window "Airport" 
			then
			click button "Turn Airport On" of window "Airport"
			set airport to "on"
		end if
    end if
	end tell
end tell
EOF
fi

runlater s 10 echo | osascript <<EOF
tell application "Internet Connect"
	quit
end tell
EOF

# -------------------------- 
#
# end airport, ~/Documents/Software/unixscripts//airport/airport.sh

