#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_check_root 50 2005-05-20 15:33:29Z martin $
#
# 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
#
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."
      $ECHO "**WARNING**"
    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