#!/bin/sh


BIRCHADMIN=`grep BirchProps.adminUserid $BIRCH/local/admin/BIRCH.properties |cut -f2 -d"="`

if [ "$BIRCHADMIN" = `whoami` ] 
then
    # It seems stupid, but in Fedora 16, if you don't explicitly
    # make $HOME your current working directory, birch starts in root (/)
    # and for whatever reason, bioLegato jobs launched from birch won't 
    # be able to find important files.
    cd

    # 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/birchadmin
    export BL_DATA_DIR


    BL_PROPERTIES=$BL_DATA_DIR/birchadmin.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."`

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

    #java -Xms6M -Xmx64M -jar $BIOLEGATO_HOME/image.jar $*
    #java -Xms6M -Xmx96M -jar $BIOLEGATO_HOME/biolegato.jar $*
    #java -Xms6M -Xmx128M -jar $BIOLEGATO_HOME/biolegato.jar $*
    #java -Xms6M -Xmx256M -jar $BIOLEGATO_HOME/biolegato.jar $*
    java -Xms6M -Xmx384M -jar $BIOLEGATO_HOME/image.jar $* "$BIRCH/dat/birchadmin/splash.blimg"
    #java -jar $BIOLEGATO_HOME/biolegato.jar $*

    # ------------------------ LOGGING ------------------------------
    # Check to see if we can log when GDE is launched.
    # If BIRCH is mounted read only, we don't crash the script

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

else
    #java -cp "$BIRCH/script" ErrorBox "You must be logged in as $BIRCHADMIN to run birchadmin"
    java -jar $BIRCH/script/ErrorBox2.jar "You must be logged in as $BIRCHADMIN to run birchadmin"	 
    echo '>>> you must be logged in as '$BIRCHADMIN' to run birchadmin'
 
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