/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 *
 */ 

##############################################################
#
# Configure an Apache Server 1.3.29 or 2.0.48
#
##############################################################

agent_configure() {
    typeset -i status=0
    typeset apache_config_file=dsame.conf
    APACHE_VERSION=`$SERVER_BINARY/httpd -v |grep "Server version"|awk '{print $3}' |sed -n 's/Apache\///p' | awk -F'.' '{print $1}'`


    $ECHO "`$GETTEXT 'Configuring Apache Web Server ...'`"
    $ECHO "`$GETTEXT 'Server version:' $APACHE_VERSION` "

    if [[ "$APACHE_VERSION" == "2" ]]; then
        EAPI="2";
    elif [[ "$EAPI" == "true" ]]; then
	EAPI="_ssl";
    else
        EAPI="";
    fi
    
    CERT_DIR="$SERVER_INSTANCE/cert"

    # the following echo are use for debug 
    # $ECHO CERT_DIR=$CERT_DIR
    # $ECHO "$PKG_DIR/config/$apache_config_file.orig"
    # $ECHO AGENT_CONFIG_DIR=$AGENT_CONFIG_DIR
    # $ECHO apache_config_file=$apache_config_file
    # $ECHO COMMON_PKG_BASEDIR=$COMMON_PKG_BASEDIR
    # $ECHO COMMON_PKG_PRODUCT_DIR=$COMMON_PKG_PRODUCT_DIR

    common_agent_dir=$COMMON_PKG_BASEDIR/$COMMON_PKG_PRODUCT_DIR

    # $ECHO common_agent_dir=$common_agent_dir
    # $ECHO "$SED -e 's#MODULE#$PKG_DIR/lib#g' -e 's#DSAMECONFIG#$AGENT_CONFIG_DIR/$AGENT_PROPERTIES_FILE_NAME#g' -e 's#SHAREDLIBDIR#$PKG_DIR/lib#g' $PKG_DIR/config/$apache_config_file.orig > $AGENT_CONFIG_DIR/$apache_config_file"

    $SED -e "s#MODULE#$common_agent_dir/apache/lib#g" \
	 -e "s#DSAMECONFIG#$AGENT_CONFIG_DIR/$AGENT_PROPERTIES_FILE_NAME#g" \
	 -e "s#EAPI#$EAPI#g" \
	 $PKG_DIR/config/$apache_config_file.orig > $AGENT_CONFIG_DIR/$apache_config_file

    if [[ $? -eq 0 ]]; then
	cd $SERVER_INSTANCE
        $ECHO SERVER_INSTANCE=$SERVER_INSTANCE

	cat_line httpd.conf "include $AGENT_CONFIG_DIR/$apache_config_file"
	status=$(($status + $?))
        
        # orig
	# typeset agent_doc_dir=`$GREP \^DocumentRoot httpd.conf | $TAIL -1 | $SED -e "s/DocumentRoot//" | $TR -d '" '`$AGENT_DEPLOY_URI/html

        APACHEDOCROOT=`grep \^DocumentRoot httpd.conf|tail -1 | sed -e "s/DocumentRoot//" | $TR -d '"' | $TR -d ' '`
        typeset agent_doc_dir=$APACHEDOCROOT$AGENT_DEPLOY_URI/html

        # $ECHO APACHEDOCROOT=$APACHEDOCROOT
        # $ECHO AGENT_DEPLOY_URI=$AGENT_DEPLOY_URI
        # $ECHO agent_doc_dir=$agent_doc_dir
        # $ECHO AGENT_ACCESS_DENY_FILE=$AGENT_ACCESS_DENY_FILE

	$MKDIR -p $agent_doc_dir
	status=$(($status + $?))

	if [[ $status -eq 0 ]]; then

            if [ ! -d $CERT_DIR ]; then
                mkdir $CERT_DIR
            fi
	    cd $CERT_DIR

	    if [ -z "$LD_LIBRARY_PATH" ] ; then
	        LD_LIBRARY_PATH="$PKG_DIR/lib:$common_agent_dir/lib"
	    else
		LD_LIBRARY_PATH="$PKG_DIR/lib:$common_agent_dir/lib:$LD_LIBRARY_PATH"
	    fi
	    export LD_LIBRARY_PATH
            # $ECHO LD_LIBRARY_PATH=$LD_LIBRARY_PATH

	    if [ ! -f cert7.db ]; then
		$ECHO "password" > password.txt
		#$PKG_DIR/cert/certutil -N -d . -f password.txt
		status=$(($status + $?))
		$CHMOD -f 644 *.db
		$RM -f password.txt
	    fi

	    NEW_OWNER=`$GREP "^User[^a-zA-Z0-9]" $SERVER_INSTANCE/httpd.conf | $AWK '{ print $2 }'`
            # $ECHO NEW_OWNER=$NEW_OWNER
	    if [[ -z "$NEW_OWNER" ]]; then
		NEW_OWNER=nobody
	    fi
	fi

	if [[ $status -ne 0 ]]; then
	    restore_backup_file httpd.conf
            if [ -d $CERT_DIR ]; then
               $RM -rf $CERT_DIR
            fi
	    $RM -rf $agent_doc_dir
	fi
    else
	$RM -f $AGENT_CONFIG_DIR/$apache_config_file
	status = 1
    fi
    
    # For apache, check for both sides SSL/nonSSL     
    #if [[ "$PRIMARY_SERVER_PROTOCOL" == "https" ]] && [[ "$AGENT_PROTOCOL" == "https" ]]; then
        NOTIFICATION_ENABLE=false;
        LOG_ROTATION=false;
    #fi

    
    # $ECHO status=$status
    return $status
}

