#!/usr/local/perl5/bin/perl -T

# PREAMBLE vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
#for security
$ENV{'PATH'}="";

#redirect stderr to stdout so we can see err msgs on the web
$|=1;  #force flush of buffers after each print
open (STDERR, ">&STDOUT") || die "can't dup stdout ($!)\n";

use lib "/w3/utilities"; # my utilities
require "timelocal.pl";   #includes 'timegm' for calculations in gmt
require "cgi-lib.pl";
    
#get directory and URL
use File::Basename;
($basename,$thisDir) = fileparse($0);
#untaint it
$thisDir =~ /([a-zA-Z0-9\.\/\~]*)/;
$thisDir = $1;
$basename =~ /([a-zA-Z0-9\.\/\~]*)/;
$basename=$1;

#change to the proper directory
use Cwd 'chdir'; #use perl version so this isn't unix-dependent
chdir ("$thisDir") ||
          die "Content-type: text/html\n\nCan't cd to $thisDir: $!\n";

#get best return address
$returnAddress = ($ENV{'REMOTE_HOST'} || $ENV{REMOTE_ADDR} ||
                  "(Unknown-Requestor)");

#parse the query string
$parse_val = &ReadParse(*input);

#useful DEBUGGING info vvvvvvvvvvvvvv
#print "Content-type: text/html\n\n";
#print "QS=|$ENV{'QUERY_STRING'}|\n";
#print &PrintVariables(%input);
#end useful DEBUGGING info ^^^^^^^^^^^^^^^^^

#set up some nice variables
@month=(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
@day=(Sun,Mon,Tue,Wed,Thu,Fri,Sat);

#get time for log file
$time = time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time);
#if($year > 99){$year = $year - 100}
$year = $year%=100;
$thisMonth=$month[$mon];

$to = " to ";
$stringout = "$returnAddress";
$blanks = " ";
$denied = "...DENIED....";

# END OF PREAMBLE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#execute stuff here
$logFile = "${thisDir}LAPS_SOFTWARE.cgi.log";
$x = `/bin/head -1 ../software/tarversions.txt | /bin/tail -1`;
$lapsTarFile="laps_$x.tar.gz";
$x=`/bin/head -1 ../software/newlaps_tarversions.txt | /bin/tail -1`;
$newlapsTarFile="laps-$x.tar.gz";
$ftpTarFile="laps-$x.html";
$lapsTerrainFile="laps_terrain.tar.gz";
$globalTerrainFile="world_topo_30s";
$lapsDataTarFile="lapsdata";
$lapsCompiledTarFile="compiled";
$version1 = `/bin/head -1 ../software/newlaps_tarversions.txt | /bin/tail -1`;
$version2 = `/bin/head -2 ../software/newlaps_tarversions.txt | /bin/tail -1`;
$version3 = `/bin/head -3 ../software/newlaps_tarversions.txt | /bin/tail -1`;
$version_data = `/bin/head -1 ../software/lapsdata.version`;

