#!/bin/bash
#
# Protocol driver for reading data from the Claims Database
#
# Filename: read_records_claims.sh

# To see how to use this script, read the Protocol Documentation
# Available from http://benscomputer.no-ip.org/Claims_DB/

MYPID="$$"

source /etc/claims_db.conf
DBFILE="/$DBTEMPDIR/DBCOPY.$MYPID"

if [ "$DBROOT" == "" ]
then
DBROOT="$DBNAME"
fi

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

fi

cp "$PROGROOT"/"$DBROOT"/"$TABLE".csv "$DBFILE"
chmod 777 "$DBFILE"
DBTEMPFILE="/$DBTEMPDIR/DBFILE.csv.$MYPID"

# 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"
OTHERCOLREF="$2"

select_distinct_function ()
{
COLREF="$OTHERCOLREF"
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


# Now seperate out the column, and remove duplicates
/bin/cat << EOM > /$DBTEMPDIR/LOCATS2.tmp.$MYPID
$AWKCMD 
EOM
SKIPINFO=$( head -n 1 /$DBTEMPDIR/LOCATS2.tmp.$MYPID )
cat /$DBTEMPDIR/LOCATS2.tmp.$MYPID | grep -v "$SKIPINFO" > /$DBTEMPDIR/LOCATS2.tmp2.$MYPID
mv /$DBTEMPDIR/LOCATS2.tmp2.$MYPID /$DBTEMPDIR/LOCATS2.tmp.$MYPID

awk '{
if ($0 in stored_lines)
x=1
else
print
stored_lines[$0]=1
}' /$DBTEMPDIR/LOCATS2.tmp.$MYPID | sed s/\"//g
exit
}

select_next_pk ()
{


COLREF="$OTHERCOLREF"

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

/bin/cat << EOM > /$DBTEMPDIR/LOCATS2.tmp.$MYPID
$AWKCMD
EOM

# Strip out the text Delimiter - "
sed s/\"//g /$DBTEMPDIR/LOCATS2.tmp.$MYPID > /$DBTEMPDIR/LOCATS.tmp.$MYPID

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

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

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

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

NEWKEY=$(( $LASTKEY + 1 ))
rm -f /$DBTEMPDIR/LOCATS.tmp.$MYPID
rm -f /$DBTEMPDIR/LOCATS2.tmp.$MYPID
echo $NEWKEY
exit

}


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"

rm -f $DBTEMPFILE 2> /dev/null
rm -f $DBFILE 2> /dev/null
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\"" > /$DBTEMPDIR/DBRECS.$MYPID
$AWKCMD
EOM

chmod 777 /$DBTEMPDIR/DBRECS.$MYPID

rm -f /$DBTEMPDIR/DBRECS2.$MYPID 2> /dev/null
for a in $( cat /$DBTEMPDIR/DBRECS.$MYPID );
do
echo -n "=" >> /$DBTEMPDIR/DBRECS2.$MYPID
echo "$a" >> /$DBTEMPDIR/DBRECS2.$MYPID
done

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

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

# There used to be a good reason for this line
#LINE_NUMBER2=$(( $LINE_NUMBER - 1 ))

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" > /$DBTEMPDIR/DBRECS.$MYPID
chmod 777 /$DBTEMPDIR/DBRECS.$MYPID
$AWKCMD
EOM



rm -f /$DBTEMPDIR/DBRECS2.$MYPID 2> /dev/null


for a in $( cat /$DBTEMPDIR/DBRECS.$MYPID );
do
echo -n "=" >> /$DBTEMPDIR/DBRECS2.$MYPID
echo "$a" >> /$DBTEMPDIR/DBRECS2.$MYPID
done

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

for a in $( cat /$DBTEMPDIR/DBRECS2.$MYPID );
do
LINE_NUMBER=$( echo "$a" | sed -n 's/^.*=\([^:]*\).*$/\1/p' )
# There used to be a good reason for this line
# LINE_NUMBER=$(( $LINE_NUMBER - 1 ))
echo "$LINE_NUMBER"

done
# Output the retrieved line for processing by external scripts
rm -f $DBTEMPFILE 2> /dev/null
rm -f $DBFILE 2> /dev/null
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



# Added in V0.2

if [ "$1" == "-gen-PK" ]
then

# user wants to know what the next Primary key is
select_next_pk


fi


if [ "$1" == "-select-distinct" ]
then
select_distinct_function
fi

# Uh-oh
rm $DBFILE
echo "BADREQUEST"




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


