#!/bin/sh

#
# generate the example page for WWW Homepage Access Counter
#
# ma_muquit@fccc.edu
# Nov-15-1997
#


trap 'echo '';echo "Interrupt Detected..exiting";/bin/rm -f test_counter*.gif
2>/dev/null; echo '';exit 1' 1 2 3 15


#
# conter version
#
VER=`cat ../VERSION`
DVER=`cat ../VERSION|sed 's/\./_/g'`
DEBUG=1

#
# program version
#
VERSION="1.2"
PROGNAME="testcount-sh"
BDIR="Count$VER"

#
# my URL
#
MYURL="http://www.fccc.edu/users/muquit/"

#
# official Counter URL
# lets make it relative. mm, jan-24-1999
#
CURL="Count.html"

EXDIR="$BDIR/eximages"
if [ ! -d $EXDIR ]; then
    echo "making directory $EXDIR"
    mkdir -p $EXDIR
fi

echo "/bin/rm -f $EXDIR/ex$DVER*.gif"

/bin/rm -f $EXDIR/ex$DVER*.gif

#
# Counter program
# 
uname=`uname`
#
# I run cygwin32 unix stuff
#
if [ $uname = "CYGWIN_NT-4.0" ]; then
    CPROG="Count.exe"
    COUNTER="/Xitami/cgi-bin/$CPROG"
else
    CPROG="Count.cgi"
    COUNTER="../src/$CPROG"
fi

if [ ! -f $COUNTER ]; then
    echo "Counter program $COUNTER does not exist.. aborting test"
    exit 0
fi

echo "program= $COUNTER"

#
# the name of html file with all examples
#
EXF="Count$DVER-ex.html"

#
# we will save the image to a file with the called QUERY_STRING
# and will call netscape with -remote to display the file
#

#
# source to get env variables
#
ENVV_FILE="./env.dat"


PWD=`pwd`

. $ENVV_FILE

#
# read through the file which contains various types of QUERY_STRING
#
QS_FILE="./qs.dat"

j=0

#
# head of the html page
#
if [ -f $EXF ]; then
    echo "echo /bin/rm -f $EXF"
    /bin/rm -f $EXF
fi

#
# redirect stdout to html file
#
exec 1>$EXF
date=`date`

cat<<EOH
<html>
    <head>
        <title>Example page for Count $VER</title>
        <link rev=made href="mailto:ma_muquit@fccc.edu">
    </head>
<body bgcolor="#c0c0c0">
<center>
<font size="+1"><b>Example page for Count $VER</b></font>
<p>
<b>Generated by the program $PROGNAME</b>
<br>
</center>
<p>
For NT and other platforms, replace Count.cgi with Count.exe.
In the examples, few number of digits are used for brevity, by default, the 
counter can handle 80 digit long number (10^80)-1, which is a very
large number, that is if data is not edited by hand, it is 
impossible to overflow the counter. These tests use 
<a href="Count.html#conf">count_reload=Yes</a>, so you will see consecutive hits
are incremented.
Note: these examples do not cover all
the capabilities of the counter. Please read the 
<a href="$CURL">Counter documentation</a> for details.
<p>
EOH

#
# scan through the definition file. strip off comment and empty line
#

for i in `sed '/^#/d;/^$/d' $QS_FILE`
do
    COMMENT=`echo $i | awk -F% '{print $2}' | sed "s/_/ /g"|sed "s/\^/_/g"`
    QUERY_STRING=`echo $i | awk -F% '{print $1}'`
    export QUERY_STRING
    echo "<table border=2 cellpadding=5 width=\"100%\">"

cat<<EOC
    <tr>
        <td align=center bgcolor="#99ccff">
            <b>$COMMENT</b>
        </td>
    </tr>
EOC
    h=`echo $QUERY_STRING | sed 's/&/&amp;/g'`
cat<<EOQ
    <tr>
        <td align=center>
            <code>&lt;img src="/cgi-bin/$CPROG?$h"&gt;</code>
        </td>
    </tr>
EOQ

    #
    # make a unique filename each time 
    #
    j=`expr $$ + $j`
    filename="$EXDIR/ex$DVER$j.gif"
    if [ $DEBUG = 0 ]; then
        $COUNTER  -debug > $filename 2>/dev/null
    else
        $COUNTER  -debug > $filename
    fi

    echo "<tr>"
    echo "<td align=center>"
    echo "<img src=\"$filename\" alt=\"$h\" align=absmiddle>" 
    echo "</td>"
    echo "</tr>"
    echo "</table>"
    echo "<p>"
done

date=`date`
uname=`uname -a`
cat<<EON
<a href="$MYURL">Muhammad A Muquit</a>
<br>
Generated on: $date by <code>$PROGNAME ($VERSION)</code>
<br>
<font size="-1">
($uname)
</font>
</body>
</html>
EON
&                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  