#!/bin/sh

#
# @(#)setup wrapper
#
# Copyright 2003 Sun Microsystems, Inc. All rights reserved.
# 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
#
# This software is the confidential and proprietary information
# of Sun Microsystems, Inc. ("Confidential Information").  You
# shall not disclose such Confidential Information and shall use
# it only in accordance with the terms of the license agreement
# you entered into with Sun.
#


# I use the following construct because some shell interpreters have
# problems with if ! <command>; then ...
#   if command; then
#     :
#   else
#     ... ! command ...
#   fi

if [ -x /usr/bin/uname -o -x /bin/uname -o -x /usr/sbin/uname -o -x /sbin/uname ]; then
	PATH="$PATH:/usr/bin:/bin:/usr/sbin:/sbin"
	export PATH
	s1os=`uname`
else
	echo "Error: Unable to locate binary 'uname'."
	echo "       Please make certain this binary is on your path and try again."
	exit 1
fi


# The following binaries will be searched for in the following search
# paths.  Once located, their full paths will be put in variables named 
# s1<binary>.  If any binaries are missing, report the missing
# binary and exit.
binaries='ls:basename:dirname:expr:cut:whoami:cp'
search_dirs="/sbin:/usr/sbin:/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH"

if [ "$s1os" = "SunOS" ]; then
  binaries="$binaries:roleadd:groupadd:getent"
fi

OLD_IFS=$IFS
IFS=:

for binary in $binaries; do
	binary_path=""
	for dir in $search_dirs; do
		if [ -x "$dir/$binary" ]; then
			binary_path="$dir/$binary"
			break
		fi
	done

	if [ -z "$binary_path" ]; then
		echo "Error: Unable to locate binary '$binary'."
		echo "       Please make certain this binary is on your path and try again."
		exit 1
	else
		eval "s1$binary=$binary_path"
	fi
done

IFS=$OLD_IFS

isRelativePath="`$s1dirname $0 | $s1cut -f 1 -d /`"
if [ "$isRelativePath" = "." -o "$isRelativePath" = ".." ]; then
	PRG="`pwd`/$0"
else
	PRG=$0
fi

progname=`$s1basename $PRG`

# Resolve symlinks, so that the pathname computations below find the
# directory structure they expect.
while [ -h "$PRG" ]; do
	# Get the target of the symlink.  N.B.:  We assume that neither the
	# link's value nor the pathname leading to it contains "-> ".
	ls=`$s1ls -ld "$PRG"`
	link=`$s1expr "$ls" : '^.*-> \(.*\)$'`
	# If the link is absolute, use it as is; otherwise, substitute it
	# into the leafname part of $PRG.
	if $s1expr "$link" : '^/' > /dev/null; then
		prg="$link"
	else
		prg="`$s1dirname $PRG`/$link"
	fi
	PRG=`type -p "$prg"` >/dev/null 2>&1
done

INSTALLHOME=`$s1dirname "$PRG"`

# Support shared library path support for both Linux and Solaris
# NOTE: These shared library path support should come only after searching
#       in our bundle lib location.
#
libpath_prefix=${INSTALLHOME}/lib
if [ "$s1os" = "Linux" ]; then
  MPS_LIB_DIRS="/opt/sun/private/lib:/opt/sun/private/share/lib:/opt/sun/lib"
  libpath_prefix=${INSTALLHOME}/lib:${MPS_LIB_DIRS}
fi
if [ "$s1os" = "SunOS" ]; then
  MPS_LIB_DIRS="/usr/lib/mps/secv1:/usr/lib/mps:/usr/lib/mps/sasl2"
  libpath_prefix=${INSTALLHOME}/lib:${MPS_LIB_DIRS}
fi

# Set LD_LIBRARY_PATH
libpath_end="${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
LD_LIBRARY_PATH="${libpath_prefix}${ldlibpath_end}"
export LD_LIBRARY_PATH

# Set SHLIB_PATH for HP-UX
libpath_end="${SHLIB_PATH:+:$SHLIB_PATH}"
SHLIB_PATH="${libpath_prefix}${ldlibpath_end}"
export SHLIB_PATH

# Set LIBPATH for AIX
libpath_end="${LIBPATH:+:$LIBPATH}"
LIBPATH="${libpath_prefix}${ldlibpath_end}"
export LIBPATH

# On Solaris, we have registered the webservd username / groupname to
# be associated with uid 80 / gid 80.  On other OS' we continue to
# use nobody / nobody as the default.

if [ "$s1os" = "SunOS" ] && [ `$s1whoami` =  "root" ]; then
	if $s1getent group webservd > /dev/null 2>&1; then
		group_entry=`$s1getent group webservd 2>&1`
		gid=`echo $group_entry | $s1cut -f 3 -d':'`
		
		if [ "$gid" != "80" ]; then
			echo "Warning: Non-standard webservd group detected with gid=$gid."
			echo "         Expected webservd to have gid=80."
		fi
	else
	   	if $s1groupadd -g 80 webservd >> /tmp/s1ws-setup.log.$$ 2>&1; then
			:
		else
			echo "Warning: Unable to install the webservd group at gid 80." 1>&2
		fi
	fi

	if $s1getent passwd webservd > /dev/null 2>&1; then
		passwd_entry=`$s1getent passwd webservd 2>&1`
		uid=`echo $passwd_entry | $s1cut -f 3 -d':'`
		gid=`echo $passwd_entry | $s1cut -f 4 -d':'`

		if [ "$uid" != "80" ]; then
			echo "Warning: Non-standard webservd user detected with uid=$uid." 1>&2
			echo "         Expected webservd to have uid=80."
		fi
		if [ "$gid" != "80" ]; then
			echo "Warning: Non-standard webservd user detected with primary gid=$gid." 1>&2
			echo "         Expected webservd to have gid=80."
		fi
	else
		$s1roleadd -u 80 -g webservd webservd >> /tmp/s1ws-setup.log.$$ 2>&1
		err_code=$?
		if [ $err_code -eq 0 ]; then
			:
		elif [ $err_code -eq 9 ]; then
			# webservd user already exists at uid 80.
			:
		else
			echo "Warning: Unable to install the webservd user at uid 80." 1>&2
			echo "         View /tmp/s1ws-setup.log.$$ for more details"
		fi
	fi
fi

prog="$INSTALLHOME/.${progname}"

# Run.
if [ -x "$prog" ]
then
     exec "$prog" "$@"
else
    echo >&2 "$0: `basename ${prog}` was not found in `$s1dirname ${prog}`"
    exit 1
fi

