# Obtains CPU specific file type info for FirstClass app whose path is passed as an argument
# Stores resultant file type in $excutable_type
get_my_executable_info()
{
	fileType=`file "$*"`
	if [ "$_FC_PLATFORM" = "Mac OS X" ]; then
	   type=`echo "$fileType" | grep 'x86_64'`
	   if [ "$type" = "" ]; then
		 type=`echo "$fileType" | grep '2 architectures'`
		 if [ "$type" = "" ]; then
		   type=`echo "$fileType" | grep 'ppc'`
		   if [ "$type" = "" ]; then
			 type=`echo "$fileType" | grep 'i386'`
			 if [ "$type" = "" ]; then
			   excutable_type="unknown"
			 else
			   excutable_type="$OSXTYPE32INTEL"
			 fi
		   else
			 excutable_type="$OSXTYPE32PPC"
		   fi
		 else
		   excutable_type="$OSXTYPE32UNI"
		 fi
	   else
		 excutable_type="$OSXTYPE64INTEL"
	   fi
	else
		type=`echo "$fileType" | grep '32-bit'`
		if [ "$type" = "" ]; then
			excutable_type="$APPTYPE64INTEL"
		else
			excutable_type="$APPTYPE32INTEL"
		fi	
	fi
}

# Obtains CPU type info for hardware
# Stores resultant file type in $cpu_type
get_cpu_type()
{
   if [ "$_FC_PLATFORM" = "Mac OS X" ]; then
	   type=`sysctl -a hw | grep 'hw.cpu64bit_capable: 1'`
	   if [ "$type" = "" ]; then
		 type=`sysctl -a hw | grep 'hw.machine = i386'`
		 if [ "$type" = "" ]; then
		   cpu_type="$CPUTYPE32PPC"
		 else
		   cpu_type="$CPUTYPE32INTEL"
		 fi
	   else
		 cpu_type="$CPUTYPE64INTEL"
	   fi
	else
		type=`cat /proc/cpuinfo | grep " lm "`
		if [ "$type" = "" ]; then
			cpu_type="$CPUTYPE32INTEL"
		else
			cpu_type="$CPUTYPE64INTEL"
		fi
	fi
}


# Checks if CPU type supported for the excutable_type type passed in as $1
# If supported "1" sent to StdOut, or "0" if not supported.
cpu_compatibilty_check()
{
compatibility="0"
case "$*" in
"$APPTYPE64GENERAL")
  if [ "$cpu_type" = "$CPUTYPE64INTEL" ]; then
    compatibility="1"
  fi
  ;;
"$OSXTYPE64INTEL")
  if [ "$cpu_type" = "$CPUTYPE64INTEL" ]; then
    compatibility="1"
  fi
  ;;
"$OSXTYPE32PPC")
  if [ "$cpu_type" = "$CPUTYPE32PPC" ]; then
    compatibility="1"
  fi
  ;;
"$OSXTYPE32INTEL")
  if [ ! "$cpu_type" = "$CPUTYPE32PPC" ]; then
    compatibility="1"
  fi
  ;;
"$OSXTYPE32UNI")
    compatibility="1"
  ;;
"$APPTYPE64INTEL")
  if [ "$cpu_type" = "$CPUTYPE64INTEL" ]; then
    compatibility="1"
  fi
  ;;
"$APPTYPE32INTEL")
    compatibility="1"
  ;;
*)
    compatibility="0"
  ;;
esac

echo "$compatibility"
}


find_pid()
{
	ps axc | grep -w $1 | awk '{print $1}'
}


check_sudoerStatus()
{
	sudo -l >& /dev/null
    if [ "$?" -eq "1" ]; then
    	myUserIsSudoer="0"
    else
    	myUserIsSudoer="1"
    fi
}

