#!/bin/sh 
set -a;HOME=`ls -d /home*/*|egrep -i 'rishen$|a45|/n$'|tail -1`; HOME=${HOME:-/homes/rishen}; BS=$HOME/Bin.sys; Binsun=$HOME/Bin.sun; PATH=$HOME/bin:$Binsun:$HOME/UCSB/BIN:$HOME/Bin.Linux:$Binsun/LINUX:$PATH; logdir=$HOME/Z; rishen=$HOME; cgidir=`echo $PWD|sed 's/.*fiu.edu[.]//'`; webdir=$DOCUMENT_ROOT; http=http://$SERVER_NAME;n1=$webdir; TZ=America/New_York; lynxs=$Binsun/lynxscurl; lynx=$BS/lynx; gawk=$BS/gawk; myprintenvif
if (restrict-cgi) then exit; fi
t=$HOME/Z/gnis1.cgi;t1=$t.1
#BS=$rishen/Bin.sys
#lynx=/usr/bin/lynx;lynx="/usr/local/bin/lynx"; gawk="/usr/local/bin/gawk"; 
#lynx=$BS/lynx; gawk=$BS/gawk
#lynxs="$lynx -source"
#temp20120909
streen=3
te=$t.stderr
#(
case $HOST_NAME$Hostname$HOSTNAME$SERVER_NAME in
#case iiiI in
*n0* ) is_n0=1;;
*.com ) is_com=1; redirector="http://vn4.cs.fiu.edu/cgi-bin/redirector.cgi?";;
esac

if (test -n "$is_n0" )
then
for e in `set|egrep '(SERVER|HTTP|REQUEST|HOST|REMOTE)'|/bin/sed 's/=.*//;/^[^a-zA-Z]/d;/SHELLOP/d'`
do
echo "$e">> $te.1
export $e
#export is_n0
done
fi

case "$QUERY_STRING" in
#*referer=miamire*folioref=*) QUERY_STRING=`mlsref2url `; arquery.cgi;exit 0;;
*referer=miamire*folioref=*) QUERY_STRING=`mlsref2url `; redirecth "http://vn4-2.cs.fiu.edu/cgi-bin/arquery.cgi?$QUERY_STRING";exit 0;;
esac


lasttga=`cat $rishen/Bin.sun/.lasttga`
#ht=hotels-shopper.com/scripts/travelbar.js
ht=/scripts/travelbar.js
#terrapan='http://n129.cs.fiu.edu/terrapan.html'
#terrapan='http://books-maps.com/gnisterrapan0.html'

SERVER_NAME=${SERVER_NAME:-n00.cs.fiu.edu}
#if [ "$SERVER_NAME" = n3.cs.fiu.edu ]
#then
#logdir=/lclhome/n3-http/logs  
#cgidir=/lclhome/n3-http/cgi-bin
#fi
#if [ "$SERVER_NAME" = n2.cs.fiu.edu ]
#then
#logdir=/fiu/oldn2b/n2-http/logs  
#cgidir=/fiu/oldn2b/n2-http/cgi-bin
#fi


#if (test 1 = 2 )
#if (test `date +%S` -ge 0  )
if (test -z "$HTTP_COOKIE" -o "$HTTP_COOKIE" = "vid=" -o "$QUERY_STRING" = sony ) 
then
#if (test "$QUERY_STRING" = sony )
if (test -f $logdir/testload -o "$QUERY_STRING" = sony )
#if (test -s $rishen/Z/testload )
then
#echo Content-Type: text/html; echo "";echo Point information is temporarily unavailable.  Please return in a couple of hours.;exit
cat <<ENDALL
Content-Type: text/html; 

While the full PointData page is in maintenance for the next few hours, we bring you our TerraFly imagery browser and a universal information search.
ENDALL
# !(mlike "$QUERY_STRING" "vid=(mreals|fdep)") && exit #20250222
alert=$t.alert.`date +%Y%m%d`
if (test -f $alert )
then
alerted=1
else
date> $alert
echo gnis load |mail -s "gnis load alert" rishen@cs.fiu.edu
fi
exit
fi
fi
if (test "$SCRIPT_NAME" = /cgi-bin/gnis1.cgi )
then
tester=1
fi
case "$SCRIPT_NAME$0" in
*1.cgi* ) tester=1;;
esac
if (test -z "$QUERY_STRING")
then
QUERY_STRING=sony
fi
if (test "$QUERY_STRING" = sony) 
then
#QUERY_STRING="place_type=park&place_name=Loyola+College&county=Miami-Dade&state=FL&lat=25.748251&long=-80.36299"
QUERY_STRING="Lat=25.748251&Long=-80.36299"
fi
#zipcode=`$rishen/Bin.sun/coordinates2zipcode "$QUERY_STRING"`
#zipcode_town=`$rishen/Bin.sun/coordinates2zipcode+ "$QUERY_STRING"`
#echo $zipcode === $zipcode_town
#ziprecord=`/home/n2/compsci/sun4/bin/zipcode $zipcode`

