#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_paths 29 2005-03-02 17:43:02Z martin $
#
# This script installs product paths that need to be picked up by server
# processes.
#
# Calling format:
#
# install_paths product_install_path path1 path2 ... pathn
#
# paths - a list of paths
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
# 3 = file not found
# 4 = create file/dir failure
# 5 = permission denied
#
ESLDSOCONF="es.ld.so.conf"
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
if [ "$TESTEXISTS" = "" ];then
  $ECHO "error - TESTEXISTS not defined, expect it to be exported"
  exit 2
fi
$ECHO "$0 script running to store link paths"
#
# Check command line
#
cmdline_error=""
if [ $# -lt 1 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error="1"
else
  PRODUCTINSTALLPATH="$1"
  shift
  LDLIBDIRS="$*"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$PRODUCTINSTALLPATH $*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_paths product_install_dir path1 path2 ... pathn"
  exit 1
fi
#
# Check the product path already exists
#
if [ ! -d "$PRODUCTINSTALLPATH" ];then
  $ECHO "$PRODUCTINSTALLPATH not found"
  exit 3
fi
#
# if PRODUCTINSTALLPATH/etc does not already exist create it
#
if [ ! -d "$PRODUCTINSTALLPATH/etc" ];then
  mkdir "$PRODUCTINSTALLPATH/etc"
  if [ $? -ne 0 ];then
    $ECHO "Failed to create $PRODUCTINSTALLPATH/etc"
    exit 4
  fi
fi
#
# If "ESLDSOCONF file does not already exist in $PRODUCTINSTALLPATH/etc
# create it and check it is writeable
#
if [ ! "$TESTEXISTS" "$PRODUCTINSTALLPATH/etc/$ESLDSOCONF" ];then
  > "$PRODUCTINSTALLPATH/etc/$ESLDSOCONF"
  if [ $? -ne 0 ];then
    $ECHO "Failed to create $PRODUCTINSTALLPATH/etc/$ESLDSOCONF"
    exit 4
  fi
fi
if [ ! -w "$PRODUCTINSTALLPATH/etc/$ESLDSOCONF" ];then
  $ECHO "$PRODUCTINSTALLPATH/etc/$ESLDSOCONF not writeable"
  exit 5
fi
#
# Now add the paths to $LDSOCONF ignoring duplicates
#
for ldpath in $LDLIBDIRS
do
  $ECHO ".\c"
  if ( grep "$ldpath" "$PRODUCTINSTALLPATH/etc/$ESLDSOCONF" 1>/dev/null )
  then
      #$ECHO "$ldpath already exists in $PRODUCTINSTALLPATH/etc/$ESLDSOCONF"
      true
  else
    #$ECHO "Adding \"$ldpath\" to $PRODUCTINSTALLPATH/etc/$ESLDSOCONF"
    $ECHO "$ldpath" >> "$PRODUCTINSTALLPATH/etc/$ESLDSOCONF"
  fi
done
$ECHO
exit 0