#!/usr/bin/perl

# WO&D Trail Distance Calculator
# Version 1.0
# By Mike Calabrese
# http://www.mikecalabrese.com
# 06-21-2002

# This makes sure that the data is coming from your site
$refurl = ("yourisp.com");
if ($ENV{'HTTP_REFERER'} !~ /$refurl/g) {
    &bad_referer;
}

# Receive and clean up the  data
$theinput = <>;
chomp ($theinput);
$theinput=~s/\,//g;
$theinput=~s/\+//g; # Convert plus signs to spaces
$theinput=~s/%(..)/pack("c",hex($1))/ge; # keep the @ from showing as %40

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

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

# Remove the capitals
$starting_point = lc($starting_point);
$ending_point = lc($ending_point);

# Remove the comma stuff
$starting_point=~s/%2c//g;
$ending_point=~s/%2c//g;

# Remove any non alpha-numeric charactors
$starting_point=~s/\W//g;
$ending_point=~s/\W//g;

# This is the exact UNIX path to the data files
$points_directory = "/exact/UNIX/path/to/cgi-bin/woddist/points";

# Open the starting point files and get start distance info  
chdir($points_directory);
open(MYFILE, "$starting_point");
$starting_file = <MYFILE>;
@starting_file = split (/,/, $starting_file);
$starting_file_point = $starting_file[0]; # Start point
$starting_file_mileage = $starting_file[1]; # Start mileage

# Open the ending point files and get end distance info
chdir($points_directory);
open(MYFILE, "$ending_point");
$ending_file = <MYFILE>;
@ending_file = split (/,/, $ending_file);
$ending_file_point = $ending_file[0]; # End point
$ending_file_mileage = $ending_file[1]; # End mileage

# Compute the distance between the 2 points
$thedistance = abs($ending_file_mileage-$starting_file_mileage);

# 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>WO&D 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>WO&D Trail Distance Calculator</H3>\n";
print "<P>\n";
print "<BR><HR>\n";
print "The distance on the WO&D Trail between <BR>\n";
print "<B>$starting_file_point</B> and <BR><B>$ending_file_point</B> \n";
print "is <BR><B>$thedistance</B> miles.\n";
print "<BR><HR>\n";
print "The WO&D Trail Guide and other related gifts can be purchased at\n";
print "<BR> \n";
print "<A HREF=http://www.wodfriends.org/marketplace.html>\n";
print "The WO&D Trail Mail Order Gift Shop</A>.\n";    
print "</TD></TR></TABLE></CENTER>\n";
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</h2></body></html>\n";
   exit;
}

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