case "$QUERY_STRING" in
  *pixel_of_max_long*[0-9],[0-9]* ) QUERY_STRING="`$rishen/Bin.sun/minlat2lat$tester`";;
esac


#tempoff20081031 removing matchprop: 
matchprop=2
QUERY_STRING="tester="$tester"&gnis0=1&matchprop=$matchprop&$QUERY_STRING"
case "$QUERY_STRING" in
 *Lat*|*lat*|*long*|*LAT*|*LONG* ) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/[Ll]atitude=/lat=/g;s/[Ll]ongitude=/long=/g;s/lat=/Lat=/g;s/long=/Long=/g;s/LAT=/Lat=/g;s/LONG=/Long=/g'`;;
esac


case "$QUERY_STRING" in
#*tfaction=arquery*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=arquery/category=/'`;$rishen/Bin.sun/arquery.cgi;exit 0
*tfaction=MiamiCompTax*) QUERY_STRING=`echo "$QUERY_STRING"&category=MiamiCompTax&filetype=.xml/'`; echo Content-Type: text/xml; echo "";  $lynxs "http://n6.cs.fiu.edu/cgi-bin/condorep$tester.cgi?$QUERY_STRING"|$rishen/Bin.sun/stripascii2fhtml$tester.soelim;exit 0;;
*tfaction=arqueryCndxMLSPhotos*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=arquery[a-zA-Z-]*/vid=mreal\&category=allreal\&choosefields=photo+folio_number+mls+status+street_address\&filetype=.xml/'`; echo Content-Type: text/xml; echo "";  $lynxs "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";exit 0;;
*tfaction=arquery*filetype=.csv*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=arquery/category=/'`; echo Content-Type: text/csv; echo ""; $lynxs "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";exit 0;;
*tfaction=arquery*filetype=.json*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=arquery/category=/'`; echo Content-Type: text/json; echo ""; $lynxs "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";exit 0;;
*tfaction=arquery*filetype=.xml*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=arquery/category=/'`; echo Content-Type: text/xml; echo ""; $lynxs "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";exit 0;;
*tfaction=arquerycensusmap*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/x1/long/;s/y1/lat/;'`; echo Content-Type: text/html; echo ""; echo $lynxs "http://n6.cs.fiu.edu/cgi-bin/d$tester.cgi?tigermaps=&$QUERY_STRING&bigmap=1";exit 0;;
*tfaction=arquery*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=arquery\([a-zA-Z0-9_-]*\),*/category=\1\&tfaction=/'`; redirecth "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";exit 0;;
*tfaction=photos*) QUERY_STRING=`echo "$QUERY_STRING"|/bin/sed 's/tfaction=photos/category=geoimages\&tfaction=shortdisplayphotos/'`; redirecth "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";exit 0;;
esac

#echo $zipcode === $zipcode_town === $ziprecord === $QUERY_STRING
#$rishen/Bin.sun/d${tester}.cgi
#stree="n158.cs.fiu.edu:8080"
#stree="stree.cs.fiu.edu:8081"
#stree="stree1.cs.fiu.edu:8080"
#stree="stree2.cs.fiu.edu:8080"
stree="stree$streen.cs.fiu.edu:8080"
#20220228:
stree=midway.cs.fiu.edu/GeocodingServiceLegacy/
#stree="stree3.cs.fiu.edu:8080"
#if (test -n "$tester")
#then
#stree="stree1.cs.fiu.edu:88"
#fi
hstree="http://"$stree
QUERY_STRING="center=1&bbox=1&sreq=gnisstr&$QUERY_STRING"
#QUERY_STRING="sreq=gnisstr&$QUERY_STRING"
#$lynxs "$hstree/request?$QUERY_STRING"|
program=$rishen/Bin.sun/tfallobjects
fullprogram=$program$tester.soelim
case $QUERY_STRING in
*tfaction=censusonly*) program=$rishen/Bin.sun/gnisgrep;;
esac
fullprogram=$program$tester.soelim
if (test "$tester" = "1" )
then
#/usr/bin/soelim $program$tester|$gawk '/^\./{next}{print}'> $fullprogram
$Binsun/soelimg $program$tester|$gawk '/^\./{next}{print}'> $fullprogram
chmod a+x $fullprogram
fi
fullstreeurl="$hstree/request?$QUERY_STRING"
if (test -n "$testerxxx")
then
#echo Content-Type: text/html
echo ""
echo "<p><b><a href=$fullstreeurl>$fullstreeurl</a></b></p>"
fi
$rishen/Bin.sun/getstree$tester|
grep -v gnsw|
#/bin/sed 's/[^ -~	]//g'|
#$rishen/Bin.sun/tfallobjects$tester
$fullprogram

