ifmxsym="/opt/informix"
entdir="/opt/entrust"
authdir="$entdir/authority7"
entdocdir="$entdir/doc7"
authver="7.0"

isIfmxInstalled() {
  test -f $ifmxsym/bin/oninit
}

isOracleInstalled() {
  if [ "`uname`" = "SunOS" ]; then
     test -f /var/opt/oracle/oratab
  else
     test 0 = 1
  fi
}

logFile=""
logData() {
   if [ ! -z "$logFile" -a -w `dirname "$logFile"` ]; then
      if [ ! -f "$logFile" ]; then
         touch $logFile
         chmod 644 $logFile
      fi
      ov=`echo $1`
      echo "`date \"+%Y/%m/%d %H:%M:%S\"`: $ov" >> $logFile
   fi
}

# usage: fatal <code> <message>
# make code typically a two part identifier 1.4, 1.5 with
# the first corresponding to the phase.  This makes it easier
# for you to insert new error codes without re-enumerating
#
fatal() {
   logData "***Error*** : $area:$1 $2"
   echo "***Error*** : $area:$1 $2"
   echo "cleaning up..."
   test -z "$cleanList" || rm $cleanList
   exit 1
}

warning() {
   logData "***Warning*** : $area:$1 $2"
   echo "***Warning*** : $area:$1 $2"
}

yesno() {
   def=${2:-y}
   while true; do
      echo  "$1 (y/n) ? [$def] \c";
      read y;
      y=${y:-$def};
      case "$y" in
      [yY][eE][sS]) y="y"; break;;
      y|n|no|NO|No|nO) break;;
      esac;
      echo 'Please enter "y", "yes", "n", or "no"'
   done
   test "$y" = "y"
}

YESNO() {
   while true; do
      echo "$1 (yes/no) ? \c";
      read y;
      case $y in [yY][eE][sS]) return 0;; [nN][oO]) return 1;; esac
      echo 'Please enter "yes" or "no"'
   done
}


os=`uname -a`

# there should be code here to verify the min config is met
# not just that the O/S is supported
case "$os" in
HP-UX*11*) true;;
SunOS*5.7*sparc*) true;;
SunOS*5.8*sparc*) true;;
AIX*3*4*) true;;
Linux*) true;;
OSF1*alpha) true;;

*) warning "`uname -r -s` is not a supported platform at the time of release.";
   yesno "Continue install anyway" || exit 1
   ;;
esac

# override this with the area: Install-Ifmx, Config-Ifmx etc


isCurrentUserRoot() {
   test "`id root | awk '{print $1}'`" = "`id | awk '{print $1}'`"
}

assertUser() {
   test "`id $1 | awk '{print $1}'`" = "`id | awk '{print $1}'`"  ||  fatal 1 "install must be run by userid $1"
}

isCurrentUser() {
   test "`id $1 | awk '{print $1}'`" = "`id | awk '{print $1}'`" 
}

authuser() {
  ls -lL $authdir/bin/entdvt | awk '{print $3}';
}
authowner() {
  if [ $# -eq 0 ]; then
     afile=$authdir/bin/entdvt
  else
     afile=$1
  fi
  ls -lL $afile | awk '{print $3 ":" $4}';
}

mbToKb() {
  expr $1 \* 1024
}
kbToMb() {
  expr $1 / 1024
}

cleanMe() {
  cleanList="$cleanList $*";
}

interrupt() {
   fatal 1 "interrupted"
}
trap interrupt INT


display() {
   echo "$*"
   logData "$*"
}

isNumber()
{
	rest=`echo $* | tr -d '[0-9]'`
	if test -z "$rest" ; then
		echo "1"
	else
		echo "0"
	fi
	return 0
}



getFree()
{
   # the sed substitution is to ensure that something is in field 1
   # in cases where df will print a long field on its own line
  case `uname` in
    HP-UX) 	val=`/usr/bin/df -kP $* | sed -e 's/^/X/g' | tail -1 | awk ' { print $4 } '`;;
    SunOS|Linux|OSF1)	val=`df -k $* | sed -e 's/^/X/g' | tail -1 | awk ' { print $4 } '`;;
    AIX)	val=`/usr/bin/df -k $* | sed -e 's/^/X/g' | tail -1 | awk ' { print $3 } '`;;
  esac
  kbToMb $val
}


