#!/bin/bash
#
# Index page for Claims_DB front end.
# Copyright Ben Tasker 2009
#
# Released under the GNU GPL
# See http://benscomputer.no-ip.org/LICENSE
#

# Set Variables here

# Where did you install Claims_DB?
CLAIMS_PROGS="/home/ben/programs/Claims_DB/"

# The name of the Database we are using
DBNAME="Sample_DB"



# Insert functions here - If any









# Function Main!

# Tell the browser what mime type we are using
echo Content-type: text/html
echo ""

# May as well start the HTML generation

/bin/cat << EOM
<html><head><title>Claims_DB Sample Database Frontend</title>
<body bgcolor="white">
<center><b>Claims_DB Sample Database Frontend</b></center><br><br>
EOM

# Lets create our Add record button

/bin/cat << EOM
<!-- Pass user to the add record CGI -->
<FORM ACTION="add_record.sh" METHOD="GET"><INPUT TYPE="SUBMIT" VALUE="Add Record"></FORM>
<br><br>
<!-- Section ends -->
EOM


# Now lets create the drop down box for our report


/bin/cat << EOM
<!-- User to select Query criteria, then send them to the report generator -->
<FORM ACTION="gen_report.sh" METHOD="GET">
<SELECT NAME="CRITERIA">
EOM

# Now we want to run our own query to provide the user with valid criteria
# We want to run a SELECT DISTINCT style query.

# Grab the available locations, we know they are stored in Column 3

# Until new functionality is implemented, we need to grab the entire table
DBROOT="$DBNAME" TABLE="Catalogue" "$CLAIMS_PROGS"/bin/read_records_claims.sh -read > /tmp/LOCATIONS.tmp

# Now seperate out Column 3, and remove duplicates
awk -F\, '{print $3}' /tmp/LOCATIONS.tmp > /tmp/LOCATS2.tmp
awk '{
if ($0 in stored_lines)
   x=1
else
   print
   stored_lines[$0]=1
}' /tmp/LOCATS2.tmp > /tmp/LOCATIONS.tmp 


# Strip out the text delimeter - "
sed s/\"//g /tmp/LOCATIONS.tmp > /tmp/LOCATS2.tmp

# Read the resulting file line by line and create an Option value for each
while read -r a                    
do   
/bin/cat << EOM
<option value="$a">$a</option>
EOM
done < /tmp/LOCATSS2.tmp

# Tidy up!!!!!!!!
rm -f /tmp/LOCATIONS.tmp 2> /dev/null
rm -f /tmp/LOCATS2.tmp 2> /dev/null

# Now finish creating the form

/bin/cat << EOM
</select>
<INPUT TYPE="submit" Value="Generate Report"></form>
<br><br>
</body>
</html>
EOM

# Page generation finished
exit

