#!/bin/sh

# This script allows Linux users to login to HSBC Australia's online banking.
# You need to visit HSBC's online banking first (you get an error message, but
# it sets a cookie first which is required for further operation).
# Then run this script and enter your PBN and PIN in the HTML form.
# You can now login to HSBC!

# Place your PBN (not required) and web browser command here
PBN=""
BROWSER="mozilla"

# Remove/replace these lines. Set your HTTP_PROXY if necessary
if [ $(gconftool-2 -g /system/http_proxy/use_http_proxy) == true ]
then export HTTP_PROXY=http://$(gconftool-2 -g /system/http_proxy/host):$(gconftool-2 -g /system/http_proxy/port)
else export HTTP_PROXY=""
fi

# Fetch a special session id variable from hsbc
ID=$(wget -q -O - --cookies=on https://www.ebank.hsbc.com.au/jsp/en/PreLogon.jsp https://www.ebank.hsbc.com.au/servlet/com.hsbc.ebank.app.hib.au.servlets.LogonServlet | sed -n 's/.*"screenId" VALUE="\(.*\)".*/\1/p')

# Create a temporary file
FILE=$(tempfile -s .html)

# Print out useful information
echo "Proxy: " $HTTP_PROXY
echo "   ID: " $ID
echo " File: " $FILE

# Generate a HTML form within the temporary file
echo "
<html><head></head><body>
<FORM method=\"POST\" name=\"PasswordForm\" action=\"https://www.ebank.hsbc.com.au/servlet/com.hsbc.ebank.app.hib.base.HsbcHibServlet\">
<P>
Personal Banking Number (PBN) <INPUT TYPE=\"text\" SIZE=\"10\" MAXLENGTH=\"10\" NAME=\"PBN\" VALUE=\"$PBN\" AUTOCOMPLETE=\"OFF\">
</P><P>
Personal Identification Number (PIN) <INPUT TYPE=\"password\" SIZE=\"10\" MAXLENGTH=\"6\" NAME=\"password\" VALUE=\"\" AUTOCOMPLETE=\"OFF\">
</P>
<INPUT TYPE=\"hidden\" NAME=\"screenId\" VALUE=\"$ID\">
<INPUT TYPE=\"hidden\" NAME=\"Destination\" VALUE=\"0\">
<INPUT type=\"submit\" value=\"Send\">
</FORM>
</body>
</html>
" > $FILE

# Open the file, then remove it
"$BROWSER" $FILE
rm $FILE
