#! /bin/echo This file must be sourced inside (ba)sh using: .
#
#   GNUstep.sh.in
#
#   Shell initialization for the GNUstep environment.
#
#   Copyright (C) 1997-2002 Free Software Foundation, Inc.
#
#   Author:  Scott Christley <scottc@net-community.com>
#   Author:  Adam Fedor <fedor@gnu.org>
#   Author:  Richard Frith-Macdonald <rfm@gnu.org>
#   Author:  Nicola Pero <n.pero@mi.flashnet.it>
#
#   This file is part of the GNUstep Makefile Package.
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2
#   of the License, or (at your option) any later version.
#   
#   You should have received a copy of the GNU General Public
#   License along with this library; see the file COPYING.LIB.
#   If not, write to the Free Software Foundation,
#   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

#
# Set the GNUstep system root and local root
#
GNUSTEP_ROOT=/usr/local/GNUstep
GNUSTEP_SYSTEM_ROOT=/usr/local/GNUstep/System
GNUSTEP_FLATTENED=
if [ -z "$LIBRARY_COMBO" ]; then
  LIBRARY_COMBO=gnu-gnu-gnu
fi
export GNUSTEP_ROOT GNUSTEP_SYSTEM_ROOT GNUSTEP_FLATTENED LIBRARY_COMBO

GNUSTEP_MAKEFILES=$GNUSTEP_SYSTEM_ROOT/Makefiles
export GNUSTEP_MAKEFILES

GNUSTEP_LOCAL_ROOT=/usr/local/GNUstep/Local
GNUSTEP_NETWORK_ROOT=/usr/local/GNUstep/Network
export GNUSTEP_LOCAL_ROOT GNUSTEP_NETWORK_ROOT

# test to see if GNUSTEP_ROOT is in Win32 standard form and flag it
if [ -z "`echo $GNUSTEP_SYSTEM_ROOT | sed 's|^[a-zA-Z]:/.*$||'`" ]; then
  convert_win_paths=yes
fi

#
# Determine the host information
#
if [ -z "$GNUSTEP_HOST" ]; then
  # Not all shells (e.g. /bin/sh on FreeBSD < 4.0 or ash) have pushd/popd
  tmpdir=`pwd`; cd /tmp
  GNUSTEP_HOST=`$GNUSTEP_MAKEFILES/config.guess`
  GNUSTEP_HOST=`$GNUSTEP_MAKEFILES/config.sub $GNUSTEP_HOST`
  cd $tmpdir
  # Because this file is sourced rather than executed, we need to unset
  # every temporary variable when we are done with it, otherwise they
  # pollute the calling environment!
  unset tmpdir
fi

if [ -z "$GNUSTEP_HOST_CPU" ]; then
  GNUSTEP_HOST_CPU=`$GNUSTEP_MAKEFILES/cpu.sh $GNUSTEP_HOST`
  GNUSTEP_HOST_CPU=`$GNUSTEP_MAKEFILES/clean_cpu.sh $GNUSTEP_HOST_CPU`
fi

if [ -z "$GNUSTEP_HOST_VENDOR" ]; then
  GNUSTEP_HOST_VENDOR=`$GNUSTEP_MAKEFILES/vendor.sh $GNUSTEP_HOST`
  GNUSTEP_HOST_VENDOR=`$GNUSTEP_MAKEFILES/clean_vendor.sh $GNUSTEP_HOST_VENDOR`
fi

if [ -z "$GNUSTEP_HOST_OS" ]; then 
  GNUSTEP_HOST_OS=`$GNUSTEP_MAKEFILES/os.sh $GNUSTEP_HOST`
  GNUSTEP_HOST_OS=`$GNUSTEP_MAKEFILES/clean_os.sh $GNUSTEP_HOST_OS`
fi

export GNUSTEP_HOST GNUSTEP_HOST_CPU GNUSTEP_HOST_VENDOR GNUSTEP_HOST_OS

#
# Ask the user_home tool for the root path.
#
if [ -z "$GNUSTEP_FLATTENED" ]; then
  GNUSTEP_USER_ROOT=`$GNUSTEP_MAKEFILES/$GNUSTEP_HOST_CPU/$GNUSTEP_HOST_OS/user_home user`
else
  GNUSTEP_USER_ROOT=`$GNUSTEP_MAKEFILES/user_home user`
fi

export GNUSTEP_USER_ROOT

