#!/bin/bash

# STARTING AUTHORIZATION
logger -i -s "FSUNETWORKLOGIN: Automatically Logging into FSU Network…"

# GETTING THE ADDRESS FOR AUTHORIZATION
AUTHREDURL=`curl -s http://www.google.com | grep -o URL=.*\</head | sed -e 's#URL=##' -e "s#\'\>\<\/head##"`
if [ "$AUTHREDURL" != "" ]; then
	logger -i -s "FSUNETWORKLOGIN: AUTHREDURL=$AUTHREDURL"
	SUBDOMAIN=`echo $AUTHREDURL | sed -e 's#URL=https://##' -e 's#\.ferris.*##' -e 's#https://##'`
	logger -i -s "FSUNETWORKLOGIN: SUBDOMAIN=$SUBDOMAIN"
	CM=`echo $AUTHREDURL | sed -e 's#https://##' -e 's#[A-z0-9./]*?cm=##' -e 's#&uri=[A-z0-9./%]*##'`
	logger -i -s "FSUNETWORKLOGIN: CM=$CM"

	S3=`curl -s $AUTHREDURL | grep -o 'value="-\?[0-9]\+" />' | sed -e 's#value="##' -e 's#" />##'`
	logger -i -s "FSUNETWORKLOGIN: S3=$S3"


	# GETTING AUTHORIZATION VARIABLES

	# ACTUAL FORM VALUES
	USERNAME="username" # CHANGE TO YOUR USERNAME
	PASSWORD="password" # CHANGE TO YOUR PASSWORD
	PROVIDER="Authentication Cluster"

	# HIDDEN FORM VALUES
	GUESTUSERNAMELABEL="Guest ID"
	GUESTPASSWORDLABEL="Password"
	PASSWORDLABEL="Password"
	USERNAMELABEL="Computing ID"
	REGISTERGUEST="NO"
	COMPACT="false"
	PAGEID="-1"
	INDEX="7"
	PM=""
	SESSION=$S3
	USERIP=$IP
	URI="http://www.google.com/"
	REQFROM="perfigo_login.jsp"
	CM=""


	FULLSTRING="username=$USERNAME&password=$PASSWORD&provider=$PROVIDER&guestUserNameLabel=$GUESTUSERNAMELABEL&guestPasswordLabel=$GUESTPASSWORDLABEL&passwordLabel=$PASSWORDLABEL&userNameLabel=$USERNAMELABEL&registerGuest=$REGISTERGUEST&compact=$COMPACT&pageid=$PAGEID&index=$INDEX&pm=$PM&session=$SESSION&userip=$USERIP&cm=$CM&uri=$URI&reqFrom=$REQFROM"
	logger -i -s "FSUNETWORKLOGIN: FULLSTRING=$FULLSTRING"

	# SENDING AUTHORIZATION
	
	# STATUS=`curl -s -A -f "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.1) Gecko/20060305 Firefox/1.5.0.1" -d "$FULLSTRING" https://$SUBDOMAIN.ferris.edu/auth/perfigo_cm_validate.jsp`
	STATUS=`curl --max-time 60 --connect-timeout 30 -A "Mozilla/4.0" -d "$FULLSTRING" https://$SUBDOMAIN.ferris.edu/auth/perfigo_cm_validate.jsp`
	
	logger -i -s "FSUNETWORKLOGIN: Status=$STATUS"
else
	logger -i -s "FSUNETWORKLOGIN: Could not complete; you may already be logged in or the internet connection is not available."
fi

# Maybe this will fix the weird launchd things
sleep 10
exit 0