#
# Copyright (c) 1999-2001 Easysoft Ltd. All rights reserved.
#
# This script installs dynamic linker paths
#
# Calling format:
#
# install_link_paths product path1 path2 ... pathn
#
# product - the product name used in output (spaces allowed)
# paths - a list of paths
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
#
# $Id: install_linkpaths,v 1.9 2003/09/22 13:24:56 martin Exp $
#
# $Log: install_linkpaths,v $
# Revision 1.9  2003/09/22 13:24:56  martin
# Delimit the ldconfig output.
#
# Revision 1.8  2002/10/10 14:47:40  martin
# Fixed bug ID 1337 - reinstalling Easysoft product can duplicate entries
# 	added to ldconfig_paths in /etc/defaults/rc.conf in FreeBSD.
#
# Revision 1.7  2002/07/04 15:45:52  martin
# Replace pre_OOB with pre_ES.
#
# Revision 1.6  2001/11/23 11:42:13  martin
# Add DYLD_LIBRARY_PATH for MAC OS X, darwin.
#
# Revision 1.5  2001/03/12 09:18:11  martin
# Remove spurious n# at start.
#
# Revision 1.4  2001/03/08 14:15:11  martin
# Support FreeBSD ldconfig - automatically add new paths
# 	for dynamic linker.
#
# Revision 1.3  2000/10/18 21:42:03  martin
# Always run ldconfig.
#
# Revision 1.2  2000/04/19 13:37:36  jason
# Added LIBPATH for AIX and SHLIB_PATH for HP
#
# Revision 1.1  2000/03/17 16:37:41  martin
# Initial revision
#
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
if [ "$platform" = "" ];then
  echo "error - platform not defined, expect it to be exported"
  exit 2
fi
#
# Check command line
#
cmdline_error=""
if [ $# -lt 2 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error="1"
else
  PRODUCTLONG="$1"
  shift
  LDLIBDIRS="$*"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_linkpaths product path1 path2 ... pathn"
  exit 1
fi
#
# Install ld.so paths
# ===================
#
if [ \( "$platform" = "linux" \) -o \( "$platform" = "freebsd" \) ];then
  $ECHO
  $ECHO "Attempting to update the dynamic linker with $PRODUCTLONG"
  $ECHO "object paths."
  $ECHO
  uid=`id | awk '{print $1}' | awk -F= '{print $2}' | awk -F\( '{print $1}'`
  if [ "$uid" != "0" ];then
    cat <<EOF
You do not appear to be root and so the $PRODUCTLONG install is incomplete.
You may need to add

$LDLIBDIRS

to the dynamic linker configuration file and run ldconfig to pick up
the $PRODUCTLONG shared objects.
EOF
  elif [ "$platform" = "linux" ];then	# Linux and root
    LDCONFFILE="/etc/ld.so.conf"
    #
    # Attempt to add an entries to ld.so.conf for the shared objects
    #
    if [ ! -w "$LDCONFFILE" ];then
      $ECHO "Cannot find/write your dynamic linker configuration file"
      $ECHO "$LDCONFFILE. You must inform the dynamic linker that"
      $ECHO "$LDLIBDIRS"
      $ECHO "contain shared objects manually."
    else
      #
      # We used to only run ldconfig if a path had been added to ld.so.conf
      # but we also need to run it if a new shared object has been added
      # to an existing dir. Hence runld now set to "y".
      #
      runld="y"
      for ldpath in $LDLIBDIRS
      do
        if ( grep "$ldpath" "$LDCONFFILE" 1>/dev/null )
        then
          $ECHO "$ldpath already exists in $LDCONFFILE"
        else
          $ECHO "Adding \"$ldpath\" to $LDCONFFILE."
          runld="y"
          $ECHO "$ldpath\n" >> "$LDCONFFILE"
        fi
      done
      if [ "$runld" = "y" ];then
        if [ ! -x /sbin/ldconfig ];then
          $ECHO "Failed to find ldconfig"
          $ECHO "You must inform the dynamic linker of the change to $LDCONFFILE manually"
        else
          $ECHO "Running ldconfig"
          $ECHO "Any \"cannot open\" warnings are unlikely to be a result of this installation"
	  $ECHO "===================================================================="
          /sbin/ldconfig
	  $ECHO "===================================================================="
          INSTALLED_LDSO_LINKS="y"
        fi
      fi			# run ld
    fi				# ld.so.conf found and writeable
  elif [ "$platform" = "freebsd" ];then		#freebsd and root
    LDCONFFILE="/etc/defaults/rc.conf"
    if [ ! -w "$LDCONFFILE" ];then
      cat <<EOF
Cannot find a writeable $LDCONFFILE.
You must inform the dynamic linker that
$LDLIBDIRS
contain shared objects manually.
EOF
    else
      grep ldconfig_paths "$LDCONFFILE" 1>/dev/null 2>/dev/null
      if [ $? -ne 0 ];then
        cat <<EOF
Cannot locate ldconfig_paths in $LDCONFFILE.
You must inform the dynamic linker that
$LDLIBDIRS
contain shared objects manually.
EOF
      else
	LIBADDED="no"
        flibs=`grep 'ldconfig_paths=' $LDCONFFILE | sed -e 's|ldconfig_paths=\"\([^\"]*\)\"|\1|'`
        NEWLIBPATHS="$flibs"
	for olib in $LDLIBDIRS
        do
	  ADD="yes"
          for ilib in $NEWLIBPATHS
          do
            if [ "$olib" = "$ilib" ];then
	      ADD="no"
            fi
          done
          if [ "$ADD" = "yes" ]; then
            LIBADDED="yes"
            NEWLIBPATHS="$NEWLIBPATHS $olib"
          fi
        done
        sed -e "s|\(ldconfig_paths=\)\"\([^\"]*\)\"|\1\"${NEWLIBPATHS}\"|g" < "$LDCONFFILE" > "${LDCONFFILE}.new"
        xx=`wc -l $LDCONFFILE | awk '{print $1}'`
        yy=`wc -l ${LDCONFFILE}.new | awk '{print $1}'`
        if [ "$xx" != "$yy" ];then
cat <<EOF
Failed to update $LDCONFFILE.
You must inform the dynamic linker that
$LDLIBDIRS
contain shared objects manually.
EOF
        else
          if [ "$LIBADDED" = "yes" ];then
            $ECHO "This script has made the following changes to" 
            $ECHO "$LDCONFFILE"
            $ECHO
            diff "$LDCONFFILE" "${LDCONFFILE}.new"
            mv $LDCONFFILE $LDCONFFILE.pre_ES
            mv "${LDCONFFILE}.new" "$LDCONFFILE" 
            $ECHO 
            $ECHO "The original file is now called ${LDCONFFILE}.pre_ES"
          fi
          if [ ! -x /sbin/ldconfig ];then
            $ECHO "Failed to find ldconfig"
            $ECHO "You must inform the dynamic linker of the change to $LDCONFFILE manually"
          else
            $ECHO "Running ldconfig"
            $ECHO "Any \"cannot open\" warnings are unlikely to be a result of this installation"
            /sbin/ldconfig -m ${LDLIBDIRS} 
            INSTALLED_LDSO_LINKS="y"
          fi
        fi
      fi 
    fi 
  fi				# linux/freebsd
else				# !linux/freebsd
  $ECHO
  cat <<EOF
You may need to add

$LDLIBDIRS

paths to your DYLD_LIBRARY_PATH, LD_RUN_PATH, LD_LIBRARY_PATH, LIBPATH or
SHLIB_PATH (depending on your operating system) environment variable and
export it before you can use the $PRODUCTLONG.
EOF
fi
exit 0
