#!/bin/sh

top=`pwd`
area="Install"
. ./common.sh
logFile="/tmp/install_proxy.log"
instdir=$entdir/proxy6.0
prodname="Entrust Authority Security Manager Proxy"
tarfile=proxy_tar.z

# temporary hack as no versions yet
pkgbld=$bldversion; # for this script

reinstall=false;
noconfig=false;
owner=""
user=""

assertUser root

instuser() {
  ls -lL $instdir/bin/proc | awk '{print $3}';
}
instowner() {
  if [ $# -eq 0 ]; then
     afile=$instdir/bin/proc
  else
     afile=$1
  fi
  ls -lL $afile | awk '{print $3 ":" $4}';
}


# asks for the target directory and checks it
#
getTarget() {
  if test -z "$instolddir"; then
    getDir "$prodname" "$instdir" 50
  else
    dir="$instolddir"; # this is a minor version upgrade
    pdircreated=true
    test -x `dirname $dir` && pdircreated=false
    mkdir -p $dir || fatal 1 "Could not create $dir."
  fi
  UserDir="$dir"
  if test -d $instdir || test -h $instdir || test -f $instdir; then
     if test `cd "$instdir" && /bin/pwd` != `cd "$UserDir" && /bin/pwd`; then
        fatal 2 "$instdir already exists.\n Although $UserDir will be used as the install dir,\nthe file $instdir will be created as a symbolic link.  Please remove this file before continuing the install."
     fi
  fi

  if $pdircreated; then
    chown $owner `dirname $UserDir`;
  fi
  test -d $entdir || { mkdir $entdir; chown $owner $entdir; }
  test -d $instdir || ln -s $UserDir $instdir || display "Warning: could not create symbolic link $instdir";
}

extractTarFile() {
  cd $UserDir
  cleanMe $UserDir
  display "Now extracting files, please wait...\c"
  uncompress -c < $top/$tarfile | tar xf -
  display "done."
}


getOwner() {
  if test -z "$owner"; then
    while true; do
      display "Please enter the user that will own this installation  : \c"
      read user
      id "$user" > /dev/null 2>&1 && break;
      display "Invalid response: '$user' is not a valid username"
    done

    while true; do
      display "Please enter the group that will own this installation : \c"
      read group
      isValidGroup $user $group && break;
      display "Invalid response: '$user' must be a member of this group"
    done
    case "$os" in
      HP*) test -f /etc/logingroup || if yesno "The /etc/logingroup file is not configured.  This must be configured for the group permissions to take effect.  Would you like the install to do this now"; then ln -s /etc/group /etc/logingroup; fi;;
    esac
    owner=$user:$group
  fi
}

setOwner() {
  chown -R $owner $UserDir
  chmod -R ug+rX $UserDir
  chmod -R o-w $UserDir
  chmod 755 $UserDir/etc/*.sh
  chown root $UserDir/bin/sfbind || display "Warning: could not change the owner for $UserDir/bin/sfbind"
  chmod 4755 $UserDir/bin/sfbind || display "Warning: could not set the right mode for $UserDir/bin/sfbind"

}

more license.txt
YESNO "\nIf you agree with the licensing terms above\nthen type YES and press Enter" || { display "Install will now exit"; exit 1; }

if [ -z "$bldprodversion" ]; then
   bldprodversion=`echo $pkgbld | cut -f 1,2 -d.`
fi
cat <<@E

     $prodname $bldprodversion Installation and Configuration Utility
@E


#check for an existing version...
if test -x $instdir/bin/proc; then
  if test -r $instdir/etc/bldversion.sh; then
    . $instdir/etc/bldversion.sh
  fi
  echo "$pkgbld.$bldversion" |
  awk -F. '{a=sprintf("%04d%04d%04d%04d",$1,$2,$3,$4); b=sprintf("%04d%04d%04d%04d",$5,$6,$7,$8); { if (a == b) exit(0); else if (a < b) exit(1); else exit(2); 
} }'
  case $? in
      1) # the version being installed predates the installed version
         display "A more recent version of $prodname ($bldversion) is already installed."
         display "Install will not replace a newer version with an older version
"
         exit 1;;
  esac;
    
  if test "$pkgbld" != "$bldversion"; then
    display "Version $bldversion of $prodname is currently installed."
    display "This will first be uninstalled in order to install version $pkgbld with the same install settings."
    if yesno "Do you want to uninstall now"; then
      owner=`instowner`;
      export owner;
      # 
      instolddir=`cd $instdir && /bin/pwd`
      export instolddir;
      cd $instolddir/etc; ./uninstall.sh || exit 1;
      reinstall=true; noconfig=true;
    fi
  else
    display "Version $bldversion of $prodname is already installed."
    exit 1
  fi
fi
#elif [ -d $entdir/proxy7 ]; then...(future) upgrade checks

bootconfig=true;
#yesno "Configure system boot to start servers" && bootconfig=true;
# check patch levels of libC etc?

getOwner
getTarget
extractTarFile
(cd $instdir/lib && for f in *.1; do ln -s $f `basename $f .1`; done)
setOwner

if $bootconfig; then
   display "Configuring system boot files...\c"
   sed -e 's@=rootdir@="'$instdir'"@' etc/startstop.sh > /etc/init.d/entrustproxy.sh
   chmod a=rx /etc/init.d/entrustproxy.sh
   (cd /etc/rc2.d && ln -s ../init.d/entrustproxy.sh S89entrustproxy)
   (cd /etc/rc0.d && ln -s ../init.d/entrustproxy.sh K21entrustproxy)
   display "done."
fi



user=`instowner`
if "$noconfig"; then exit 0; fi

if yesno "Would you like to configure an instance of $prodname now"; then
  (display "" && cd $instdir/etc && su  `instuser` -c "./config.sh") ||
  fatal 4 "$prodname configuration failed."
else
  display "\nYou can configure a server later by logging in as $user and running "
  display "$instdir/etc/config.sh"
fi

display Installation complete. 
echo ""

