#!/bin/bash

# This is a wrapper for blastdbkit.py

# Synopsis: 
#     blastdbkit.sh options

# version April 5, 2020

#---------------------------------------------------------
# Set message strings for confirmation popup box, and then
# get the user to verify that they want to proceed.
MESSAGE='Do you wish to proceed?'
DBLIST=""
args=$* # save argument list to be used later

task='None'
while test $# -gt 0; do
        case "$1" in
                --configure)
                        MESSAGE=''
                        task='configure'
                        shift
                        ;;
                --reportlocal)
                        MESSAGE=''
                        task='reportlocal'
                        shift
                        ;;
                --reportftp)
                        MESSAGE=''
                        task='reportftp'
                        shift
                        ;;
                --add)
                        MESSAGE='Do you wish to add the following files?'
                        task='add'
                        shift
                        ;;
                --delete)
                        MESSAGE='Do you wish to delete the following files?'
                        task='delete'
                        shift
                        ;;
                --update)
                        MESSAGE='Do you wish to update the following files?'
                        task='update'
                        shift
                        ;;
                --dblist)
                        shift
                        rawlist=$1
                        DBLIST=`echo $rawlist | tr -s "," | sed -e "s/^,//" | sed -e "s/,$//"`
                        shift
                        ;;
                --ftpsite)
                        shift
                        shift
                        ;;
                *)
                        shift
                        ;;
        esac
done

echo 'DBLIST: '$DBLIST



#---------------------------------------------------------

case $task in 
    add|update|delete)
       # If the user confirms that it's okay to proceed, launch
       # blastdbkit.py in the background.
        MESSAGE=$MESSAGE$'\n'$DBLIST
        #PROCEED=`java -cp "$BIRCH/script" ConfirmBox "$MESSAGE"`
        PROCEED=`java -jar $BIRCH/script/ConfirmBox2.jar "$MESSAGE"`
        if [ $PROCEED == "Yes" ]
        then
            MESSAGE="blastdbkit: An email notification will be sent when the job is completed."$'\n'"Restart birchadmin after job is complete to see changes appear in menus."
            #java -cp "$BIRCH/script" OkayBox "$MESSAGE"
            java -jar $BIRCH/script/OkayBox2.jar "$MESSAGE"

            # Interestingly, if we don't include an output stream for messages from blastdbkit.py, 
            # running in the background generates errors.
            (nohup $BIRCH/script/blastdbkit.py $args > $BLASTDB/blastdbkit.msg )&

        else
            MESSAGE='blastdbkit: Add/Update/Delete canceled.'
            #java -cp "$BIRCH/script" OkayBox "$MESSAGE"
            java -jar $BIRCH/script/OkayBox2.jar "$MESSAGE"
        fi
        ;;

    *)
        python3 $BIRCH/script/blastdbkit.py $args > $BLASTDB/blastdbkit.msg
        ;;
esac
