#!/usr/local/bin/perl -w

#
# Site Redirect
# Mark Bondurant 3/2001

# Checks for the availability of my web server at home.
# If it's available then redirect there at port 81
# to avoid the Verizon nazis block of port 80, otherwise use
# the static site at Earthlink. Port 81 is more stealthy too.
# If there's a problem, email me then use the Earthlink static 
# site. My server's address is stored in 'server.dat', which is 
# refreshed via ftp every time my server reboots. This will have 
# to do until I can switch ISP's and obtain a static IP address.
#

#
# error failure notification
#

sub errorout {
    print <<EOF;
</HEAD>
<BODY>
<CENTER>
<H1><EM>
EOF
    print "$_[0]";
    print <<EOF;
</EM></H1>
</CENTER>
</BODY>
</HTML>
EOF
    die($_[0]);
}

use IO::Socket;
use strict;

my($socket, $serverip);

#
# read the my current server address from server.dat
#

errorout("Failed trying to open the server address file")
    unless (open(WEBSERVER, "server.dat"));

$serverip = <WEBSERVER>;

#
# try to open a socket to the website
#

$socket = IO::Socket::INET->new(PeerAddr => $serverip,
                                PeerPort => 81,
                                Proto => "tcp",
                                Type => SOCK_STREAM);

#
# start the web page
#

print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
<TITLE>Redirect Page</TITLE>
EOF

if ($socket) {
    print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=http://", $serverip, ":81\">\n";
    close($socket);
} else {
    print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=../static.htm\">\n";
}

#
# end the web page
#

print <<EOF;
</HEAD>
<BODY>
<CENTER>
EOF
print "<A HREF=\"http://", $serverip, ":81\">Click here if this fails to foward</A"
print <<EOF;
</CENTER>
</BODY>
</HTML>
EOF