#!/bin/sh
# Wrapper for GDE 
# Version 2/26/10

# Special code for text editors used by GDE and scripts called by GDE
# 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 GDE_TEXTEDIT if there is no
# wrapper.
GDE_TEXTEDIT=`choose_edit_wrapper.sh`
export GDE_TEXTEDIT   


# ------------------------ RUN THE PROGRAM ------------------------------
# remove the script name from the arguments, and then run gde.bin with the
# remaining arguments


# In the interim, until biolegato can completely replace GDE
# we need a way to get gde menu items, used from biolegato,
# to call biolegato, when
# it would normally be calling GDE.
# The gde script will call biolegato, rather than gde, if
# BIOLEGATO_ON=1

if [ "${BIOLEGATO_ON}" = "1" ]
   then
   biolegato $*
else
   # --------------------- LIBRARIES -----------------------
   # Use this for a more diverse server cluster
   # Platform-specific setup commands
   # GDE requires the obsolete xview libraries, which are probably available by default on
   # Solaris systems in /usr/openwin, but we put this here just to make sure.
   # Linux systems almost never have /usr/openwin, so we need to supply that in lib-linux-intel/openwin/lib.
   # We put /usr/dt/lib in in case someone is using CDE, and the other libraries are there just as additional
   # fallbacks. For example, if /usr/openwin/lib wasn't on your Solaris system, you could install it in 
   # $BIRCH/local/lib-solaris-sparc.

   BIRCHLIBS=""
   case "$BIRCH_PLATFORM" in
     "solaris-sparc")
       BIRCHLIBS=/usr/openwin/lib:/usr/lib:/usr/lib/X11:/usr/dt/lib:$BIRCH/lib-solaris-sparc:$BIRCH/local/lib-solaris-sparc
       ;; 
     "solaris-amd64")
       BIRCHLIBS=/usr/openwin/lib:/usr/lib:/usr/lib/X11:/usr/dt/lib:$BIRCH/lib-solaris-amd64:$BIRCH/local/lib-solaris-amd64
       ;;   
     "linux-intel")
       BIRCHLIBS=$BIRCH/local/lib-linux-intel:$BIRCH/lib-linux-intel:$BIRCH/lib-linux-intel/openwin/lib:$BIRCH/lib-linux-intel/fc4libs
       GDEFONTS="$BIRCH/lib-linux-intel/gde.fonts/100dpi"
       GDEFONTS=$GDEFONTS','$BIRCH/lib-linux-intel/gde.fonts/misc
       GDEFONTS=$GDEFONTS','$BIRCH/lib-linux-intel/gde.fonts/75dpi

       ;;
     "linux-x86_64")
       BIRCHLIBS=$BIRCH/local/lib-linux-x86_64:$BIRCH/lib-linux-x86_64:$BIRCH/lib-linux-x86_64/openwin/lib:$BIRCH/lib-linux-x86_64/fc4libs
	# The LD_LIBRARY_PATH clobbers nedit on Linux systems, so if nedit is being used,
	# we run it through a wrapper that unsets LD_LIBRARY_PATH first.
       if [ "$GDE_TEXTEDIT" = "nedit" ]
	  then
             GDE_TEXTEDIT=$BIRCH/script/nedit_wrapper.sh
	     export GDE_TEXTEDIT
       fi
       GDEFONTS="$BIRCH/lib-linux-x86_64/gde.fonts/100dpi"
       GDEFONTS=$GDEFONTS','$BIRCH/lib-linux-x86_64/gde.fonts/misc
       GDEFONTS=$GDEFONTS','$BIRCH/lib-linux-x86_64/gde.fonts/75dpi
       ;;
     "osx-x86_64")
       BIRCHLIBS=$BIRCH/local/lib-osx-x86_64:$BIRCH/lib-osx-x86_64:$BIRCH/lib-osx-x86_64/openwin/lib:$BIRCH/lib-osx-x86_64/gdelibs
	# The LD_LIBRARY_PATH clobbers nedit on Linux systems, so if nedit is being used,
	# we run it through a wrapper that unsets LD_LIBRARY_PATH first.
       if [ "$GDE_TEXTEDIT" = "nedit" ]
	  then
             GDE_TEXTEDIT=$BIRCH/script/nedit_wrapper.sh
	     export GDE_TEXTEDIT
       fi
       GDEFONTS="$BIRCH/lib-osx-x86_64/gde.fonts/100dpi"
       GDEFONTS=$GDEFONTS','$BIRCH/lib-osx-x86_64/gde.fonts/misc
       GDEFONTS=$GDEFONTS','$BIRCH/lib-osx-x86_64/gde.fonts/75dpi
       ;;    
     *)
       ;;
   esac

   if [ ! -z "$BIRCHLIBS" ] 
      then
      if [ ! -z "$LD_LIBRARY_PATH" ]
   	   then
	 LD_LIBRARY_PATH=$BIRCHLIBS:$LD_LIBRARY_PATH
      else
	 LD_LIBRARY_PATH=$BIRCHLIBS
      fi
      export LD_LIBRARY_PATH      
   fi

   if [ $BIRCH_PLATFORM = "osx-x86_64" ]
      then
      DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH
      export DYLD_LIBRARY_PATH
      echo DYLD_LIBRARY_PATH $DYLD_LIBRARY_PATH
   fi

   # ------------------------ FONTS ------------------------------
   # See $BIRCH/local/lib-linux-intel/README.fonts

   if [ ! -z "$GDEFONTS" ]
	   then
   #    echo 'Original font settings from xset:'
   #    xset -q
   #   echo GDEFONTS "$GDEFONTS"

      # FOR TESTING PURPOSES ONLY:
      # Remove all fonts from fontpath, and set ONLY the fonts we think we need
      #xset fp- /home/psgendb/.gnome2/share/cursor-fonts,tcp/localhost:7100,/home/psgendb/.gnome2/share/fonts
      # Set $GDEFONTS to fontpath
      #xset fp= $GDEFONTS

      # Append $GDEFONTS to fontpath. Probably the safest choice.
      xset +fp $GDEFONTS
      # Prepend $GDEFONTS to fontpath
      #xset fp+ $GDEFONTS

      # Show current X settings, including fontpath
   #   echo 'Revised font settings from xset:'
   #   xset -q   
   fi

   gde.bin $*
fi
      



# ------------------------ 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/gde.log
fi