#!/usr/bin/perl -w
use CGI;
use Switch;
use Text::Template 'fill_in_file';

my $q = new CGI;

my @deprecated_wlans = ( 'UoA', 'UoA-Guest' );

my $action = $q->param('switch_url');
my $wlan = $q->param('wlan');
my $status_code = $q->param('statusCode');
my $redirect = $q->param('redirect');

my $invalid = $action eq '' or $wlan eq '' or !($action =~ /^https:\/\/wireless\.auckland\.ac\.nz\//);
my $deprecated = grep { $_ eq $wlan } @deprecated_wlans;

my $status;
switch ($status_code) {
	case '1' {
		$status = "You are already logged in. No further action is required on your part.";
	}
	case '2' {
		$status = "You are not configured to authenticate against web portal. No further action is required on your part.";
	}
	case '3' {
		$status = "The username specified cannot be used at this time. Perhaps the username is already logged into the system?";
	}
	case '4' {
		$status = "The User has been excluded. Please contact the administrator.";
	}
	case '5' {
		$status = "Invalid username and password. Please try again.";
	}
}

my $view;
if ($invalid) {
	$view = 'templates/direct-access.tmpl';
} elsif ($deprecated) {
	$view = 'templates/uoa-2010-deprecated.tmpl';
} else {
	$view = 'templates/login.tmpl';
}

print "Content-type: text/html\n\n";
print fill_in_file($view, HASH => {
	action => $action,
	redirect => $redirect,
	status => $status
});