#allow only certain sites to access this
#seawfo.noaa.gov            =       WFO - Seattle                  (Dave Ovens       : ovens@atmos.washington.edu)
#atmos.washington.edu       =           "                               "                        "
#ucar.edu                   =       NCAR                           (Dave Jorgenson   : davej@ncar.ucar.edu)
#ws3.wwb.noaa.gov           =       ?????
#und.nodak.edu              =       UND                            (Bryan Hahn       : bryanh@rwic.und.nodak.edu)
#und.edu                    =        "                                 "                          "
#140.90.186.10              =       WFO - Monterey?                (Dave Reynolds    : )
#ou.edu                     =       OU - CAPS                      (Keith Brewster   : kbrews@thunder.caps.ou.edu)
#204.158.58.7               =       WFO - Lubbock                  (Don Baker        : dvbaker@dryline.nws.noaa.gov)
#lmco.com                   =       Lockheed                       (Dave Bennett     : laps@cyclone.lmms.lmco.com)   
#llnl.gov                   =       Lawrence Livermore             (Robert Lee       : bllee@bleesun2.llnl.gov)
#sage.dir.edu               =       WFO - Reno                     (Mary Cairns      : )
#dri.edu                    =           "                                                          "
#204.228.191                =           "                                                          "
#climate.usu.edu            =       Utah State Univ.
#dis.anl.gov                =       Bosnia                         (                 : simunich@dis.anl.gov)   
#baaqmd.gov                 =       Bay area air quality mangement 
#192.69.142.6               =       Bay area air quality mangement 
#comet.ucar.edu             =       SOO from Hanford at COMAP      (Larry Greiss     : larryg@tamari.comet.ucar.edu)
#204.228.183.10             =       SOO from Hanford at Hanford    (Larry Greiss     : gempak@hay.hnx.noaa.gov)
#198.177.182.100            =       NWS - Western Region 
#sac.dvn.noaa.gov           =       WFO - Davenport
#ssesco.com                 =       ?????                          (                 : dam@ssesco.com)
#192.149.4.6                =       Aerojet                        (                 : kreissb@ms4.aes.com)
#fw1.hitsdn.com             =       Raytheon                       (Magda Hashem     : )
#209.186.167.2              =       Raytheon                       (Magda Hashem     : )
#gandalf.etl.noaa.gov       =       NOAA/ETL                       (Bob Zamora       : x6526)
#129.193.164.137            =       TRW                            (Al Ronn          : Al.Ronn@trw.com)
#129.193.164.81             =        "                                "                       "
#129.193.165.8              =        "                                "
#PCWML.MSFC.NASA.GOV        =       NASA/Marshall                  
#jsc.nasa.gov               =       NOAA/SFMG (NASA/JSC)           (Tim Garner       : )
#fl.ensco.com               =       Hughes                         (John Manobianco  : johnm@FL.ENSCO.COM)
#koyukuk.gi.alaska.edu      =       NWS - FAI                      (Peter Olsson     : olsson@koyukuk.gi.alaska.edu)
#198.206.34                 =       NWS - Sterling VA              (Steven Zubrick   : steven.zubrick@noaa.gov)
#wlwx.noaa.gov              =        "                                "                       "
#bro.noaa.gov               =       NWS - BRO                      (Mark Jackson     : Mark.Jackson@noaa.gov)
#saic.com                   =       SAIC                           (David Bacon      : bacon@apo.saic.com)
#afgwc.af.mil               =       AFGWC                          (Jennifer Roman   : )  
#195.62.164.235             =        "                                "                       "
#aster.com                  =       Aster                          (Craig Tremback   : )
#140.90.12.131              =       NWS - ?                        (Bill Carrigg     : William.Carrigg@noaa.gov)
#nrlmry.navy.mil            =       NRL (Monterey)                 (Qin Xu           : xuq@helium.nrlmry.navy.mil)
#spock.serfc.noaa.gov       =       NWS - ATL                      (Clark Safford    : lcs@serfc.noaa.gov)
#207.64.72.78               =          "                              "                       "
#lma.fi.upm.es              =       Technical University of Madrid (Roberto San Jose : roberto@fi.upm.es)
#wimauma.tbw.noaa.gov       =       Ruskin (Tampa) NWSFO           (Charlie Paxton   : Charlie.Paxton@noaa.gov)
#165.212.229.244            =       FORETELL (iMSC)                (Greg Chesney     : greg.chesney@imsc.com)
#sheba.oce.orst.edu         =       Oregon State University        (Eric Skyllingstad: skylling@oce.orst.edu)
#jhuapl.edu                 =       Johns Hopkins University       (Richard M. Giannola: GiannaRM1@APLMSG.JHUAPL.EDU)
#128.244.68.129             =       Johns Hopkins University       (Richard M. Giannola: GiannaRM1@APLMSG.JHUAPL.EDU)
#niwa.cri.nz                =       New Zealand (Nat. Inst. of Water & Atm Res. Jeff Copeland): j.copeland@niwa.cri.nz)
#las.noaa.gov               =       Las Vegas NWSFO                (Kim Runk         : krunk@cumulus.las.noaa.gov)
#thecount.prc.com           =       IR&D/Litton/PRC weather system (Lee Tsengdar:                                 )
#dryline.nws.noaa.gov       =       Lubbock NWSFO                  (Loren Phillips   : loren.phillips@noaa.gov)   
#ralph.contact-paging.com   =       MCSD Denver                    (Ralph Forsythe   : ralph@contact-paging.com)
#204.133.18*                =              "                              "                      "
#140.90.192.191             =       NCEP Mesoscale Branch          (Matthew Pyle     :)

