#!/bin/bash
#
# add_process.sh
#
# Part of the Claims_DB Sample DB 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 ""


# Run /bin/env to get environment details
REQUEST_URI=$( /bin/env | grep "REQUEST_URI" )

# Now we need to seperate out each of the elements.

# Primary Key
RECORDID=$( echo "$REQUEST_URI" | sed -n 's/^.*RecordID=\([^&]*\).*$/\1/p' )

# Description
DESCRIPTIONA=$( echo "$REQUEST_URI" | sed -n 's/^.*Desc=\([^&]*\).*$/\1/p' )

# Part Location
LOCATIONA=$( echo "$REQUEST_URI" | sed -n 's/^.*Locat=\([^&]*\).*$/\1/p' )

# Part Number
PARTNOA=$( echo "$REQUEST_URI" | sed -n 's/^.*Partno=\([^&]*\).*$/\1/p' )

# Number Type
NUMBERTYPE=$( echo "$REQUEST_URI" | sed -n 's/^.*NoType=\([^&]*\).*$/\1/p' )

# STOCKIST
STOCKISTA=$( echo "$REQUEST_URI" | sed -n 's/^.*Stkis=\([^&]*\).*$/\1/p' )

# Quantity in pack
PPQ=$( echo "$REQUEST_URI" | sed -n 's/^.*PPQ=\([^&]*\).*$/\1/p' )

# Quantity on bike
QOB=$( echo "$REQUEST_URI" | sed -n 's/^.*Need=\([^&]*\).*$/\1/p' )

# Rating
RATINGA=$( echo "$REQUEST_URI" | sed -n 's/^.*Rate=\([^&]*\).*$/\1/p' )

# Bike
BIKEA=$( echo "$REQUEST_URI" | sed -n 's/^.*bike=\([^&]*\).*$/\1/p' )

# Translate NUMBERTYPE into the value to be inserted into the database

if [ "$NUMBERTYPE" == "0" ]
then
NUMBERTYPE="OEM"
else
NUMBERTYPE="Stockist"
fi


# Remove URL encoding from those that may have it

# Strip out any HTML nuances here, change + to space etc.


