#!/usr/bin/perl

$> = $<;  # set effective user IDs to real id
$) = $(;  # set goup id to real id

$ENV{'PATH'} = '/bin:/usr/bin';
$ENV{'IFS'} = '' if $ENV{'IFS'} ne '';

$num = time;
srand($num);	# seed random number generator with PID
$number = int(rand(10000));

# ------------------- Banner Ad -----------------------
#$ad = "<A HREF=\"http://www.burstnet.com/ads/ad6389a-map.cgi/$number\" target=\"new\"><IMG SRC=\"http://www.burstnet.com/cgi-bin/ads/ad6389a.cgi/$number\" BORDER=\"0\" WIDTH=\"468\" HEIGHT=\"60\"></A>";
   open (INPUT,"../burstcode.htm");
   while(eof(INPUT) == 0) {
      @ad = <INPUT>;
   }
   close(INPUT);

#$ad = "<Table width=100% height=100 BGCOLOR=\"#FFB0FF\">\n  <tr><td>\n";
#$ad = $ad."    <center>$adstring</center>\n";
#$ad = $ad."  </td></tr>\n";
#$ad = $ad."</table>\n";

# -------------- Link Exchange Ad ------------------
$le="<a href=\"http://ad.linkexchange.com/$number/X483973/gotoad.map\" target=\"new\"><img width=440 height=40 border=1 ismap alt=\"LinkExchange\" src=\"http://ad.linkexchange.com/$number/X483973/logoshowad?free\"></a><br><font size=1><a href=\"http://www.linkexchange.com/\" target=\"new\">LinkExchange Member</a></font><br>\n";

$docnumber=$ENV{'QUERY_STRING'};
#Stop nasty subshell stuff that could crop up...
$docnumber =~ s/~!/ ~!/g;
$docnumber =~ s/\W//g;      # get rid of anything except 0-9 a-z

$next = 0;
$pos = 0;

random_book_video();

# --------- A dummy file must exist in this directory before displaying 
#open(INPUT,"resorts2/$docnumber.htm") || return_error();
#close (INPUT);

read_resort_name();
read_resort_info();
read_snow_cond();
read_enhanced_info();
return_web_page();

exit;

#------------------------------------------
#           Subroutines
#------------------------------------------

sub return_error() {
   print "Content-type: text/html\n\n";
   print "<html><head><title>Error</title></head><body>";
   print "<b><font color=\"#FF0000\">There is no data for your request.</font> <br> If you feel that this is in error, please <a href=\"mailto:temporary\@funguide.com?subject=Error retrieving information for (resort.pl?$docnumber)\">Contact us</a>.</b>";
   print "</body></html>";

   exit;
}

sub read_resort_name() {
   open (INPUT,"accounts/accounts.dat");
   while(eof(INPUT) == 0) {
      @lines = <INPUT>;
   }
   close(INPUT);

   $total_lines = @lines;
   if($total_lines == 0) { return_error(); }

   $done = 0;
   $count =0;
   do {
      chop($lines[$count]);
      ($account,$name,$acct_type,$city,$state,$country,$url2) = split('\|',$lines[$count]);
      if($account == $docnumber) { $done = 1; }
   } while(++$count < $total_lines && $done == 0);

#   if($done == 0) { return_error(); }
}

sub read_resort_info() {
   open (INPUT,"accounts/info/$docnumber.pl") || return_error();
     $line = <INPUT>;
     chop($line);
   close(INPUT);

   ($infophone,$snowphone,$pipes,$lifts,$snowtubing,$trails,$beg,$int,$adv,$annual,$snowmaking,
        $longest,$vert,$acres,$topelev,@checked) = split('\|',$line);
   if($infophone) { $infophone = "<b>Information:</b> $infophone<br>"; }
   else { $infophone = ""; }
   if($snowphone) { $snowphone = "<b>Snow Cond:</b> $snowphone<br>"; }
   else { $snowphone = ""; }
   if($pipes) { $pipes = "<b>Pipes/Parks:</b> $pipes<br>"; }
   else { $pipes = ""; }
   if($lifts) { $lifts = "<b>Lifts:</b> $lifts<br>"; }
   else { $lifts = ""; }
   if($trails) { $trails = "<b>Trails:</b> $trails<br>"; }
   else { $trails = ""; }
   if($snowtubing) { $snowtubing = "<b>Snow tubing:</b> $snowtubing<br>"; }
   else { $snowtubing = ""; }
   if($beg) { $beg = "<b>Beginner:</b> $beg<br>"; }
   else { $beg = ""; }
   if($int) { $int = "<b>Intermediate:</b> $int<br>"; }
   else { $int = ""; }
   if($adv) { $adv = "<b>Advanced:</b> $adv<br>"; }
   else { $adv = ""; }
   if($annual) { $annual = "<b>Annual Snow:</b> $annual inches<br>"; }
   else { $annual = ""; }
   if($snowmaking) { $snowmaking = "<b>Snowmaking:</b> $snowmaking%<br>"; }
   else { $snowmaking = ""; }
   if($longest) { $longest = "<b>Longest Run:</b> $longest ft.<br>"; }
   else { $longest = ""; }
   if($vert) { $vert = "<b>Vertical:</b> $vert ft.<br>"; }
   else { $vert = ""; }
   if($acres) { $acres = "<b>Skiiable Acres:</b> $acres<br>"; }
   else { $acres = ""; }
   if($topelev) { $topelev = "<b>Peak Elevation:</b> $topelev ft.<br>"; }
   else { $topelev = ""; }
   if($checked[0] || $checked[1] || $checked[2] || $checked[3] || $checked[4]) {
      $other = "<b>Other:</b>\n";
      if($checked[0] eq "checked") { $other = $other."<li>Cross Country Skiing\n"; }
      if($checked[1] eq "checked") { $other = $other."<li>Mogul Fields\n"; }
      if($checked[2] eq "checked") { $other = $other."<li>Extreme skiing\n"; }
      if($checked[3] eq "checked") { $other = $other."<li>Night skiing\n"; }
      if($checked[4] eq "checked") { $other = $other."<li>Snowmobiling Available\n"; }
      $other = $other."<br>\n";
   }
}

sub read_snow_cond() {
   open (INPUT,"accounts/snow/$docnumber.pl") || return_error();
     $line = <INPUT>;
     chop($line);
   close(INPUT);

   ($update,$base,$newsnow,$surface,$temp) = split('\|',$line);
}

sub read_enhanced_info() {
   open (INPUT,"accounts/enhance/$docnumber.pl") || return_error();
     $line = <INPUT>;
     chop($line);
   close(INPUT);

   ($url,$add_link,$cam_cap[0],$cam_url[0],$cam_cap[1],$cam_url[1],$cam_cap[2],$cam_url[2],
    $email,$special_evt_date[0],$special_evt[0],$special_evt_date[1],$special_evt[1],
    $special_evt_date[2],$special_evt[2]) = split('\|',$line);

   if($special_evt_date[0] || $special_evt_date[1] || $special_evt_date[2]) {
      $special = "<table width=100%><tr><td colspan=2 align=left><b><i>Special Events</i></b></td></tr>";
      $special = $special."<tr><td width=80 align=left valign=top><u>Date</u></td><td align=left valign=top><u>Event</u></td></tr>";
      if($special_evt_date[0]) {
         $special = $special."<tr><td width=80 align=left valign=top>$special_evt_date[0]</td><td align=left valign=top>$special_evt[0]</td></tr>";
      }
      if($special_evt_date[1]) {
         $special = $special."<tr><td width=80 align=left valign=top>$special_evt_date[1]</td><td align=left valign=top>$special_evt[1]</td></tr>";
      }
      if($special_evt_date[2]) {
         $special = $special."<tr><td width=80 align=left valign=top>$special_evt_date[2]</td><td align=left valign=top>$special_evt[2]</td></tr>";
      }
      $special = $special."</table><br><br>\n";
   }

   if($email) {
      $email = "<a href=\"contact.pl?$docnumber\">Email Ski Resort</a><br>";
   }
}

sub return_web_page() {

   #----- Open & read templete file
   open(INPUT,"resort.htm");
   while(eof(INPUT) == 0) {
      @lines = <INPUT>;
   }
   close(INPUT);

   $total_lines = @lines;
   $count = 0;

   print "Content-type: text/html\n\n";
   do{     
      $string = "<!AD>";
      $lines[$count] =~ s/$string/@ad/;
#      $lines[$count] =~ s/$string/$ad/;
      $string = "<!LE>";
      $lines[$count] =~ s/$string/$le/;
      $string = "<!SUGGESTION>";
      $lines[$count] =~ s/$string/$suggestion/;
      $string = "<!RESORT>";
      $lines[$count] =~ s/$string/$name/;
      $string = "<!CITY>";
      $lines[$count] =~ s/$string/$city/;
      $string = "<!STATE>";
      $lines[$count] =~ s/$string/$state/;
      $string = "<!COUNTRY>";
      $lines[$count] =~ s/$string/$country/;
      $string = "<!INFOPHONE>";
      $lines[$count] =~ s/$string/$infophone/;
      $string = "<!SNOWPHONE>";
      $lines[$count] =~ s/$string/$snowphone/;
      $string = "<!VERT>";
      $lines[$count] =~ s/$string/$vert/;
      $string = "<!TRAILS>";
      $lines[$count] =~ s/$string/$trails/;
      $string = "<!BEG>";
      $lines[$count] =~ s/$string/$beg/;
      $string = "<!INT>";
      $lines[$count] =~ s/$string/$int/;
      $string = "<!ADV>";
      $lines[$count] =~ s/$string/$adv/;
      $string = "<!TOPELEV>";
      $lines[$count] =~ s/$string/$topelev/;
      $string = "<!ANNUAL>";
      $lines[$count] =~ s/$string/$annual/;
      $string = "<!ACRES>";
      $lines[$count] =~ s/$string/$acres/;
      $string = "<!LIFTS>";
      $lines[$count] =~ s/$string/$lifts/;
      $string = "<!SNOWMAKING>";
      $lines[$count] =~ s/$string/$snowmaking/;
      $string = "<!LONGEST>";
      $lines[$count] =~ s/$string/$longest/;
      $string = "<!OTHER>";
      $lines[$count] =~ s/$string/$other/;
      $string = "<!PIPES>";
      $lines[$count] =~ s/$string/$pipes/;
      $string = "<!SNOWTUBING>";
      $lines[$count] =~ s/$string/$snowtubing/;
      $string = "<!UPDATE>";
      $lines[$count] =~ s/$string/$update/;
      $string = "<!BASE>";
      $lines[$count] =~ s/$string/$base/;
      $string = "<!SURFACE>";
      $lines[$count] =~ s/$string/$surface/;
      $string = "<!NEWSNOW>";
      $lines[$count] =~ s/$string/$newsnow/;
      $string = "<!TEMP>";
      $lines[$count] =~ s/$string/$temp/;
      $string = "<!SPECIAL>";
      $lines[$count] =~ s/$string/$special/;
#      $string = "<!EMAIL>";
if($acct_type == 2)
{
      $link1 = "<b>Web site:</b> <a target=\"new\" href=\"http://$url\">$url</a><br>";
      $string = "<!URL>";
      $lines[$count] =~ s/$string/$link1/; 
} 

if($acct_type == 3)
{
      $link1 = "<b>Web site:</b> <a target=\"new\" href=\"http://$url\">$url</a><br>";
      $string = "<!URL>";
      $lines[$count] =~ s/$string/$link1/; 
      $link2 = "<font size=2><b><a target=\"new\" href=\"http://$url2\">Latest snow conditions</a></b></font><br><br>";
      $string = "<!URL2>";
      $lines[$count] =~ s/$string/$link2/; 
} 
      print $lines[$count];
   } while (++$count < $total_lines);

}

# -------------- Random Book or Video ------------------
sub random_book_video() {
   open(INPUT,"../books_video/skidata.txt");
   while (eof(INPUT) == 0) {
      @items = <INPUT>;
   }
   close(INPUT);

   $total = @items;
   $done =0;

     $number = 1 + int(rand($total-1));
     $line = $items[$number];
     chop($line);
     ($type,$asin,$img,$w,$h,$title) = split('\|',$line);
      if(length($title) > 45) {
         $temp = substr($title,0,42);
         $title = $temp."\.\.\.";
      }

      $suggestion = "<center><b>";
      if($type eq "VHS") { $suggestion = $suggestion."<font color=\"#FF0000\">Video Suggestion:</font> "; }
      elsif($type eq "BOOK") { $suggestion = $suggestion."<font color=\"#FF0000\">Book Suggestion:</font> "; }
      $suggestion = $suggestion."<a href = \"http://www.funguide.com/books_video/moreinfo.pl?$asin\" target=\"new\">$title</a> ";
      $suggestion = $suggestion." </b><a href=\"http://www.funguide.com/books_video/show_all.pl\" target=\"new\">[More books & videos]</a>\n";
      $suggestion = $suggestion."</center><br>\n";
}

