#!/bin/bash

# User specific settings:
username="username"
password="password"
use_growl=true
sticky_growl=true # should the Growl notification stick on the screen?

# Network specific settings:
# Currently these will work at Marquette University
access_point="MU_Wireless" #access point name (SSID)
post_data="buttonClicked=4&err_flag=0&err_msg=&info_flag=0&info_msg=&redirect_url=&username=$username&password=$password"
login_url="https://1.1.1.1/login.html"

# Messages
success_message="Now connected to the Marquette network."
failure_message="Connecting to the Marquette network failed."

# Don't change anything below this line
#==================================================

wl="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I" #command path
curl="/usr/bin/curl -sk" #path to curl

if [ "$1" = "-h" ]; then
    echo "Usage: wireless_connect.sh [delay in seconds]"
    exit 0
fi

if [ "$1" != "" ]; then
    sleep $1 # wait however many seconds to give the computer time to connect to the network first
fi

if [ $sticky_growl = "true" ]; then
    growl_arg="-s"
else
    growl_arg=""
fi

#make sure we are on the appropriate network
correct_ap=`$wl | grep $access_point`

# if we are, then log in
if [ "$correct_ap" ]; then
    result=`$curl -d "$post_data" $login_url`

    if [ $use_growl = "true" ]; then
        # display a Growl notification of success or failure
        if ping -t3 -o google.com &> /dev/null
        then
            growlnotify $growl_arg -m "$success_message" -t "Network Status"
        else
            growlnotify $growl_arg -m "$failure_message" -t "Network Status"
        fi
    fi
fi