##############################################################
#
# Additional callback routines.
#
##############################################################

is_apache_instance() {
    is_apache_binary $1	
    SERVER_BINARY=$1
    [[ -f "$SERVER_INSTANCE"/httpd.conf ]]
}

agent_get_config() {
    get_web_service_instance "Enter the Apache Server Binary Directory:" "Invalid Apache server binary !" "$SERVER_BINARY" is_apache_instance
    agent_get_eapi "SSL Ready:" "Please enter true or false" "true" is_true_false
}

agent_init_variables() {
    SERVER_BINARY=/etc/apache
}

agent_get_eapi() {
    typeset value=
    get_value "$1" "$2" "$3" $4
    EAPI=$value
}

is_true_false() {
   [[ "x$1" == "xtrue" ]] || [[ "x$1" == "xfalse" ]]
}

is_apache_binary() {
    if [[ -f $1/httpd ]]; then
        HTTPD_ROOT=`"$1"/httpd -V|grep "HTTPD_ROOT"|awk '{print $2}'|tr -d [\"][\ ]|sed -n 's/HTTPD_ROOT=//p'`
        SERVER_CONFIG_DIR=`"$1"/httpd -V|grep "SERVER_CONFIG_FILE"|awk '{print $2}'|tr -d [\"][\ ]|sed -n 's/\/httpd.conf$//p'|sed -n 's/SERVER_CONFIG_FILE=//p'`
        SERVER_INSTANCE=$HTTPD_ROOT$SERVER_CONFIG_DIR
	# Check if httpd.conf really exists in the directory.
	# This check is necessary for servers like Oracle 9iAS that does not 
	# return the right conf directory in httpd -V. 
	# In this case check if the <instance>/../conf is the right directory. 
	# If not then we are out of luck - default to the given directory.
	if [ ! -f $SERVER_INSTANCE/httpd.conf ]; then 
	    CONFIG_DIR=`echo $1 | sed 's/\/bin/\/conf/'`
	    if [[ -f $CONFIG_DIR/httpd.conf ]]; then 
		SERVER_INSTANCE=$CONFIG_DIR
	    else
		value=$1
		SERVER_INSTANCE=$1
	    fi
	fi    
        value=$SERVER_INSTANCE
    else
	value=$1
	SERVER_INSTANCE=$1
    fi		
}
##############################################################
#
# The script itself, beginning with the standard preamble.
#
##############################################################

GETTEXT=/usr/bin/gettext
PKGPARAM=/usr/bin/pkgparam
COMMON_PKG_BASEDIR=`$PKGPARAM SUNWamapc BASEDIR 2>/dev/null`
if [ $? = 1 ]; then
    echo "`$GETTEXT 'Required package SUNWamapc is not installed. Exit...'`"
    exit 1
fi
COMMON_PKG_PRODUCT_DIR=`$PKGPARAM SUNWamapc PRODUCT_DIR 2>/dev/null`
. $COMMON_PKG_BASEDIR/$COMMON_PKG_PRODUCT_DIR/apache/bin/comlib

#$ECHO COMMON_PKG_BASEDIR=$COMMON_PKG_BASEDIR
#$ECHO COMMON_PKG_PRODUCT_DIR=$COMMON_PKG_PRODUCT_DIR
init_variables $COMMON_PKG_BASEDIR $COMMON_PKG_PRODUCT_DIR SUNWamapc

configure "$@"
