#!/bin/ksh
#
# ident "@(#)M21HTTP.ksh	1.4 04/09/30 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: HTTP
# AUTHOR     : Andy Yang
# DESCRIPTION: 
#
# The following exported variables (initialized by the master task) are
# available (read-only) for the module:
#
# G_PROGRAM_ID    : program name
# G_MEDIA_DIR     : pathname of the install directory on the media (CD,...)
# G_PRODUCT_DIR   : pathname of the bundles directory on the media (CD,...)
# G_ADMIN_FILE    : pathname of the admin file used by pkgadd. default provided.
# G_DEBUG         : debug on/off. Possible values "yes", "no"
# G_QUICK_INSTALL : used to force a quick install (no user inputs).
#                   possible values "yes", "no"
# G_TMP_DIR       : pathname of the temporary directory
# G_UT_PROD_NAME  : product name
# G_UT_VERSION    : product version
# G_DAEMON_LOC    : location of OS daemon scripts
# G_LOGFILE       : pathname of the log file.
#

EstablishUtwsconfig() {
  VAR_OPT_UT="/var/opt/SUNWut"
  ETC_OPT_UT="/etc/opt/SUNWut"
  UT_HTTP_VAR=$VAR_OPT_UT/http
  TMP_DIR_PATH="${VAR_OPT_UT}/tmp"
  TMP_PROGID=${TMP_DIR_PATH}/$G_PROGRAM_ID.$$
  TMP_FILE="${TMP_PROGID}.tmp"

  SetPlatformSpecs

  if [[ $? == "1" && $OS == "SunOS" ]]; then
	COMPLETE_WS_CONFIG=false
  fi
  
  UTADMIN_GROUP=utadmin
  UTA_BASEDIR=/opt/SUNWut
  UT_INST_SITE=$UT_HTTP_VAR/docroot
  
  APACHECFG="$APACHEDIR"/httpd.conf
  APACHETMPCFG="$ETC_OPT_UT"/http/apache.conf
  USE_EXISTING_HTTP_CFG=false

  TOKEN_DIR="${VAR_OPT_UT}/cgitokens"

  EN_DOC_DIR="/opt/SUNWut/doc/en/html"
  JA_DOC_DIR="/opt/SUNWut/doc/ja/html"

  LOCAL_EN_DOC_DIR="lib/locale/en_US/docdir"
  LOCAL_FR_DOC_DIR="lib/locale/fr/docdir"
  LOCAL_JA_DOC_DIR="lib/locale/ja/docdir"
  LOCAL_ZH_DOC_DIR="lib/locale/zh/docdir"

  BEFORE="before.$G_PROGRAM_ID"

  typeset COMMENT="ut admin web server cgi user"
}

#
# Module developers to provide the following functions:
# Module_Init, Module_Preserve, Module_Remove, Module_Install, Module_Restore
# Module_Abort, Module_Exit
#

#
# Module_Init()
#
Module_Init() {

  exit_no=0

  . ${G_MEDIA_DIR}/support_lib/sras_config
  EstablishUtwsconfig
  if IsInstallRequired; then
      HTTP_PreInstall
      exit_no=$?

  elif IsPreserveRequired; then
      if HttpConfigured; then
	HTTP_PrePreserve
	exit_no=$?
	AddPostInitMessage \
        "Preserve [ Configuration for Sun Ray Web Administration ]"
      fi
      
  fi

  return $exit_no
}

HTTP_PreInstall()
{
  if HttpConfigured ; then
	if [[ $APACHE_INSTALLED != "none" ]]; then
		if ! ApacheConfigured ; then
			ApacheVersion
			GetCurrentSRVersion
            		typeset -r ver=$_RETURN_VAL
                	if ReplyIsYes "\nAn installation of Apache Web Server version " \
			"$APACHE_VERSION has been" \
			"\ndetected at $APACHEDIR.  Would you like to upgrade the" \
                	"\nexisting $ver Sun Ray Web Administration configuration" \
                	"\nto this installation?" ; then
                		COMPLETE_WS_CONFIG=true
                		AddPostInitMessage \
                        	"Upgrade\t [ Configuration for Sun Ray Web Administration ]"
        		else
                		COMPLETE_WS_CONFIG=false
                		AddPostInitMessage \
                        	"Save\t [ Configuration for Sun Ray Web Administration ]"
			fi
		else
			COMPLETE_WS_CONFIG=true
			AddPostInitMessage \
			"Upgrade\t [ Configuration for Sun Ray Web Administration ]"
		fi
	else
		COMPLETE_WS_CONFIG=false
		AddPostInitMessage \
		"Save\t [ Configuration for Sun Ray Web Administration ]"
        fi
	_DO_REMOVE="yes"
	_SW_INSTALLED="yes"
  elif SRASConfigured ; then
	_SW_INSTALLED="yes"
	if [[ $APACHE_INSTALLED != "none" ]]; then
		ApacheVersion
		GetCurrentSRVersion
		typeset -r ver=$_RETURN_VAL
		if [[ $APACHE_INSTALLED == "one" ]]; then
                	if ReplyIsYes "\nAn installation of Apache Web Server version " \
			"$APACHE_VERSION has been" \
                	"\ndetected at $APACHEDIR.  Would you like to migrate" \
                	"\nthe pre-$ver Sun Ray Web Administration configuration" \
                	"\nto this installation?" ; then
                		COMPLETE_WS_CONFIG=true
				AddPostInitMessage \
         				"Migrate\t [ Configuration for Sun Ray Web Administration ]"
        		else
                		COMPLETE_WS_CONFIG=false
				AddPostInitMessage \
         				"Save\t [ Configuration for Sun Ray Web Administration ]"
			fi
		else
			if ! ReplyIsYes "\nThere is an instance of Apache detected in both" \
			"\n/etc/httpd and /usr/apache.  The default is /etc/httpd." \
			"\nWould you like to use this instance?  Answering \"no\"" \
			"\nwill configure the /usr/apache instance." ; then
				APACHETMP="$ETC_OPT_UT"/http/templates/apache.tpl
				APACHECTL="/usr/apache/bin/apachectl"
				APACHEDIR="/usr/apache/conf"
				APACHECFG="$APACHEDIR"/httpd.conf
			fi
			COMPLETE_WS_CONFIG=true
			AddPostInitMessage \
				"Migrate\t [ Configuration for Sun Ray Web Administration ]"
                       	UT_LOCATION=$APACHEDIR
		fi
	else
		COMPLETE_WS_CONFIG=false
                AddPostInitMessage \
                "Save\t [ Configuration for Sun Ray Web Administration ]"
        fi
  fi

  return 0
}