DESCRIPTION=$( echo "$DESCRIPTIONA" | sed "s/%0D%0A/<br>/g" | sed "s/+/ /g" | sed "s/%21/\!/g" | sed "s/%40/@/g" | sed "s/%20/ /g" | sed "s/%26/\&/g" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" | sed "s/%2F/\//g" | sed "s/%5C/\\\/g" | sed "s/%24/$/g" | sed "s/%A3/£/g" | sed "s/%27/\'/g" | sed "s/%23/\#/g" | sed "s/%3A/\:/g" | sed "s/%5B/\[/g" | sed "s/%40/@/g" | sed "s/%5D/\]/g" | sed "s/%25/\%/g" | sed "s/%5E/\^/g" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )


LOCATION=$( echo "$LOCATIONA" | sed "s/%0D%0A/<br>/g" | sed "s/+/ /g" | sed "s/%21/\!/g" | sed "s/%40/@/g" | sed "s/%20/ /g" | sed "s/%26/\&/g" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" | sed "s/%2F/\//g" | sed "s/%5C/\\\/g" | sed "s/%24/$/g" | sed "s/%A3/£/g" | sed "s/%27/\'/g" | sed "s/%23/\#/g" | sed "s/%3A/\:/g" | sed "s/%5B/\[/g" | sed "s/%40/@/g" | sed "s/%5D/\]/g" | sed "s/%25/\%/g" | sed "s/%5E/\^/g" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )


PARTNO=$( echo "$PARTNOA" | sed "s/%0D%0A/<br>/g" | sed "s/+/ /g" | sed "s/%21/\!/g" | sed "s/%40/@/g" | sed "s/%20/ /g" | sed "s/%26/\&/g" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" | sed "s/%2F/\//g" | sed "s/%5C/\\\/g" | sed "s/%24/$/g" | sed "s/%A3/£/g" | sed "s/%27/\'/g" | sed "s/%23/\#/g" | sed "s/%3A/\:/g" | sed "s/%5B/\[/g" | sed "s/%40/@/g" | sed "s/%5D/\]/g" | sed "s/%25/\%/g" | sed "s/%5E/\^/g" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )


STOCKIST=$( echo "$STOCKISTA" | sed "s/%0D%0A/<br>/g" | sed "s/+/ /g" | sed "s/%21/\!/g" | sed "s/%40/@/g" | sed "s/%20/ /g" | sed "s/%26/\&/g" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" | sed "s/%2F/\//g" | sed "s/%5C/\\\/g" | sed "s/%24/$/g" | sed "s/%A3/£/g" | sed "s/%27/\'/g" | sed "s/%23/\#/g" | sed "s/%3A/\:/g" | sed "s/%5B/\[/g" | sed "s/%40/@/g" | sed "s/%5D/\]/g" | sed "s/%25/\%/g" | sed "s/%5E/\^/g" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )


RATING=$( echo "$RATINGA" | sed "s/%0D%0A/<br>/g" | sed "s/+/ /g" | sed "s/%21/\!/g" | sed "s/%40/@/g" | sed "s/%20/ /g" | sed "s/%26/\&/g" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" | sed "s/%2F/\//g" | sed "s/%5C/\\\/g" | sed "s/%24/$/g" | sed "s/%A3/£/g" | sed "s/%27/\'/g" | sed "s/%23/\#/g" | sed "s/%3A/\:/g" | sed "s/%5B/\[/g" | sed "s/%40/@/g" | sed "s/%5D/\]/g" | sed "s/%25/\%/g" | sed "s/%5E/\^/g" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )


BIKE=$( echo "$BIKEA" | sed "s/%0D%0A/<br>/g" | sed "s/+/ /g" | sed "s/%21/\!/g" | sed "s/%40/@/g" | sed "s/%20/ /g" | sed "s/%26/\&/g" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" | sed "s/%2F/\//g" | sed "s/%5C/\\\/g" | sed "s/%24/$/g" | sed "s/%A3/£/g" | sed "s/%27/\'/g" | sed "s/%23/\#/g" | sed "s/%3A/\:/g" | sed "s/%5B/\[/g" | sed "s/%40/@/g" | sed "s/%5D/\]/g" | sed "s/%25/\%/g" | sed "s/%5E/\^/g" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )



# Insert the record into the Database

COL1="$RECORDID" COL2="$DESCRIPTION" COL3="$LOCATION" COL4="$PARTNO" COL5="$NUMBERTYPE" COL6="$STOCKIST" COL7="$PPQ" COL8="$NEED" COL9="$RATING" COL10="$BIKE"  DBROOT="$DBNAME" TABLE="Catalogue" "$CLAIMS_PROGS"/bin/insert_record_claims.sh -insert > /tmp/STATUS.tmp

STATCODE=$( cat /tmp/STATUS.tmp )

# Generate the HTML header
/bin/cat << EOM
<html><head><title>Claims_DB Sample Database Front End</title></head>
<body bgcolor="white><center>Claims DB Sample Database Front End - Add Record Stage 2</center><br><br>
EOM



# Check whether it worked
if [ "$STATCODE" != "SUCCESS" ]
then
# Something went wrong
/bin/cat << EOM
Your record was <b><font color="red">NOT</font></b> inserted into the Database due to an error. The status code returned by the database was<br>
$STATCODE<br><br>
Please report this error to your Database Administrator.<br><br>
Sorry for the inconvenience.<br><br>
<a href="Index.sh">Return to the Index Page</a>
EOM


else
# It worked!

/bin/cat << EOM 

Your record <b><font color="green">WAS</font></b> inserted into the database.<br><br>
<a href="Index.sh">Return to the Index Page</a>
<br><br>
EOM

fi


# Generate the HTML footer

/bin/cat << EOM
</body></html>
EOM

# Tidy up and exit - make sure no errors are sent to the browser
rm -f  /tmp/STATUS.tmp 2> /dev/null
exit

