#!/bin/ksh
#
# create_site.sh - private script to generate an empty site directory
# from the configuration templates
#
# create_site.sh [site_name] [site_root] [sws install path]

#-------------------------------------------------------------------------------
#
#	PutFile template-file target-file
#
# Routine to 'situate' a template by replacing macro-strings and
# by either removing or unadorning variant lines 
# (currently, lines marked #SERVLET)

PutFile() {
    INPUT=$1
    OUTPUT=$2

    #
    # Except for the default_site, url block must begin with
    # url //website {
    #     ^^^^^^^^^^
    #
    TMPSTR=""
    if [ "$SITE_NAME" != "default_site" ]; then
	TMPSTR="//$SITE_NAME"
    fi

    M4="/usr/ccs/bin/m4 -DSITE_ROOT=$SITE_ROOT -DSITE_NAME=$TMPSTR" 

    if [ -d ${SWSPATH}/etc/http/site_template/servlets ]; then
	HAVESERVLETS=1
    else
	HAVESERVLETS=0
    fi

    if [ $HAVESERVLETS -eq 1 ]; then
	$M4 $INPUT | sed 's/#SERVLET//g' > $OUTPUT;
    else
	$M4 $INPUT | grep -v '#SERVLET' > $OUTPUT;
    fi;
}

#-------------------------------------------------------------------------------
#
# Argument processing

if [ $# -lt 2 ]; then
	echo `gettext SUNWhttp "usage: create_site.sh site_name site_root [sws_install_path] [site_config_file]"`
	echo `gettext SUNWhttp "provide \"-\" for optional parameters."`
	exit 1
fi

SITE_NAME=$1
SITE_ROOT=$2
SWSPATH=$3
SITE_CONFIG=$4

if [ x"-" = x"$SWSPATH" ]; then
	SWSPATH="/"
fi

if [ "/" = "$SWSPATH"  ]; then
	SWSPATH=""
fi

if [ ! -d ${SWSPATH}/etc/http ]; then
	echo
	echo `gettext SUNWhttp "SWS does not appear to be installed in"` $SWSPATH
	echo
	echo `gettext SUNWhttp "usage: create_site.sh site_name site_root [sws_install_path]"`
	exit 2
fi

if [ "/" = "$SWSPATH"  ]; then
	SWSPATH=""
fi

if [ x"-" = x"$SITE_CONFIG" ]; then
	SITE_CONFIG=${SITE_ROOT}/conf/${SITE_NAME}.site.conf;
fi

#-------------------------------------------------------------------------------
# Body - Build the site root area from the template tree.
#        We do nothing if the site configuration file already exists.
#        (This script is called from 'hthost add', which can be used
#        whether or not the host configuration files exist.)

if [ ! -f ${SITE_CONFIG} ]; then

	if [ ! -d $SITE_ROOT/conf ]; then

	    if [ ! -d ${SWSPATH}/etc/http/site_template ]; then
		echo `gettext SUNWhttp "The site template directory is not installed."`
		echo `gettext SUNWhttp "No site definition can be created."`
		exit 1
	    fi;

	    echo `gettext SUNWhttp "Creating site directory:"`
		echo "$SITE_ROOT"
	    mkdir -p $SITE_ROOT
	    chown -f adm:adm ${SITE_ROOT}

	    /bin/cp -rp ${SWSPATH}/etc/http/site_template/* ${SITE_ROOT}
	    /bin/rm -f ${SITE_ROOT}/conf/site.conf
	    PutFile ${SITE_ROOT}/conf/map.conf ${SITE_ROOT}/conf/ins.map.$$
	    mv -f ${SITE_ROOT}/conf/ins.map.$$ ${SITE_ROOT}/conf/map.conf
	    chown -f adm ${SITE_ROOT}/conf/map.conf
	    chgrp -f adm ${SITE_ROOT}/conf/map.conf
	fi

	# Generate the site configuration file.
	# BUG - need to account for ownership - is adm the only choice?
	#

	if [ -f ${SWSPATH}/etc/http/site_template/conf/site.conf ]; then

		echo `gettext SUNWhttp "Creating site configuration:"`
		echo "${SITE_CONFIG}"

		PutFile ${SWSPATH}/etc/http/site_template/conf/site.conf \
	        	${SITE_CONFIG};

		chown -f adm ${SITE_CONFIG};
		chgrp -f adm ${SITE_CONFIG};
	else
		echo `gettext SUNWhttp "The site template is not installed."`
		echo `gettext SUNWhttp "No site definition can be created."`
		exit 1
	fi;
fi