HTTP_PrePreserve()
{
  if HttpConfigured ; then
      _SW_INSTALLED="yes"
  fi
  if SRASConfigured ; then
        if [[ $APACHE_INSTALLED != "none" ]]; then	
		ApacheVersion
		if [[ $APACHE_INSTALLED == "one" ]]; then
			if ReplyIsYes "\nAn installation Apache Web Server version " \
			"$APACHE_VERSION has been" \
			"\ndetected at $APACHEDIR.  Would you like to migrate" \
			"\nthe pre-3.0 Sun Ray Web Administration configuration" \
			"\nto this installation?" ; then
				COMPLETE_WS_CONFIG=true
			else
				COMPLETE_WS_CONFIG=false
			fi
		else
			if ! ReplyIsYes "\nThere is an instance of Apache detected in both" \
                        "\n/etc/httpd and /usr/apache.  The default is /etc/httpd." \
                        "\nWould you like to use this instance?  Answering \"no\"" \
                        "\nwill configure the /usr/apache instance." ; then
                                APACHETMP="$ETC_OPT_UT"/http/templates/apache.tpl
                                APACHECTL="/usr/apache/bin/apachectl"
                                APACHEDIR="/usr/apache/conf"
                                APACHECFG="$APACHEDIR"/httpd.conf
                        fi
                        COMPLETE_WS_CONFIG=true			
			UT_LOCATION=$APACHEDIR
		fi
	fi
  fi

  return 0
}

#
# Module_Preserve()
#

Module_Preserve() {

  . ${G_MEDIA_DIR}/support_lib/sras_config

  SaveFiles $HTTP_PRESERVE_FILE
  if [[ $? != "0" ]]; then
     return 2
  fi

  return 0

}

#
# Module_Restore()
#

Module_Restore() {

  . ${G_MEDIA_DIR}/support_lib/sras_config

  for file in $HTTP_PRESERVE_FILE; do
	if GetPreservedFilePath "$file"; then
		preservedfile=${_RETURN_VAL}
		cp -p "$preservedfile" "$file"
	fi
  done

  if HttpConfigured ; then
  	EstablishUtwsconfig
	InitHttpVars
        HttpInfo $HTTPCFG
	rm $HTTPCFG
        cp $HTTPTMP $HTTPCFG
	UpdateHttpCfg $HTTPCFG
	if ! $COMPLETE_WS_CONFIG ; then
		COMPLETE_CONFIG=false
	fi
	ConfigHttp
	if [[ $APACHE_INSTALLED == "none" || $COMPLETE_CONFIG == "false" ]]; then
        	print "Warning: The installation was unable to configure Apache\n" \
              		"in /usr/apache.  However the configuration from the\n" \
              		"previous installation has been preserved in a temporary\n" \
              		"configuration file.  Please see the Administration Guide\n" \
			"to complete the configuration."
        fi
        echo "Restoration of the web server configuration complete."
  fi

  return 0

}

#
# Module_Remove()
#

Module_Remove() {

  . ${G_MEDIA_DIR}/support_lib/sras_config
  EstablishUtwsconfig
  InitHttpVars
  UnconfigHttp

  return 0

}

#
# Module_Install()
#

Module_Install() {

  return 0

}

#
# Module_Abort()
#

Module_Abort() {

   return 0

}

#
# Module_Exit()
#
# Description:
#    Exit procedure (normal termination)
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Exit() {

   return 0

}

#
# END
#

#
# MAIN STARTS HERE
#

trap "exit 2" HUP INT QUIT TERM

#
# include libraries
#

. ${G_MEDIA_DIR}/support_lib/upgrade_lib
. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/module_lib

export _EVENT=$1
export _MODULE_NAME=$(basename $0)
export _VARS_LIST=""
export _EXIT_CODE=0
export _RETURN_VAL=0
export _VAR_STORAGE_FILE="${G_TMP_DIR}/.${G_PROGRAM_ID}.${_MODULE_NAME#???}"

DeclareModuleVar _DO_INSTALL="no"
DeclareModuleVar _DO_REMOVE="no"
DeclareModuleVar _SW_INSTALLED="no"
DeclareModuleVar _SW_COMPATIBLE="yes"
DeclareModuleVar COMPLETE_WS_CONFIG=true

#
# BEGIN: Developers module variables definition here
#
export UT_ETC_DIR="/etc/opt/SUNWut"
export HTTPCFG="$UT_ETC_DIR/http/http.conf"
export HTTPTMP="$UT_ETC_DIR/http/templates/http.tpl"

export HTTP_PRESERVE_FILE="$HTTPCFG"

#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