if (echo "$QUERY_STRING"| grep -i filetype=.xml>/dev/null)
then
hstree=http://midway.cs.fiu.edu:8080/sks/
case $QUERY_STRING in
*censusonly* ) exit 0;;
*Lat=* ) ok=1;;
*LAT=* ) ok=1;;
*x1=* ) ok=1;;
*lat=* ) ok=1;;
#20190124 off temp to drop geocoding * ) QUERY_STRING=`($lynxs $hstree/street"?$QUERY_STRING"|/usr/bin/tr '\012' ' '; echo "&")| /bin/sed 's/ *X= */Long=/;s/[	 ]*Y= */\&Lat=/;s/[ 	]*Level=.[ 	]/\&place_name=/;s/[ 	]/+/g'``echo "$QUERY_STRING"|/bin/sed 's/street=[^&]*&//;s/state=[^&]*&//;s/city=[^&]*&//;s/zip=[^&]*//'`;;
#* ) QUERY_STRING=`($lynxs $hstree.cs.fiu.edu/street"?$QUERY_STRING"|/usr/bin/tr '\012' ' '; echo "&")| /bin/sed 's/ *X= */Long=/;s/[	 ]*Y= */\&Lat=/;s/[ 	]*Level=.[ 	]/\&place_name=/;s/[ 	]/+/g;s/Lat=/y1=/g;s/Long=/x1=/'``echo "$QUERY_STRING"|/bin/sed 's/street=[^&]*&//;s/state=[^&]*&//;s/city=[^&]*&//;s/zip=[^&]*//;s/Lat=/y1=/;s/Long=/x1=/'`;;
esac
#echo "Q=$QUERY_STRING"
#orig="$QUERY_STRING"
     #orig="$QUERY_STRING&dxuf=1"
orig=`echo "$QUERY_STRING"|/bin/sed 's/Lat=/y1=/g;s/Long=/x1=/g'`"&dxuf=1&more=p"
#echo "orig=$orig"
cats="nfolio airport bcndx cndx college geoimages hotels ikonos newgnis nfolio ngagns pbg pblock pcongress pcounty pincorp pmetro private_schools ptract public_schools pzipcode wcity wtown3 ypages"

case $QUERY_STRING in
*ProP*) cats="nfolio flproperties";;
*LighT*) cats="nypages nfolio&OWNER=ref airport college hotels private_schools public_schools ypages" ;;
esac
for urlcat in $cats
do
#QUERY_STRING="category=$urlcat&more=p&$orig"
QUERY_STRING="category=$urlcat&$orig"
 #$lynxs "http://$SERVER_NAME/cgi-bin/arquery.cgi?$QUERY_STRING";
 #$lynxs "http://$SERVER_NAME/cgi-bin/arquery$tester.cgi?$QUERY_STRING";
 #echo $lynxs "$hstree/query?$QUERY_STRING&n=10&d=50&header=1"
 $lynxs "$hstree/query?$QUERY_STRING&numfind=10&d=50&header=1&printdist=1"|$rishen/Bin.sun/stripascii2fhtml.soelim;
if (test "$tester" = 1 )
then
echo "<done>$urlcat "|/bin/sed 's/[&]/ /g'
date
echo "</done>"
fi
done|
grep -v "xml version"

echo "</PointData>"
fi


if (test "$tester" = 1 )
then
#echo "<a href=$hstree/request?$QUERY_STRING>stree</a>"
#echo "<a href=http://n6.cs.fiu.edu/cgi-bin/showstree.cgi?$QUERY_STRING>sstree</a>"
echo >/dev/null
fi
#) 2> $te
