#!/bin/ksh -p
#
# ident "@(#)utwsadm.ksh	1.11 04/10/25 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# utwsadm.ksh
#
# This is the CLI for handling web server configuration.  Currently
# only Apache installed in /usr/apache is supported.  However, the
# internal interface is designed to be extensible.  All of the config
# parameters are stored in /etc/opt/SUNWut/http/http.conf.
# 

PATH="/usr/sbin:/usr/bin:/bin:/sbin"
PROGRAM_ID=$(basename $0)
VAR_OPT_UT="/var/opt/SUNWut"
ETC_OPT_UT="/etc/opt/SUNWut"
UT_ETC_DIR="/etc/opt/SUNWut"
UT_HTTP_VAR=$VAR_OPT_UT/http
TMP_DIR_PATH="${VAR_OPT_UT}/tmp"
TMP_PROGID=${TMP_DIR_PATH}/$PROGRAM_ID.$$
TMP_FILE="${TMP_PROGID}.tmp"
UTADMIN_GROUP="utadmin"
BEFORE="before.$PROGRAM_ID"

if [[ -d /var/adm/log ]] ; then
  LOG_DIR="/var/adm/log"
elif [[ -d /var/log/SUNWut ]] ; then
  LOG_DIR="/var/log/SUNWut"
else
  LOG_DIR="/var/log"
fi

LOGFILE="${LOG_DIR}/${PROGRAM_ID}.$(date '+%Y_%m_%d_%H:%M:%S').log"

