#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_linkpaths 45 2005-05-17 14:35:33Z martin $
#
# 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
#
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
$ECHO "$0 script running to install dynamic linker paths"
$ECHO "Please be patient, this may take a minute."
#
# 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
**WARNING**
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
	$ECHO ".\c"
        if ( grep "$ldpath" "$LDCONFFILE" 1>/dev/null )
        then
	    #$ECHO "$ldpath already exists in $LDCONFFILE"
	    true
        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 2>/dev/null
	  #$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
NOTE:
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
$ECHO
exit 0
