#!/bin/ksh
#
# ident "@(#)M21HTTP.ksh	1.6 07/06/07 SMI"
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: HTTP
# DESCRIPTION: Install script used for Admin GUI Apache configuration
#
# 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 [ data for Admin GUI ]"
      fi
      
  fi

  return $exit_no
}

HTTP_PreInstall()
{
  if HttpConfigured ; then
	_DO_REMOVE="yes"
	_SW_INSTALLED="yes"
        AddPostInitMessage \
        "Migrate\t [ data for Admin GUI ]"

  elif SRASConfigured ; then
	_SW_INSTALLED="yes"
  fi

  return 0
}

HTTP_PrePreserve()
{
  if HttpConfigured ; then
      _SW_INSTALLED="yes"
  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

}

#
# SetPropertyValue()
#
# Description:
#   Adapt the property value in new Admin GUI configuration
#
# Parameters:
#   $1 = The name of the property
#   $2 = The desired new value
#

SetPropertyValue() {
    
    sed 's|^'"${1}"'=.*|'"${1}"'='"${2}"'|' <"${SRWA_CFG}" >/tmp/webadmin.conf.$$
    mv /tmp/webadmin.conf.$$ "${SRWA_CFG}"
}

#
# 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
     UNSECURE_PORT=`grep '^Port' ${HTTPCFG} | cut -f2 -d' '`
     USER_NAME=`grep '^User' ${HTTPCFG} | cut -f2 -d' '`
     GROUP_NAME=`grep '^Group' ${HTTPCFG} | cut -f2 -d' '`
     REMOTE_ACCESS=`grep '^RemoteAdmin' ${HTTPCFG} | cut -f2 -d' '`

     if [[ "$REMOTE_ACCESS" = "no" ]] ; then
        REMOTE_ACCESS="127.0.0.1"
     else
        REMOTE_ACCESS=".*"
     fi
        
     SetPropertyValue "http.port" "$UNSECURE_PORT"
     SetPropertyValue "process.username" "$USER_NAME"
     SetPropertyValue "process.groupname" "$GROUP_NAME"
     SetPropertyValue "remote.access" "$REMOTE_ACCESS"
     rm -f ${HTTPCFG}
     
	fmt <<-!

		Migrated Admin GUI data used for Apache to new format used for Tomcat.

		Use 'utconfig -w' to specify Tomcat installation directory
		and to complete Admin GUI configuration.
    
	!
     echo "Restoration of Admin GUI data 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 SRWA_CFG="$UT_ETC_DIR/webadmin/webadmin.conf"

export HTTP_PRESERVE_FILE="$HTTPCFG"

#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
