#!/bin/bash
#
# Protocol driver for reading data from the Claims Database
#
# Filename: read_records_claims.sh
#
# Copyright Ben Tasker 2009
# Released under the GNU GPL.
# See http://benscomputer.no-ip.org/LICENSE for a copy of the license



source /etc/claims_db.conf
DBFILE="/tmp/DBCOPY"
if [ "$PROGROOT" == "" ]
then
# Essential Config Variable not set
echo "BADREQUEST"
exit

fi

cp "$PROGROOT"/"$DBROOT"/""/"$TABLE".csv "$DBFILE"
DBTEMPFILE="/tmp/DBFILE.csv"

# Find out what the headers of the file are, then we can parse it out
HEADERS=$( head -1 $DBFILE )

# If a query has been submitted, check it
QUERYSTRING="$2"
COLREF="$3"

read_data ()
{
# User wants Data from the file


# Create the temporary file, without headers
cat "$DBFILE" | grep -v "$HEADERS" > "$DBTEMPFILE"



# Read line by line
while read -r a                    
do   
echo $a
done < "$DBTEMPFILE"
exit
}





query_database ()
{


# Queries the selected Dataset

if [ "$COLREF" == "1" ]
then
AWKCMD=$( awk -F\, '{print $1}' $DBFILE )
fi
if [ "$COLREF" == "2" ]
then
AWKCMD=$( awk -F\, '{print $2}' $DBFILE )
fi

if [ "$COLREF" == "3" ]
then
AWKCMD=$( awk -F\, '{print $3}' $DBFILE )
fi
if [ "$COLREF" == "4" ]
then
AWKCMD=$( awk -F\, '{print $4}' $DBFILE )
fi
if [ "$COLREF" == "5" ]
then
AWKCMD=$( awk -F\, '{print $5}' $DBFILE )
fi
if [ "$COLREF" == "6" ]
then
AWKCMD=$( awk -F\, '{print $6}' $DBFILE )
fi
if [ "$COLREF" == "7" ]
then
AWKCMD=$( awk -F\, '{print $7}' $DBFILE )
fi
if [ "$COLREF" == "8" ]
then
AWKCMD=$( awk -F\, '{print $8}' $DBFILE )
fi
if [ "$COLREF" == "9" ]
then
AWKCMD=$( awk -F\, '{print $9}' $DBFILE )
fi
if [ "$COLREF" == "10" ]
then
AWKCMD=$( awk -F\, '{print $10}' $DBFILE )
fi
if [ "$COLREF" == "11" ]
then
AWKCMD=$( awk -F\, '{print $11}' $DBFILE )
fi
if [ "$COLREF" == "12" ]
then
AWKCMD=$( awk -F\, '{print $12}' $DBFILE )
fi
if [ "$COLREF" == "13" ]
then
AWKCMD=$( awk -F\, '{print $13}' $DBFILE )
fi
if [ "$COLREF" == "14" ]
then
AWKCMD=$( awk -F\, '{print $14}' $DBFILE )
fi
if [ "$COLREF" == "15" ]
then
AWKCMD=$( awk -F\, '{print $15}' $DBFILE )
fi
if [ "$COLREF" == "16" ]
then
AWKCMD=$( awk -F\, '{print $16}' $DBFILE )
fi
if [ "$COLREF" == "17" ]
then
AWKCMD=$( awk -F\, '{print $17}' $DBFILE )
fi
if [ "$COLREF" == "18" ]
then
AWKCMD=$( awk -F\, '{print $18}' $DBFILE )
fi
if [ "$COLREF" == "19" ]
then
AWKCMD=$( awk -F\, '{print $19}' $DBFILE )
fi
if [ "$COLREF" == "20" ]
then
AWKCMD=$( awk -F\, '{print $20}' $DBFILE )
fi
# Select Columns
/bin/cat << EOM | grep -nw "$QUERYSTRING" > /tmp/DBRECS
$AWKCMD
EOM



rm -f /tmp/DBRECS2
for a in $( cat /tmp/DBRECS );
do
echo -n "=" >> /tmp/DBRECS2
echo "$a" >> /tmp/DBRECS2
done

RESULTS_ENUMERATE=$( cat /tmp/DBRECS2 2> /dev/null )
if [ "$RESULTS_ENUMERATE" == "" ]
then
no_results
fi 

for a in $( cat /tmp/DBRECS2 );
do
LINE_NUMBER=$( echo "$a" | sed -n 's/^.*=\([^:]*\).*$/\1/p' )
sed -n $LINE_NUMBER'p' $DBFILE

done
# Output the retrieved line for processing by external scripts

exit




}

