#!/bin/bash
#
# add_record.sh
#
# Part of the Claims_DB Sample front end
# Copyright Ben Tasker 2009
# Released under the GNU GPL
# See http://benscomputer.no-ip.org/LICENSE
#


# Define your 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"



# Tell the browser our MIME Type
echo Content-type: text/html
echo ""


# Generate the HTML

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

# What will our Primary Key be?

# Read the entire table
DBROOT="$DBNAME" TABLE="Catalogue" "$CLAIMS_PROGS"/bin/read_records_claims.sh -read > /tmp/LOCATIONS.tmp

# PK is in column 1
# Now seperate out Column 1, and remove duplicates
awk -F\, '{print $1}' /tmp/LOCATIONS.tmp > /tmp/LOCATS2.tmp


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

# Now lets sort it
cat /tmp/LOCATS.tmp | sort -n -k 1 > /tmp/LOCATS2.tmp

# What is the final entry, load it into a variable

LASTKEY=$( cat /tmp/LOCATS2.tmp | tail -n 1 )

# Work out the next key with a simple bit of maths

NEWKEY=$(( $LASTKEY + 1 ))

# Tidy up

rm -f /tmp/LOCATS.tmp
rm -f /tmp/LOCATS2.tmp
rm -f /tmp/LOCATIONS.tmp

# Continue to generate the HTML

<FORM ACTION="add_process.sh" METHOD="GET">
Record ID: $NEWKEY
<br><br>
<INPUT TYPE="hidden" NAME="RecordID" VALUE="$NEWKEY">
Part Description: <INPUT TYPE="text" NAME="Desc">
<br>Part Location: <INPUT TYPE="text" NAME="Locat">
<br>Part Number: <INPUT TYPE="text" NAME="Partno">
EOM

# Use drop down box for Number type
/bin/cat << EOM
Part Number Type: <select name="NoType"> 
<OPTION VALUE="0">OEM</OPTION>
<OPTION VALUE="1">Stockist</OPTION>
</select>
EOM


# Generate rest of form
/bin/cat << EOM
Stockist: <INPUT TYPE="text" NAME="Stkis">
<br>
Qty in Pack: <INPUT TYPE="text" NAME="PPQ">
<br>
Qty on Bike: <INPUT TYPE="text" NAME="Need">
<br>
Rating: <INPUT TYPE="text" NAME="Rate">
<br>
Bike: <INPUT TYPE="text" NAME="bike">
<br><br>
<INPUT TYPE="submit" VALUE="Save Record">
</form>
<br><br>
</body>
</html>
EOM

# Generation finished
exit