if($returnAddress =~ /goodplace1\.com/ ||
#  $returnAddress =~ /noaa\.gov/ ||
   $returnAddress =~ /fsl\.noaa\.gov/ ||
   $returnAddress =~ /seawfo\.noaa\.gov/ ||
   $returnAddress =~ /atmos\.washington\.edu/ ||
   $returnAddress =~ /ucar\.edu/ ||
   $returnAddress =~ /ws3\.wwb\.noaa\.gov/ ||
   $returnAddress =~ /und\.nodak\.edu/ ||
   $returnAddress =~ /und\.edu/ ||
   $returnAddress =~ /140\.90\.186\.10/ ||
   $returnAddress =~ /ou\.edu/ ||
   $returnAddress =~ /204\.158\.58\.7/ ||
   $returnAddress =~ /lmco\.com/ ||
   $returnAddress =~ /llnl\.gov/ ||
   $returnAddress =~ /sage\.dir\.edu/ ||
   $returnAddress =~ /dri\.edu/ ||
   $returnAddress =~ /204\.228\.191/ ||
   $returnAddress =~ /CIRA\.ColoState\.EDU/ ||           # CIRA.ColoState.EDU         =       CIRA 
   $returnAddress =~ /climate\.usu\.edu/ ||
   $returnAddress =~ /dis\.anl\.gov/ ||
   $returnAddress =~ /baaqmd\.gov/ ||
   $returnAddress =~ /192\.69\.142\.6/ ||
   $returnAddress =~ /comet\.ucar\.edu/ ||
   $returnAddress =~ /204\.228\.183\.10/ ||
   $returnAddress =~ /198\.177\.182\.100/ || 
   $returnAddress =~ /sac\.dvn\.noaa\.gov/ ||
   $returnAddress =~ /ssesco\.com/ ||
   $returnAddress =~ /192\.149\.4\.6/ ||
   $returnAddress =~ /fw1\.hitsdn\.com/ ||
   $returnAddress =~ /209\.186\.167\.2/ ||
   $returnAddress =~ /gandalf\.etl\.noaa\.gov/ ||
   $returnAddress =~ /129\.193\.164\.137/ ||
   $returnAddress =~ /129\.193\.164\.81/ ||
   $returnAddress =~ /129\.193\.165\.8/ ||
   $returnAddress =~ /PCWML\.MSFC\.NASA\.GOV/ ||
   $returnAddress =~ /jsc\.nasa\.gov/ ||
   $returnAddress =~ /fl\.ensco\.com/ ||                         
   $returnAddress =~ /koyukuk\.gi\.alaska\.edu/ ||
   $returnAddress =~ /198\.206\.34/ ||
   $returnAddress =~ /wlwx\.noaa\.gov/ ||
   $returnAddress =~ /bro\.noaa\.gov/ ||
   $returnAddress =~ /saic\.com/ || 
   $returnAddress =~ /afgwc\.af\.mil/ ||
   $returnAddress =~ /195\.62\.164\.235/ ||
   $returnAddress =~ /aster\.com/ ||
   $returnAddress =~ /140\.90\.12\.131/ ||
   $returnAddress =~ /nrlmry\.navy\.mil/ ||
   $returnAddress =~ /spock\.serfc\.noaa\.gov/ ||
   $returnAddress =~ /207\.64\.72\.78/ ||
   $returnAddress =~ /lma\.fi\.upm\.es/ ||
                                                         # Europe
   $returnAddress =~ /smr\.arpa\.emr\.it/ ||             # Italy                       (Paolo Alberoni   : p.alberoni@smr.arpa.emr.it) 
   $returnAddress =~ /aquila\.infn\.it/ ||               # University of L'Aquila, Italy (Rossella Ferretti: )
   $returnAddress =~ /193\.204\.130\.155/ ||             # Abruzzo, L'Aquila (Italy)   (Memmo Adelaide   : memmo@tesre.bo.cnr.it)
   $returnAddress =~ /tin\.it/ ||                        # Telespazio                  (Luca Pietranera  : gometpz@tin.it)
   $returnAddress =~ /digsys\.bg/ ||                     # Varna Bulgaria              (Angel Dimitrov   : stormlaboratory@yahoo.com)
   $returnAddress =~ /skiron\.mg\.uoa\.gr/ ||            # University of Athens        (George Kallos)   : kallos@skiron.mg.uoa.gr)                              
   $returnAddress =~ /wimauma\.tbw\.noaa\.gov/ ||
   $returnAddress =~ /165\.212\.229\.244/ ||
   $returnAddress =~ /sheba\.oce\.orst\.edu/ ||
   $returnAddress =~ /jhuapl\.edu/ ||   
   $returnAddress =~ /128\.244\.68\.129/ ||
   $returnAddress =~ /niwa\.cri\.nz/ ||
   $returnAddress =~ /ncd\.noaa\.gov/ ||                 # NOS (NWS - Sterling)        (Kevin Fuell      : kevin.fuell@noaa.gov)
   $returnAddress =~ /las\.noaa\.gov/ ||
   $returnAddress =~ /thecount\.prc\.com/ ||
   $returnAddress =~ /dryline\.nws\.noaa\.gov/ ||
   $returnAddress =~ /ralph\.contact-paging\.com/ ||
   $returnAddress =~ /204\.133\.18/ ||
   $returnAddress =~ /colorado-research\.com/ ||         # Colorado Research Assoc.    (John Snook       : snook@colorado-research.com)
   $returnAddress =~ /209\.38\.140\.39/ ||               #         "                        "
   $returnAddress =~ /plansys\.com/ ||                   # PSI                         (Bob Wright)      : rwright@plansys.com)
   $returnAddress =~ /140\.90\.192\.191/ ||
   $returnAddress =~ /137\.51\.15\.225/ ||               # Archive Systems             (Michael Robinson : mrobinson@logicon.com)
   $returnAddress =~ /163\.29\.179\.14/ ||               # Taiwan CWB                  (Yi-Chih Huang    : jh@cwb.gov.tw)
   $returnAddress =~ /cwb\.gov\.tw/ ||                   #     "                             "                   "
   $returnAddress =~ /210\.219\.35\.3/ ||                # Korea (iris.metri.re.kr)    (Yong Sang Kim    : yskim@fsl.noaa.gov)
   $returnAddress =~ /metri\.re\.kr/ ||                  #        "                         "                     "
   $returnAddress =~ /crp\.noaa\.gov/ ||                 # NWS - CRP                   (Waylon Collins   : waylon.collins@noaa.gov)
   $returnAddress =~ /boeing\.com/ ||                    # Boeing                      (Justin Davidson                        )
   $returnAddress =~ /simepar\.br/ ||                    # Brasilia                    (Jose Eduardo Prates : jeprates@simepar.br)
   $returnAddress =~ /192\.168\.99\.71/ ||               # Brazil                      (Jim Edwards      : jedwards@inmet.gov.br)
   $returnAddress =~ /200\.252\.242\.89/ ||              #   "                              "                        "
   $returnAddress =~ /inmet\.gov\.br/ ||                 #   "                              "                        "
   $returnAddress =~ /saopaulo\.sgi\.com/ ||             #   "                              "                        "
   $returnAddress =~ /199\.45\.218/ ||                   # Quetzal Computational Assoc (Daniel R. Fuka   : dan@quetzalcoatl.com)
   $returnAddress =~ /syncrasy\.com/ ||                  #              "                                "
   $returnAddress =~ /wsicorp\.com/  ||                  # WSI                         (Steve Honey      : sjhoney@wsicorp.com)
   $returnAddress =~ /another_place\.com/ ||       
   $returnAddress =~ /last_place\.com/         ) {
 #let this guy in
} else {
 #throw this guy out
  open(LOG,">>$logFile") ||

    die "Content-type: text/html\n\nCan't open $logFile: $!\n";
#    printf LOG "%10d%02d%02d%02d%02d to $returnAddress " .
#    "$input{'request'}\n ... DENIED... \n"
#    ,$year,$mon+1,$mday,$hour,$min
#    ;

    printf LOG "%01s%02d%02d%02d%02d%02d%35s%04s%05s" .
    "\n"
    ,$blanks,$year,$mon+1,$mday,$hour,$min,$denied,$to,$stringout
    ;
  close LOG;
  die "Content-type: text/html\n\n Sorry, Access Denied. Please contact the LAPS group (mcginley\@fsl\.noaa\.gov) if you wish to register for free access. We will need your IP address (numeric characters) and domain name (alphabetic characters). There is no restriction on the use of LAPS.\n";
}