# Checks if fcadmin user has sudoer privledges in the /etc/sudoers file
# If not, fcadmin is given the sudoers priviledge.
# Needed at service startup to switch to root context in order to open ports.
update_sudoersFile()
{
    if [ "$_FC_PLATFORM" != "Mac OS X" ]; then
        
        if [ "`id -un`" != "root" ]; then
    		check_sudoerStatus
    	else
			# Now update the sudoers file if necessary.
			if [ "`$MY_FILES/issudo`" -eq "0" ] ; then
				if [ "`id -un`" = "root" ]; then
					if [ "`ls -l $SUDOERS | cut -c 3`" = "-" ]; then
						echo -n "[`$NOW`] Adding '$MY_USER' to the /etc/sudoers file ... "
						sudoers_needed_w="1";
						#echo /etc/sudoers needs write access
						chmod u+w $SUDOERS
					else
						#echo /etc/sudoers already writable
						sudoers_needed_w="0";
					fi
					echo " " >>$SUDOERS
					echo "# User '$MY_USER' added by $PRODUCT_BRAND Services installer." >>$SUDOERS
					echo "$MY_USER        ALL=(ALL)       ALL" >>$SUDOERS
					echo "" >>$SUDOERS
					if [ "$sudoers_needed_w" -eq "1" ]; then
						#echo /etc/sudoers restoring read-only
						chmod u-w $SUDOERS
					fi
					if [ "`$MY_FILES/issudo`" -eq "0" ] ; then
						echo "update failed."
					else
						echo "done."
						myUserIsSudoer="1"
					fi
				fi	
			fi
    	fi
    	
		
	fi
}



######## AUTHENTICATION ############
#Authenicate sudo account
requestAuthentication()
{
 	if [ "`id -un`" != "root" ]; then
 	    sudo -k >& /dev/null
		echo ""
		echo "When prompted for a password, enter your system password for the '$MY_USER' user. "
		echo "(This may occur more than once if the installation takes longer than 5 minutes.)"
	fi
	
	if [ "$_FC_PLATFORM" != "Mac OS X" ]; then
    	update_sudoersFile
	fi

	if [ "$myUserIsSudoer" = "1" ]; then
		#sudo -k >& /dev/null
		sudo -v >& /dev/null
		if [ "$?" -eq "0" ] ; then
			if [ "`id -un`" != "root" ]; then
				echo "Authentication accepted."
			fi
		else
			echo "Authentication failed."
			echo "Installation aborted."
			cleanup
			exit 1
		fi
		echo ""
	else
	    echo ""
		echo "[`$NOW`] ERROR: User '$MY_USER' does not appear to have sudoer privileges ... "
		echo "[`$NOW`] Please run the 'su' command to switch to the root user account and rerun this installer."
		echo "[`$NOW`] Running as the root user will allow this installer to add the '$MY_USER' user account to the /etc/sudoers file."
		echo "[`$NOW`] Subsequent installer runs on this machine may then be run as the '$MY_USER' user"
		echo "[`$NOW`] Installation aborted."
		echo ""
		cleanup
		exit 1
	fi
}

