#
# Copyright (c) 1999-2000 Easysoft Ltd. All rights reserved.
#
# This script checks the installer is root
#
# Calling format:
#
# install_check_root long_product_name what_if_not_textfile flag
#
# long_product_name - name of product being installed (may contain spaces)
# what_if_not_textfile - text file to output if not root
# flag - if 1 installer must be root
#        if 0 installer does not have to be root
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
#
# $Id: install_check_root,v 1.5 2003/12/05 15:23:59 martin Exp $
#
# $Log: install_check_root,v $
# Revision 1.5  2003/12/05 15:23:59  martin
# Make sure Interix msg is seen.
#
# Revision 1.4  2003/12/05 13:21:12  martin
# Add Interix support.
#
# Revision 1.3  2003/09/22 09:52:45  martin
# Text output formatting fixes.
#
# Revision 1.2  2003/09/11 14:22:05  martin
# If you are root - just left with text output saying you have to be
# 	root - added "- OK".
#
# Revision 1.1  2000/03/17 16:36:37  martin
# Initial revision
#
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
#
# Check command line
#
cmdline_error=""
if [ $# -ne 3 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error="1"
else
  PRODUCTLONG="$1"
  TEXTFILE="$2"
  FLAGS="$3"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_check_root product textfile flag"
  exit 1
fi
$ECHO
if [ "$FLAGS" = "1" ];then
  $ECHO "This installation requires the installer to be the root user \c"
fi

PLATFORM=`uname -s`
if [ "$PLATFORM" = "Interix" ];then
  cat <<EOF

This appears to be the Interix platform and this script cannot determine
if you have the required access rights by simply looking at the UID.

Please make sure you are running as Administrator or the installation
of the OOB Server may fail.

EOF
  $ECHO "Press the return key to continue"
  read press
else
  uid=`id | awk '{print $1}' | awk -F= '{print $2}' | awk -F\( '{print $1}'`
  if [ "$uid" != "0" ];then
    if [ "$FLAGS" = "1" ];then
      $ECHO "\nand you do not appear to be root"
    fi
    cat "$TEXTFILE"
    if [ "$FLAGS" = "1" ];then
      $ECHO "Do you want to continue with the installation? (y/n) [n]: \c"
      answer="hoho"
      while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
      do
        read answer
        if [ "$answer" = "" ];then
      	answer="n"
        fi
      done
      if [ "$answer" = "n" ];then
        $ECHO "Installation aborted at user request"
        exit 99
      fi
    fi
  else
    $ECHO " - OK"
  fi
fi
exit 0