query_database_line ()
{


# Queries the selected Dataset

if [ "$COLREF" == "1" ]
then
AWKCMD=$( awk -F\, '{print $1}' $DBFILE )
fi
if [ "$COLREF" == "2" ]
then
AWKCMD=$( awk -F\, '{print $2}' $DBFILE )
fi

if [ "$COLREF" == "3" ]
then
AWKCMD=$( awk -F\, '{print $3}' $DBFILE )
fi
if [ "$COLREF" == "4" ]
then
AWKCMD=$( awk -F\, '{print $4}' $DBFILE )
fi
if [ "$COLREF" == "5" ]
then
AWKCMD=$( awk -F\, '{print $5}' $DBFILE )
fi
if [ "$COLREF" == "6" ]
then
AWKCMD=$( awk -F\, '{print $6}' $DBFILE )
fi
if [ "$COLREF" == "7" ]
then
AWKCMD=$( awk -F\, '{print $7}' $DBFILE )
fi
if [ "$COLREF" == "8" ]
then
AWKCMD=$( awk -F\, '{print $8}' $DBFILE )
fi
if [ "$COLREF" == "9" ]
then
AWKCMD=$( awk -F\, '{print $9}' $DBFILE )
fi
if [ "$COLREF" == "10" ]
then
AWKCMD=$( awk -F\, '{print $10}' $DBFILE )
fi
if [ "$COLREF" == "11" ]
then
AWKCMD=$( awk -F\, '{print $11}' $DBFILE )
fi
if [ "$COLREF" == "12" ]
then
AWKCMD=$( awk -F\, '{print $12}' $DBFILE )
fi
if [ "$COLREF" == "13" ]
then
AWKCMD=$( awk -F\, '{print $13}' $DBFILE )
fi
if [ "$COLREF" == "14" ]
then
AWKCMD=$( awk -F\, '{print $14}' $DBFILE )
fi
if [ "$COLREF" == "15" ]
then
AWKCMD=$( awk -F\, '{print $15}' $DBFILE )
fi
if [ "$COLREF" == "16" ]
then
AWKCMD=$( awk -F\, '{print $16}' $DBFILE )
fi
if [ "$COLREF" == "17" ]
then
AWKCMD=$( awk -F\, '{print $17}' $DBFILE )
fi
if [ "$COLREF" == "18" ]
then
AWKCMD=$( awk -F\, '{print $18}' $DBFILE )
fi
if [ "$COLREF" == "19" ]
then
AWKCMD=$( awk -F\, '{print $19}' $DBFILE )
fi
if [ "$COLREF" == "20" ]
then
AWKCMD=$( awk -F\, '{print $20}' $DBFILE )
fi
# Select Columns
/bin/cat << EOM | grep -nw "$QUERYSTRING" > /tmp/DBRECS
$AWKCMD
EOM



rm -f /tmp/DBRECS2
for a in $( cat /tmp/DBRECS );
do
echo -n "=" >> /tmp/DBRECS2
echo "$a" >> /tmp/DBRECS2
done

RESULTS_ENUMERATE=$( cat /tmp/DBRECS2 2> /dev/null )
if [ "$RESULTS_ENUMERATE" == "" ]
then
no_results
fi 

for a in $( cat /tmp/DBRECS2 );
do
LINE_NUMBER=$( echo "$a" | sed -n 's/^.*=\([^:]*\).*$/\1/p' )
echo "$LINE_NUMBER"

done
# Output the retrieved line for processing by external scripts

exit




}
no_results ()
{
echo "NORESULTS"

exit
}




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

# User wants Column Headers from the Table
echo "$HEADERS"
exit
fi

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

# User wants to Read Data from the table
read_data
fi

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

# user wants to query the database
query_database


fi

if [ "$1" == "-query-line" ]
then

# user wants to query the database
query_database_line


fi


# Uh-oh
rm $DBFILE
echo "BADREQUEST"




# Select Columns
#awk -F\, '{print $1":"$3}' DBFILE