doPrerequisiteChecks()
{
echo "[`$NOW`] Performing installer prerequisite checks... "
if [ "$_FC_PLATFORM" != "Mac OS X" ]; then
	if [ "`id -un`" != "root" ] && [ "`id -un`" != "$MY_USER" ] ; then
		echo "Installer running as root or $MY_USER user: NO"
		echo "Installation aborted - Must be run as $MY_USER or root user."
		echo "_____"
		echo
		cleanup
		exit 1
	fi
	echo "Installer running as 'root' or '$MY_USER' user:         OK"
else
	if [ "`id -un`" != $MY_USER ]; then
		echo "Installer running as '$MY_USER' user: NO"
		echo "Installation aborted - not running as '$MY_USER' user."
		echo "_____"
		echo
		cleanup
		exit 1
	fi
	echo "Installer running as '$MY_USER' user:         OK"
fi

if [ "$_FC_PLATFORM" != "Mac OS X" ]; then
	if [ id -u $MY_USER >& /dev/null ]; then
		echo "The '$MY_USER' user exists:                NO"
		echo "Installation aborted - '$MY_USER' user not defined."
		echo "(Create an $MY_USER user before running this installer. See installation instructions.)"
		echo "_____"
		echo
		cleanup
		exit 1
	fi
	echo "The '$MY_USER' user exists:                OK"
fi

if [ "$_FC_PLATFORM" != "Mac OS X" ]; then
    grpExists=`grep -i "^$MY_GROUP:" /etc/group`
	if [ "$grpExists" = "" ]; then
		echo "The '$MY_GROUP' group exists:                   NO"
		echo "Installation aborted - $MY_GROUP group not defined."
		echo "(Create an '$MY_GROUP' group before running this installer. See installation instructions.)"
		echo "_____"
		echo
		cleanup
		exit 1
	fi
	echo "The '$MY_GROUP' group exists:                   OK"
fi


grpMembership=`id -Gn $MY_USER | grep -iw $MY_GROUP`
if [ "$grpMembership" = "" ]; then
	echo "The '$MY_USER' user is in the '$MY_GROUP' group:  NO"
	echo "Installation aborted - '$MY_USER' not in '$MY_GROUP' group."
	echo "(Add the '$MY_GROUP' group to the '$MY_USER' account before running this installer. See installation instructions.)"
	echo "_____"
	echo
	cleanup
	exit 1	
fi
echo "The 'fcadmin user is in the '$MY_GROUP' group:  OK"


if [ -e "$FCSD_TAR" ]; then
	tar xz -C "$MY_TEMP_FILES" -f "$FCSD_TAR" $FCSD_NAME
	chmod +x "$MY_TEMP_FILES/$FCSD_NAME"
	
	get_my_executable_info "$MY_TEMP_FILES/$FCSD_NAME"
	FCSD_TYPE="$excutable_type"
	
	if [ `cpu_compatibilty_check "$FCSD_TYPE"` -eq 1 ]; then	
		FCSD_VER=`get_version "$MY_TEMP_FILES/$FCSD_NAME"`
		FCSD_BUILD=`get_build "$MY_TEMP_FILES/$FCSD_NAME"`
		if [ "$FCSD_BUILD" = "" ]; then 
			FCSD_BUILD=$UNKNOWN_VER;
		fi
		rm "$MY_TEMP_FILES/$FCSD_NAME"
		if [ -n "$FCSD_PID" ]; then
			echo "Error: The $PRODUCT_BRAND Core Server ($FCSD_NAME) is running (pid=$FCSD_PID)."
			echo "_____"
			echo
			cleanup
			exit 2
		fi
		echo "Core server not currently running:        OK"
	else
		echo
		echo "Error: The CPU type ($cpu_type) is not supported by the ($FCSD_NAME) executable included in this installer. You will not be given the option to upgrade the Core Server."
		echo "_____"
		echo
	fi
fi

if [ -e "$FCISD_TAR" ]; then
	tar xz -C "$MY_TEMP_FILES" -f "$FCISD_TAR" $FCISD_NAME
	chmod +x "$MY_TEMP_FILES/$FCISD_NAME"
	
	get_my_executable_info "$MY_TEMP_FILES/$FCISD_NAME"
	FCISD_TYPE="$excutable_type"
	
	if [ `cpu_compatibilty_check "$FCISD_TYPE"` -eq 1 ]; then	
		FCISD_VER=`get_version "$MY_TEMP_FILES/$FCISD_NAME"`
		FCISD_BUILD=`get_build "$MY_TEMP_FILES/$FCISD_NAME"`
		
		if [ "$FCISD_BUILD" = "" ]; then 
			FCISD_BUILD=$UNKNOWN_VER;
		fi
		rm "$MY_TEMP_FILES/$FCISD_NAME"
		if [ -n "$FCISD_PID" ]; then
			echo "Error: $PRODUCT_BRAND Internet Services ($FCISD_NAME) is running (pid=$FCISD_PID)."
			echo "_____"
			echo
			cleanup
			exit 3
		fi
		echo "Internet Services not currently running:  OK"
		
	else
		echo
		echo "Error: The CPU type ($cpu_type) is not supported by the ($FCISD_NAME) executable included in this installer. You will not be given the option to upgrade the Internet Services."
		echo "_____"
		echo
	fi
fi
}