nomkdir=false;
getDir() {
  while true; do
    echo ${4:-"Enter the directory to install $1. [$2]"};
    read dir;
    dir=${dir:-$2}
    testDir "$dir" "$3" && break;
    if [ $? -ne 5 ]; then
       test -d "$dir" && rmdir "$dir"
    fi
  done
  if test -x "$2"; then
    test `ls -id "$dir" | awk '{print $1}'` = `ls -id "$2" | awk '{print $1}'` && dir="$2";
  fi
}

testDir() {
    dir="$1";
    nomkdir=${3:-false}
    quietempty=${4:-false}

    # ensure an absolute pathname (i.e., starts with a /) was entered
    sval=`echo $dir | sed -e "s/^\s*\/.*//"`
    if [ ! -z "$sval" ]; then
       echo "Entered directory must have an absolute pathname."
       return 1;
    fi

    # set status flag if the parent directory is created,
    # sometimes this will be chowned by caller if so
    $nomkdir || {
      pdircreated=true
      test -x `dirname $dir` && pdircreated=false
      mkdir -p $dir > /dev/null 2>&1 || { display "could not create '$dir'"; return 2; }

    }
    test -d "$dir" || { display "Directory '$dir' does not exist"; return 4; }

    # make sure that the directory is empty
    num=`find "$dir" -print | wc | awk '{print $1}'`
    if [ $num -ne 1 ]; then
       $quietempty || display "Directory '$dir' is not empty";
       return 5;
    fi

    mbfree=`getFree "$dir"`
    if test $mbfree -lt $2; then
      display "Insufficient disk space in '$dir' ($mbfree MB); $2 MB required"
      return 3;
    fi
    break;
}

# this is a workaround for a mainwin defect where on hp11 they
# expect to find content in the hp11 subdir but do not configure it
# It is their recommendation to symlink the dirs.
#
makeMwLinks() {
   test -d "$1" || fatal "internal error, parameter missing to makeMwLinks";
   case $os in
   HP*)
      find $1 -type d -name "*ux10*" -print | while read dir; do cd `dirname $dir`; d=`basename $dir`; ln -s $d `echo $d | sed -e s/ux10/ux11/g`; done
      ;;
   esac
}

selectOne() {
  while true; do
    echo "\n$4"
    echo "Select one of the following:" 
    i=1;
    def="";
    for o in $2; do
      if test "$o" = "$3"; then
        def=$i;
      fi
      echo "\t $i. $o";
      i=`expr $i + 1`;
    done
    if test -z "$def"; then echo "> \c"; else echo "[$def] >\c"; fi
    read x
    x=`echo "$x" | sed -e 's/ *//g'`
    i=1;
    for o in $2 ""; do
      test -n "$o" && test "$x" = "$i" && x="$o";
      test "$o" = "$x" && break 2;
      i=`expr $i + 1`
    done
    echo "Invalid response '$x'"
  done
  
  test -z "$x" && x="$3"; 

  eval $1="'$x'";
}

isValidGroup() {
   u=$1
   t=$2
   for g in `groups $u`; do
      if [ "$t" = "$g" ]; then
         true;
         return;
      fi
   done
   false;
}


promptdef() {
  eval $1="'$2'";
  echo "\n$3"
  if test -z "$2"; then echo "> \c"; else echo "[$2] > \c"; fi
  read val
  test -z "$val" || eval $1="'$val'";
}

promptpw() {
  echo "$2 "
  stty -echo
  echo "> \c";
  read val
  stty echo
  eval $1="'$val'";
}

preprompt() {
  echo "> \c";
  read val
  eval $1="'$val'";
}

# true if determine if $1 is a subtree of $2
# for this test, X is considered a subtree of itself
#
isDirSubtree() {
   test -d $2 || return 0;
   cd "$2" 2>/dev/null; set "$1" `/bin/pwd`;
   while true; do
   case "$1" in
   "$2") return 0;;
   /) return 1;;
   *) if /bin/cd `dirname $1` 2>/dev/null; then
         cd `dirname $1`;
         set `/bin/pwd` "$2"
      else
         set `dirname $1` "$2"
      fi;;
   esac
done
}

bldversion=6.0.0.8
bldprodname="Entrust Authority (TM) Security Manager Proxy"
bldprodversion="6.0"
