#!/usr/local/bin/perl

# Appalachian Trail Distance Calculator
# dbatdist.pl
# Version 2.3
# By Mike Calabrese
# http://www.mikecalabrese.com/index.shtml
# 2-3-2008

# This program is available for free on an AS-IS basis.  I am not
# responsible for consequential damages, loss of income or whatever
# else that may happen as a result  of the use of this program.

# This makes sure that the data is coming from your site
$refurl = ("http://yourdomain.com/exact/html/path/to/dbatdist/dbatdist.html");
if ($ENV{'HTTP_REFERER'} !~m#^$refurl#) {
    &bad_referer;
}

# Receive and clean up the  data
$theinput = <>;
chomp ($theinput);

# Split the elements at ampersands
@theinput = split (/&/,$theinput);

# Assign variables to the elements
$startpoint = substr($theinput[0], 6);
$endpoint = substr($theinput[1], 6);


# Remove plus signs & convert commas
$startpoint=~s/\+/ /g;
$startpoint=~s/\%2C/\,/g;
$endpoint=~s/\+/ /g;
$endpoint=~s/\%2C/\,/g;

# Connect to the database
$thedistance = 0;
$startdist = 0;
$enddist = 0;
use DBI;

# Replace databasename with the name of your database below
my ($dsn) = "DBI:mysql:databasename:localhost";

# Replace "username" with your database login name below
my ($user_name) = "username";

# Replace "userpwd" with your database password below
my ($password) = "userpwd";
my ($dbh, $sth);
my (@ary);
$dbh = DBI->connect ($dsn, $user_name, $password, {RaiseError => 1});

# Issue the select statement that retrieves the starting distance
$sth = $dbh->prepare ("SELECT location_distance from points "
   . "where location_point = '$startpoint'");
$sth->execute ();
$startdist = $sth->fetchrow_array ();

# Issue the select statement that retrieves the ending distance
$sth = $dbh->prepare ("SELECT location_distance from points "
   . "where location_point = '$endpoint'"); 
$sth->execute ();
$enddist = $sth->fetchrow_array ();

# Disconnect from the database
$sth->finish ();
$dbh->disconnect ();

# Calculate the absolute distance between the start
# point and the end point
$thedistance = abs($enddist - $startdist);

# Round off distances to nearest tenth where needed
$thedistance = sprintf("%.1f", $thedistance);

# Place commas in the distance where needed
$thedistance = comify($thedistance);

# Give out the results
print "Content-type: text/html\n\n";
print "<html><head><title>\n";
print "DB Based Appalachian Trail Distance Calculator\n";
print "</title></head>\n";
print "<body bgcolor=#ffffff text=#000000 link=#008000 vlink=#400000>\n";
print "<center>\n";
print "<table width=600 cellpadding=0 cellspacing=0>\n";
print "<TR><TD>\n";
print "<center>\n";
print "<h3>DB Based Appalachian Trail Distance Calculator</h3>\n";
print "<p align=center>\n";
print "<br><hr width='450'>\n";
print "The distance on the Appalachian Trail between <br>\n";
print "<b>$startpoint</b> and \n";
print "<br><b>$endpoint</b> \n";
print "is <br><b>$thedistance</b> miles.\n";
print "<br><hr width='450'>\n";
print "The <i>Data Book</i> and A.T. maps \n";
print "can be purchased at<br>\n";
print "<a href=http://www.atctrailstore.org>The Ultimate Trail \n";
print "Store</a> of the \n";
print "<a href=http://www.appalachiantrail.org>\n";
print "Appalachian \n";
print "Trail Conservancy</a>.\n";
print "</td></tr></table></center>\n";
print "<p align=center>\n";
print "<small>This script by:\n";
print "<a href=http://www.mikecalabrese.com/index.shtml>\n";
print "Mike Calabrese</a></small>
print "</body></html>\n";


sub bad_referer {
   print "Content-Type: text/html\n\n<html><head>\n";
   print "<title>403 Forbidden</title></head>\n";
   print "<body><h2>403 Forbidden|n";
   print "</h2></body></html>\n";

   exit;
}

sub comify {
    my $text = reverse $_[0];
    $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
    return scaler reverse $text;
}