#restrict.sh takes care of telling the user s/he is denied
#system("restrict.sh $returnAddress $allowedHosts");
#system("/w3/lapb/cgi/restrict.sh $returnAddress $allowedHosts >> $logFile");
#if ($? == 1) 
#        {
# 	echo "`date -u +%t%y%m%d%H%M`,  .........DENIED.........      $returnAddress" >> $logFile;
# 	print "........DENIED.........      $returnAddress" >> $logFile;

#	}

if($parse_val == 0 ) {
    #original call, with no arguments
#now tell the world
print<<"EOI";
Content-Type: text/html

<html><head>
<TITLE>LAPS software</TITLE>
</head>  
<!-- <body bgcolor="white"> -->
<body background="/icons_laps/LongerLAPS.gif">   

<p>
<H2>
<FONT COLOR="#AA0000">
Notice: We are experiencing some intermittent server or network problems that 
can corrupt the downloading of tar files. If you find that you cannot 'gunzip'
a tar file, please download the file and try again a couple of times. You
might also send us an 
<a href="mailto:laps-bugs\@fsl.noaa.gov,moninger\@fsl.noaa.gov,buennemeyer\@fsl.noaa.gov">email</a>
reporting the details to help us track
it down. Thank you for your patience.
</FONT>
</H2>

<p>

<H1>Latest release of LAPS software ($version2):</H1>

<H2>

<ul>

<li>LAPS software tar file:
<!--
<a href="LAPS_SOFTWARE.cgi?request=${ftpTarFile}">
<a href="ftp://bora.fsl.noaa.gov/pub/frd-laps/laps-$version1.tar.gz">
-->
<a href="LAPS_SOFTWARE.cgi?request=${newlapsTarFile}">
laps-$version1.tar.gz</a>
($version3 Bytes)

<p>

<li>You will want to download the first and possibly the second of the
following geography databases.

<ul>

<p>

<li>
The first one,
<a href="LAPS_SOFTWARE.cgi?request=${lapsTerrainFile}">
laps_terrain.tar.gz</a> 
has 11165240 Bytes. 
This has 10' resolution land/water fraction data.
This also has 30" topography over the
contiguous U.S. with 10' resolution globally.
</li>

<p>
<li>
The second, improved topography from the USGS, is
<a href="LAPS_SOFTWARE.cgi?request=${globalTerrainFile}">
<!-- <a href="/lapb/geog/world_topo_30s"> -->
Worldwide 30" Terrain</a>.
</li>

</ul>

<p>

<li>
<a href="/software/README_$version1.html">
README</a> file.

<p>

<li>LAPS test data:
<a href="LAPS_SOFTWARE.cgi?request=${lapsDataTarFile}">
lapsdata directory</a> 
(version $version_data) 

<p>

<li>
LAPS 
<a href="LAPS_SOFTWARE.cgi?request=${lapsCompiledTarFile}">
Pre-Compiled Tar Files
</a> (optional) 

<p>

<li>
<a href="/software/release_notes.html">
Release notes</a>,
including the 
<a href="/frd/laps/software/REVISIONS">REVISIONS</a> log.

<p>

<li>
<a href="/builds/laps_builds.html">
Results of Latest LAPS Builds</a>

</ul>

</H2>

<IMG SRC="/albers/colorbar.gif">

<br>

<!--
<a href="/software/laps_af_patch-$version1.tar.gz">
AFGWC</a> Patch.
|
<a href="/software/rsa">
RSA</a>
|
<a href="/software/buoy">
Buoy Ingest</a>

<br>

<IMG SRC="/albers/colorbar.gif">

-->

<p>

<a href="LAPB.homepage.cgi">(Back to LAPB homepage)</a>
<p>

Feedback:
<a href="mailto:laps-bugs\@fsl.noaa.gov"><i>laps-bugs\@fsl.noaa.gov</i></a>
|
<a href="/LAPB/laps-users.html">LAPS Users Mailing List</a>

<p>
<img src=/icons/btn-bar.gif border="0" 
  alt="navigation bar" usemap="#btn_map">
<map name="btn_map">
<area shape="circle" coords="23,19,14"  alt="FSL"
        href="http://www.fsl.noaa.gov/fsl/">
<area shape="rect" coords="42,6,108,33" alt="whatsnew" 
        href="http://www.fsl.noaa.gov/fsl/docs/wnew/fsl-whatsnew.html">
<area shape="rect" coords="114,6,186,33" alt="organization"
        href="http://www.fsl.noaa.gov/fsl/docs/orgn/fsl-organization.html">
<area shape="rect" coords="192,5,241,33" alt="current weather"
        href="http://www.fsl.noaa.gov/fsl/docs/wthr/fsl-weather.html">
<area shape="rect" coords="247,6,277,33" alt="data"
        href="http://www.fsl.noaa.gov/fsl/docs/data/fsl-data.html">
<area shape="rect" coords="282,6,331,33" alt="projects"
        href="http://www.fsl.noaa.gov/fsl/docs/proj/fsl-projects.html">
<area shape="rect" coords="336,6,406,33" alt="publications"
        href="http://www.fsl.noaa.gov/fsl/docs/publ/fsl-publications.html">
<area shape="rect" coords="413,5,466,33" alt="software"
        href="http://www.fsl.noaa.gov/fsl/docs/soft/fsl-software.html">
<area shape="circle" coords="482,19,14" alt="search our site"
        href="http://www.fsl.noaa.gov/fsl/docs/welc/help/fsl-search-ns.html">
<area shape="rect" coords="0,6,500,32" alt="FSL"
        href="http://www.fsl.noaa.gov/fsl/">
</map><br>
<font size="-2">[ <a href="http://www.fsl.noaa.gov/fsl/">FSL</a> | 
<a href="http://www.fsl.noaa.gov/fsl/docs/wnew/fsl-whatsnew.html">What's
New</a> | <a
href="http://www.fsl.noaa.gov/fsl/docs/orgn/fsl-organization.html">Organization</a>
| <a
href="http://www.fsl.noaa.gov/fsl/docs/wthr/fsl-weather.html">Weather</a>
| <a href="http://www.fsl.noaa.gov/fsl/docs/data/fsl-data.html">Data</a>
| <a
href="http://www.fsl.noaa.gov/fsl/docs/proj/fsl-projects.html">Projects</a>
| <a
href="http://www.fsl.noaa.gov/fsl/docs/publ/fsl-publications.html">Publications</a>
| <a
href="http://www.fsl.noaa.gov/fsl/docs/soft/fsl-software.html">Software</a>
| <a
href="http://www.fsl.noaa.gov/fsl/docs/welc/help/fsl-search-ns.html">Search</a>
]</font>
<br>
<font size="-1">
<br>
<!-- the tags 'hhmts start' and 'hhmts stop' are used by HTML helper mode
     to insert the modified date every time this file is saved in emacs -->
<!-- hhmts start -->
Last modified: Thu Feb  4 11:26:17 1999
<!-- hhmts end -->

</body></html>
EOI
} else {
print <<"EOI";
Location: ../software/restrct/$input{'request'}

EOI
}
# POSTLUDE vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

#log accesses

open(LOG,">>$logFile") ||
    die "Content-type: text/html\n\nCan't open $logFile: $!\n";
    printf LOG "%01s%02d%02d%02d%02d%02d%35s%04s%05s" .
    "\n"
    ,$blanks,$year,$mon+1,$mday,$hour,$min,$input{'request'},$to,$stringout
    ;
close LOG;