#!/bin/bash
#
# Insert Record into the Claims Recordset
#
# Filename: insert_record_claims.sh




# USAGE:
#
# This driver should be called with the first argument as either -insert or -delete
# Most information must be defined as a Variable from the command line
# Currently, Record insertion supports up to 12 Columns, this will be expanded at a later date

# Deletion of a record is implemented by calling the driver with -delete LINENUMBER, where LINENUMBER is 
# the line number that Grep returns

# DBROOT and TABLE must also be defined as command line variables, so an example call would look like this
#
# DBROOT="./" TABLE="Claims" COL1="01" COL2="Example Data" COL3="1" insert_record_claims.sh -insert
#
# or
#
# DBROOT="./" TABLE="Claims" insert_record_claims.sh -delete 2
source /etc/claims_db.conf

MYPID="$$"

LINE_NUMBER="$2"
DBFILE="$PROGROOT"/"$DBROOT"/"$TABLE".csv



insert_record ()
{

# Add a record to the table

/bin/cat << EOM >> $DBFILE
"$COL1","$COL2","$COL3","$COL4","$COL5","$COL6","$COL7","$COL8","$COL9","$COL10","$COL11","$COL12","$COL13","$COL14"
EOM
if [ "$?" == "0" ]
then
echo "SUCCESS"
exit
fi

echo "FAILED"

exit
}


Delete_Record ()
{

EXCLUDE_RECORD=$( sed -n "$LINE_NUMBER"p $DBFILE )
cat "$DBFILE" | grep -v "$EXCLUDE_RECORD" > /$DBTEMPDIR/DBTEMP.$MYPID
if [ "$?" == "1" ]
then
echo "FAILED"
exit
fi

mv /$DBTEMPDIR/DBTEMP.$MYPID $DBFILE
if [ "$?" == "1" ]
then
echo "FAILED"
rm -f /$DBTEMPDIR/DBTEMP.$MYPID
exit
fi
echo "SUCCESS"

exit

}

if [ "$PROGROOT" == "" ]
then
# Essential Config Variable not set
echo "BADREQUEST"
exit

fi
if [ "$1" == "-insert" ]
then
insert_record
fi

if [ "$1" == "-delete" ]
then

Delete_Record
fi

echo "BADREQUEST"
