#!/bin/bash

# Get BioLegato environment variables.
if [ -f $BIRCH/admin/setblenv.profile.$BIRCH_PLATFORM.source ]
   then
      . $BIRCH/admin/setblenv.profile.$BIRCH_PLATFORM.source
fi   

#             User Preferences that override defaults          #
BIRCHSettingsFile=$HOME/.config/BIRCH/BIRCHsettings.source
if [ -f $BIRCHSettingsFile ]
   then
      . $BIRCHSettingsFile
fi

BLHelperFile=$HOME/.config/BIRCH/BLHelper.source
if [ -f $BLHelperFile ]
   then
      . $BLHelperFile
fi

# NCBI Eutils require user to provide an email address. This should be set in 
# the BL_EMAIL environment variable.
if [ -z "$BL_EMAIL" ]
then
    MESSAGE="NCBI requires email address. In BIRCH, set email using Edit --> Settings."$'\n'"Relaunch blpfetch for changes to take effect"
    echo $MESSAGE
    java -jar $BIRCH/script/ErrorBox2.jar "$MESSAGE"
else
    #------------------------- Run blpfetch ---------------------------------------------

    BIOLEGATO_HOME=$BIRCH/java/bioLegato
    export BIOLEGATO_HOME


    # The location of anciliary data files used by bioLegato.
    BL_DATA_DIR=$BIRCH/dat/blpfetch
    export BL_DATA_DIR


    BL_PROPERTIES=$BL_DATA_DIR/blpfetch.properties
    export BL_PROPERTIES


    # The -dir switch is mainly for launching bioLegato from the birch launcher. By 
    # prompting the user for a working folder, we help enforce good file organization
    # habits.
    if [ $# -gt 0 ]
    then
       if [ ${1} = "-dir" ]
       then

          # Get rid of the -dir switch, and send the remaining command line arguments to bioLegato
          shift

          #Prompt the user to choose a working directory
          #CHANGEDIR=`java -cp "$BIRCH/script" DirChooser "Where do you want to work? Choose a folder."`
          CHANGEDIR=`java -jar $BIRCH/script/DirChooser2.jar "Where do you want to work? Choose a folder."`

          if [ -d "$CHANGEDIR" ]; then
	      cd "$CHANGEDIR"
          fi
       fi
    fi

    ################################################################
    #             User Preferences that override defaults          #
    ################################################################

    BIRCHSettingsFile=$HOME/.config/BIRCH/BIRCHsettings.source
    if [ -f $BIRCHSettingsFile ]
       then
          . $BIRCHSettingsFile
    fi

    BLHelperFile=$HOME/.config/BIRCH/BLHelper.source
    if [ -f $BLHelperFile ]
       then
          . $BLHelperFile
    fi

    # We need to explicitly set the default look and feel. On Mac OSX, the default for the 
    # table canvas has no borders around cells, which detracts from readability.
    java -Xms6M -Xmx384M -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel -jar $BIOLEGATO_HOME/table.jar $*
    #java -jar $BIOLEGATO_HOME/biolegato.jar $*


    # ------------------------ LOGGING ------------------------------
    # For future use when we have a secure logging method.

    #if [ "$BIRCH_LOGGING" -eq "1" ] 
    #    then
    #    echo `whoami` `date` >> $BIRCH/local/admin/biolegato.log
    #fi
fi