#!/bin/sh

set -e

CPREFIX=/usr/local/cpanel
APACHECTL=/etc/httpd/bin/apachectl
HTTPDCONF=/etc/httpd/conf/httpd.conf
CONFD=/etc/httpd/conf/sites
ZZNAME=zz011_sitebuilder.conf

if [ ! -d $CPREFIX ]; then
	echo "You don't have CPanel installed in $CPREFIX";
	exit 1;
fi

if [ ! -f $CONFD/$ZZNAME ]; then
	echo "Run sb_config to generate SiteBuilder configuration file first";
	exit 1;
fi

foo=`sed -ne '/^\s*Include/ {p;q}' $HTTPDCONF | cut -d" " -f2`

if [ "$foo" ]; then
	if ls -l $foo | grep -q $ZZNAME >/dev/null 2>&1; then
		echo "$ZZNAME is already included from $HTTPDCONF"
	else
		$dir=`dirname $foo`;
		echo "$HTTPDCONF includes '$dir', moving $ZZNAME there";
		[ ! -f "$dir"/$ZZNAME ] && mv $CONFD/$ZZNAME "$dir";
	fi
else
	echo "Adding $CONFD/$ZZNAME to $HTTPDCONF includes"
	echo "Include $CONFD/$ZZNAME" >> $HTTPDCONF
fi

$APACHECTL restart

echo "Done"

