#!/bin/bash

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

# Special code for text editors used by bioLegato and scripts called by bioLegato
# Nedit crashes in some Linux systems due to libraries set in BIRCHLIBS.
# nedit_wrapper unsets LD_LIBRARY_PATH before calling nedit.
# gedit opens all files in a single window. gedit_wrapper.sh forces
# gedit to open each file in different window.
# choose_edit_wrapper.sh returns the name of the wrapper to use
# for each editor, or just returns BL_TextEditor if there is no
# wrapper.
#BL_TextEditor=`choose_edit_wrapper.sh`
#export BL_TextEditor   

BIOLEGATO_HOME=$BIRCH/java/bioLegato
export BIOLEGATO_HOME


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


BL_PROPERTIES=$BL_DATA_DIR/blreads.properties
export BL_PROPERTIES

# - - - - - - Set environment variables for CPUs - - - - - - -
#BL_CORES_MAX, BL_CORES_DEFAULT allow BioLegato to tell threaded programs such as BLAST
# or FASTA how many cores are available.
# BL_CORES_DEFAULT is set to 1/4 of BL_CORES_MAX, or 1, whichever is greater.
BL_CORES_MAX=`$BIRCH/script/nproc.py`
BL_CORES_DEFAULT=$((BL_CORES_MAX/4))
if [ "$BL_CORES_DEFAULT" -lt 1 ]; then
    BL_CORES_DEFAULT=1
fi
#echo 'BL_CORES_MAX: '$BL_CORES_MAX
#echo 'BL_CORES_DEFAULT: '$BL_CORES_DEFAULT
export BL_CORES_MAX BL_CORES_DEFAULT


# - - - - - - Set environment variables for RAM - - - - - - -
#BL_RAM_MAX, BL_RAM_DEFAULT allow BioLegato find out how much memory available.
# BL_RAM_DEFAULT is set to 1/4 of BL_RAM_MAX, or 4, whichever is greater.
BL_RAM_MAX=`$BIRCH/script/ramsize.py`
BL_RAM_DEFAULT=$((BL_RAM_MAX/4))
if [ "$BL_RAM_DEFAULT" -lt 4 ]; then
    BL_RAM_DEFAULT=1
fi
#echo 'BL_RAM_MAX: '$BL_RAM_MAX
#echo 'BL_RAM_DEFAULT: '$BL_RAM_DEFAULT
export BL_RAM_MAX BL_RAM_DEFAULT


# BL_EMAIL is the user's default email address for notification of the
# completion of jobs.
# BIRCH doesn't yet have a mechanism for setting this variable, and there
# is no Unix standard. We'll probably change this code as BIRCH evolves.
if [ -z "${BL_EMAIL}" ]; then
   if [ -z "${EMAIL}" ]; then 
      BL_EMAIL=""
   else
      BL_EMAIL=$EMAIL   
   fi
fi
export BL_EMAIL
#echo 'BL_EMAIL: '$BL_EMAIL

NOAUTONAMES="0"
# 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
   elif [ ${1} = "-noautonames" ]; then
      NOAUTONAMES="1"
   fi
fi

# blreads starts with a list of files in the current working directory
if [ "$NOAUTONAMES" -eq "0" ]
then
    DIRFILE=$$.filelist
    #echo '#'$PWD > $DIRFILE
    #ls -1 >> $DIRFILE
    bldirtab.py $DIRFILE
else
    DIRFILE=""
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

#java -Xms6M -Xmx384M -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel -jar $BIOLEGATO_HOME/table.jar $* $DIRFILE
java -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel -jar $BIOLEGATO_HOME/table.jar $* $DIRFILE
if [ ! "$DIRFILE" == "" ]
then
    rm $DIRFILE
fi


# ------------------------ 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