#
# See if G_MEDIA_DIR is already defined.
#
# Must be named G_MEDIA_DIR to match the install framework
# because sras_config depends on it and is also invoked
# from the M20SRAS install module.
#
if [[ -z $G_MEDIA_DIR ]] ; then

  # get the full pathname of the command
  typeset THIS_COMMAND=$(whence $0)

  # location of this package (ex. /opt/SUNWut, cdrom)
  typeset PRODUCT_DIR=${THIS_COMMAND%/*}

  # pathname of the install library directory
  G_MEDIA_DIR=${PRODUCT_DIR}

fi # -z $G_MEDIA_DIR

# source sras_config
. ${G_MEDIA_DIR}/support_lib/sras_config

UTA_BASEDIR="$(${G_MEDIA_DIR}/utprodinfo -r SUNWuta)/SUNWut"
UT_INST_SITE=$UT_HTTP_VAR/docroot

APACHETMPCFG="$ETC_OPT_UT"/http/apache.conf
HTTPTMP="$ETC_OPT_UT"/http/templates/http.tpl
HTTPCFG="$ETC_OPT_UT"/http/http.conf

SetPlatformSpecs

if [[ $? == "1" && $OS == "SunOS" ]] ; then
        print -u2 "Both /etc/apache/ and /usr/apache/conf exist.\n" \
              "Only one instance of Apache can be located in\n" \
              "/usr/apache.  Please remove one of the them\n" \
              "and try again."
        exit 5
fi

Usage() {
  print "Usage: $PROGRAM_ID\n" \
	    "\n\t Public interfaces:\n" \
	    "\n\t\t$PROGRAM_ID setup [-q]" \
	    "\n\t\t$PROGRAM_ID remove" \
	    "\n\t\t$PROGRAM_ID help\n" \
	    "\n\t Private interfaces:\n" \
	    "\n\t\t$PROGRAM_ID info" \
	    "\n\t\t$PROGRAM_ID display [ -i <config-file> ]" \
  	    "\n\t\t$PROGRAM_ID dump [ -o <config-file> ]" \
  	    "\n\t\t$PROGRAM_ID prompt -o <config-out-file> " \
  	    "\n\t\t$PROGRAM_ID init -i <config-file> [-n]" \
  	    "\n\t\t$PROGRAM_ID update -i <config-file> [-n]\n\n"
  exit $1
}

CheckUidIsZero() {
  case "$(id)" in
    'uid=0('*) return 0;;  # uid is zero
    *)         HttpFatal "must be run as UID 0 root";;
  esac
}

DoConfig()
{
	ApacheVersion
	InitHttpVars
	PromptForHttpParameters
	if $CONFIG_APACHE ; then
		if HttpConfigured; then
			UnconfigHttp
		fi
		if ! $USE_EXISTING_HTTP_CFG ; then
			cp $HTTPTMP $HTTPCFG
		else
			HttpInfo $HTTPCFG
		fi
		ConfigHttp
	fi
}

#
# main {
#

# UMASKS
UMASK_ROOT=066
UMASK_WORLD=022

umask $UMASK_WORLD

if (( $# == 0 )); then
  	Usage 5
fi

CONFIG_APACHE=false
FORCE=false

CheckUidIsZero

case "$1" in
'info')
	ApacheVersion
	print "Apache Web Server $APACHE_VERSION"
	;;
'display')
	PARSEFILE=$HTTPCFG
        if [[ -n "$2" ]]; then
                if [[ $2 == "-i" ]]; then
                        if [[ -f $3 ]]; then
                                PARSEFILE=$3
                        else
                                HttpFatal "$3 does not exist"
                        fi
                else
                        Usage 5
                fi
        fi
	UT_PORT=$($__AWK '$1~"Port" { print $2 }' $PARSEFILE)
	print "    Apache Web Server port number: $UT_PORT"
	REMOTE=$($__AWK '$1~"RemoteAdmin" { print $2 }' $PARSEFILE)
	print "    Remote server administration: $REMOTE"
	CGI_USER=$($__AWK '$1~/^'"User"'/ {
		if (length($1) == 4) {
			print $2
		}
                }' $PARSEFILE)
	print "    CGI username: $CGI_USER"
	;;
'dump')
	if [[ -n $2 ]]; then
		if [[ $2 == "-o" ]]; then
			cp -f $HTTPCFG $3
		else
			Usage 5
		fi
	else
		cat $HTTPCFG
	fi
	;;
'prompt')
	#
	# Return Values:
	# 0 - Use user prompted values and apply the config to Apache installation.
	# 1 - Use user prompted values but do not apply the config to Apache.
	# 2 - Use existing config and apply to Apache.
	# 3 - Use existing config ut do not apply to Apache.
	# 4 - Do not config.
	# 5 - Error
	#
 	if [[ $2 == "-o" ]]; then
		PromptForHttpParameters
		PROMPT_RETURN=$?
		if [[ $PROMPT_RETURN == "0" || $PROMPT_RETURN == "1" ]]; then
			cp -f $HTTPTMP $3
			UpdateHttpCfg $3
		elif [[ $PROMPT_RETURN == "2" || $PROMPT_RETURN == "3" ]]; then
			cp -f $HTTPCFG $3
		fi
		exit $PROMPT_RETURN
	else 
		Usage 5
	fi
	;;
'init')
	if [[ $2 == "-i" ]]; then
		if [[ -f $3 ]]; then
			if ApacheConfigured; then
				print -u2 "Configuration already exists.  Please use update\n" \
				      "command to change the current configuration."
			else
				cp -f $3 $HTTPCFG
				InitHttpVars
				HttpInfo $HTTPCFG
				if [[ $4 == "-n" ]]; then
					COMPLETE_CONFIG=false
				fi
				ConfigHttp
			fi
		else
			Usage 5
		fi
	else
		Usage 5
	fi
	;;
'update')
	if [[ $2 == "-i" ]]; then
                if [[ -f $3 ]]; then
			InitHttpVars
                        if HttpConfigured; then
				UnconfigHttp
                        fi
			cp $HTTPTMP $HTTPCFG
			cp -f $3 $HTTPCFG
			HttpInfo $HTTPCFG
			if [[ $4 == "-n" ]]; then
				COMPLETE_CONFIG=false
			fi
			ConfigHttp
                else
                        Usage 5
                fi
        else
                Usage 5
        fi
        ;;
'setup')
	if [[ $2 == "-q" ]]; then
		FORCE=true
		CONFIG_APACHE=true
		InitHttpVars
		HttpInfo $HTTPTMP
	fi
	DoConfig
	;;
'remove')
	if HttpConfigured ; then
		InitHttpVars
		UnconfigHttp
	fi
	;;
'test')
	if [[ $2 == "isConfigured" ]]; then
		if ApacheConfigured; then
			return 0
		else
			return 1
		fi
	elif [[ $2 == "isRunning" ]]; then
		/* Will implement later */
		return 0
	else
		Usage 5
	fi
	;;
'help')
	Usage 4
	;;
'*')
	Usage 5
	;;
esac