#
# Add the GNUstep tools directories to the path
#
if [ -z "$GNUSTEP_PATHPREFIX_LIST" ]; then
  if [ "$convert_win_paths" = "yes" ]; then

    G_U_R=`$GNUSTEP_MAKEFILES/fixpath.sh -u "$GNUSTEP_USER_ROOT"`
    G_L_R=`$GNUSTEP_MAKEFILES/fixpath.sh -u "$GNUSTEP_LOCAL_ROOT"`
    G_N_R=`$GNUSTEP_MAKEFILES/fixpath.sh -u "$GNUSTEP_NETWORK_ROOT"`
    G_S_R=`$GNUSTEP_MAKEFILES/fixpath.sh -u "$GNUSTEP_SYSTEM_ROOT"`
    GNUSTEP_PATHPREFIX_LIST=$G_U_R:$G_L_R:$G_N_R:$G_S_R

  else
    GNUSTEP_PATHPREFIX_LIST=$GNUSTEP_USER_ROOT:$GNUSTEP_LOCAL_ROOT:$GNUSTEP_NETWORK_ROOT:$GNUSTEP_SYSTEM_ROOT
  fi
  export GNUSTEP_PATHPREFIX_LIST
fi

tmp_IFS="$IFS"
IFS=:
temp_path=
for dir in $GNUSTEP_PATHPREFIX_LIST; do
  temp_path=$temp_path$dir/Tools:
  if [ -z "$GNUSTEP_FLATTENED" ]; then
    temp_path=$temp_path$dir/Tools/${GNUSTEP_HOST_CPU}/${GNUSTEP_HOST_OS}/${LIBRARY_COMBO}:
    temp_path=$temp_path$dir/Tools/${GNUSTEP_HOST_CPU}/${GNUSTEP_HOST_OS}:
  fi
done
IFS="$tmp_IFS"
unset tmp_IFS
unset dir
if [ -z "$PATH" ]; then
  PATH="$temp_path"
else
  if ( echo ${PATH}| grep -v "${temp_path}" >/dev/null ); then
    PATH="${temp_path}${PATH}"
  fi
fi
unset temp_path
export PATH

. $GNUSTEP_MAKEFILES/ld_lib_path.sh

if [ "$convert_win_paths" = "yes" ]; then
  # FIXME - G_U_R might not have been defined
  gnustep_class_path="$G_U_R/Libraries/Java:$G_L_R/Libraries/Java:$G_N_R/Libraries/Java:$G_S_R/Libraries/Java"
  unset convert_win_paths
  unset G_U_R
  unset G_L_R
  unset G_N_R
  unset G_S_R
else
  gnustep_class_path="$GNUSTEP_USER_ROOT/Libraries/Java:$GNUSTEP_LOCAL_ROOT/Libraries/Java:$GNUSTEP_NETWORK_ROOT/Libraries/Java:$GNUSTEP_SYSTEM_ROOT/Libraries/Java"
fi

if [ -z "$CLASSPATH" ]; then
  CLASSPATH="$gnustep_class_path"
else
  if ( echo ${CLASSPATH}| grep -v "${gnustep_class_path}" >/dev/null ); then
    CLASSPATH="$CLASSPATH:$gnustep_class_path"
  fi
fi

unset gnustep_class_path
export CLASSPATH

#
# Perform any user initialization
#
if [ -f "$GNUSTEP_USER_ROOT/GNUstep.sh" ]
then
  . "$GNUSTEP_USER_ROOT/GNUstep.sh"
fi

#
# Run 'make_services' in background if possible - updates cache of services
# and applications/file-extensions known to the applications.
#
# We run it in a subshell - using ("$TDIR/make_services" &) -
# otherwise shell with job control (like bash) output an annoying
# message when make_services is done, while we want it to happen
# silently.
#
if [ -z "$GNUSTEP_FLATTENED" ]; then
  TDIR=$GNUSTEP_SYSTEM_ROOT/Tools/$GNUSTEP_HOST_CPU/$GNUSTEP_HOST_OS
  if [ -x "$TDIR/$LIBRARY_COMBO/make_services" ]
  then
    ("$TDIR/$LIBRARY_COMBO/make_services" &)
  else
    if [ -x "$TDIR/make_services" ]
    then
      ("$TDIR/make_services" &)
    fi
  fi
else
  TDIR=$GNUSTEP_SYSTEM_ROOT/Tools
  if [ -x "$TDIR/make_services" ]
  then
    ("$TDIR/make_services" &)
  fi
fi
unset TDIR

