#!/bin/sh
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)setup_apache	1.9	02/02/22 SMI"
#
# Setup a scalable apache instance on a globally mounted filesystem

# Define command paths
SCRGADM=/usr/cluster/bin/scrgadm
SCSWITCH=/usr/cluster/bin/scswitch

# Define Apache Resource information
APACHE_RS=apache-rs
APACHE_RG=apache-rg
APACHE_RT=SUNW.apache

# Define Shared Address Resource Information
SHARED_ADDRESS_RG=sa-rg

# Define directories to use
GLOBAL_DIR=/global/mirror-1
HTDOCS_DIR=${GLOBAL_DIR}/apache-data/htdocs
CGI_DIR=${GLOBAL_DIR}/apache-data/cgi-bin
BINDIR=/usr/apache/bin
CONFDIR=/etc/apache
LOGDIR=/var/cluster/spm

# Define the file which contains the shared address to use
SHARED_ADDRESS_FILE=${LOGDIR}/shared_address

# Define the file which contains the cluster size
CLUSTER_FILE=${LOGDIR}/clustersize

# Define the SC Apache Data Service package name
APACHE_PKG=SUNWscapc

# First check that the proper package is installed.
pkginfo ${APACHE_PKG} > /dev/null
if [ $? -eq 1 ]
then
	echo "Scalable Apache requires the ${APACHE_PKG} package to be installed."
	exit 1
fi

# Second check to make sure that we have a global filesystem created.
df -k | grep "mirror-1" > /dev/null
if [ $? -eq 1 ]
then
	echo "Scalable Apache requires the global filesystem \"${GLOBAL_DIR}\"."
	exit 2
fi

# Create the directory for the htdocs and cgi-bin
mkdir -p ${HTDOCS_DIR} ${CGI_DIR}

# Get the logical host from /etc/logical_host
SHARED_ADDRESS=`cat $SHARED_ADDRESS_FILE`

# Get the cluster size
CLUSTERSIZE=`head -1 $CLUSTER_FILE`

# Now it looks like we can configure HS NFS.
echo "Starting to configure Scalable Apache..."

# Create a failover resource group to contain the shared address.
${SCRGADM} -a -g ${SHARED_ADDRESS_RG}

# Add the desired shared address resource into the resource group.
${SCRGADM} -a -S -g ${SHARED_ADDRESS_RG} -l ${SHARED_ADDRESS}

# Register the Apache resource type.
${SCRGADM} -a -t ${APACHE_RT}

# Create the scalable resource group
/opt/SUNWscvw/cgi-bin/log/syslog.pl "${SCRGADM} -a -g ${APACHE_RG} -y Desired_primaries=${CLUSTERSIZE} -y Maximum_primaries=${CLUSTERSIZE} -y Rg_Dependencies=${SHARED_ADDRESS_RG}"

${SCRGADM} -a -g ${APACHE_RG} \
	      -y Desired_primaries=${CLUSTERSIZE} \
	      -y Maximum_primaries=${CLUSTERSIZE} \
	      -y Rg_Dependencies=${SHARED_ADDRESS_RG}

# Create the Apache resource in the resource group.
${SCRGADM} -a -j ${APACHE_RS} \
	      -g ${APACHE_RG} \
	      -t ${APACHE_RT} \
	      -x Bin_Dir=${BINDIR} \
	      -x Confdir_list=${CONFDIR} \
	      -y Scalable=TRUE \
	      -y Network_Resources_Used=${SHARED_ADDRESS}

# Enable the resources and the resource monitors, manage the resource groups,
# and switch the resource groups into the online state.
${SCSWITCH} -Z -g ${SHARED_ADDRESS_RG}
${SCSWITCH} -Z -g ${APACHE_RG}

# Remove the shared address file so this won't run again
rm -f ${LOGDIR}/setup_apache_flag
