#! /bin/bash

useSSL="no"
keyFile=""
certFile=""
certChainFile=""
ipAddr="*"
port="80"


eecho(){
	echo -e $@
}

help(){
	eecho "Usage: $0 [options] <hostname> <documentroot>"
	eecho ""
	eecho "Options:"
	eecho "\t-l\t--log-prefix\tLog prefix for this virtual server"
	eecho "\t-a\t--admin\tServer administraotr email address"
	eecho "\t-i\t--ip\tIP address to listen on"
	eecho "\t-p\t--port\tPort number to listen on"
	eecho "\t<hostname>\t\tHostname for this virtual server"
	eecho "\t<documentroot>\t\tDocument Root for this virtual server"
	eecho ""
	eecho "SSL options:"
	eecho "\t-s\t--ssl\t\tEnable SSL"
	eecho "\t-k\t--key\t\tKey file to use for SSL"
	eecho "\t-c\t--cert\t\tCertificate file to use for SSL"
	eecho "\t\t--cert-chain\tCertificate chain file to use for SSL"
}

if [ $# -lt 2 ]; then
	help
	exit 1
fi


while [ $# -gt 2 ]; do
	case "$1" in
		--ssl | -s)
			useSSL="yes"
			# Upgrade port to standard SSL
			if [ "x${port}" == "x80" ]; then port="443"; fi
			;;
		--log-prefix | -l)
			shift
			logPrefix="$1"
			;;
		--port | -p)
			shift
			port="$1"
			;;
		--ip | -i)
			shift
			ipAddr="$1"
			;;
		--admin | -a)
			shift
			serverAdmin="$1"
			;;
		--key | -k)
			shift
			keyFile="$1"
			;;
		--cert | -c)
			shift
			certFile="$1"
			;;
		--cert-chain)
			shift
			certChainFile="$1"
			;;
		--help | -h | -?)
			help
			;;
		*)
			echo "Unrecognised option $1"
			help
			exit 1
			;;
	esac
	shift
done

hostName="$1"
docRoot="$2"

#Sanitise the log prefix if not set.
if [ "x${logPrefix}" == "x" ]; then logPrefix="/var/log/apache2/${hostName}"; fi

#Sanitise the server admin if not set.
if [ "x${serverAdmin}" == "x" ]; then serverAdmin="admin@${hostName}"; fi

if [ ! -d ${docRoot} ]; then
	echo "Document root ${docRoot} does not exist.  Not creating a vhost that points nowhere"
	exit 1
fi

if [ "x${useSSL}" == "xyes" ]; then
	if [ "x${certFile}" == "x" ]; then echo "SSL requested but no certificate file specified."; exit 1; fi
	if [ "x${keyFile}" == "x" ]; then echo "SSL requested but no key file specified."; exit 1; fi
fi

eecho "<VirtualHost ${ipAddr}:${port}>"
eecho "\tServerAdmin ${serverAdmin}"
eecho "\tDocumentRoot ${docRoot}"
eecho "\tServerName ${hostName}"
eecho "\tServerAlias www.${hostName}"
eecho "\t<Directory ${docRoot}>"
eecho "\t\tOptions FollowSymLinks MultiViews Indexes"
eecho "\t\tAllowOverride All"
eecho "\t\tOrder allow,deny"
eecho "\t\tallow from all"
eecho "\t</Directory>"
eecho "\tErrorLog ${logPrefix}.error"
eecho "\tCustomLog ${logPrefix}.access combined"

if [ "x${useSSL}" == "xyes" ]; then
	eecho "\tSSLEngine on"
	eecho "\tSSLCertificateFile ${certFile}"
	eecho "\tSSLCertificateKeyFile ${keyFile}"
	if [ "x${certChainFile}" != "x" ]; then eecho "\tSSLCertificateChainFile ${certChainFile}"; fi
	eecho "\t<FilesMatch \"\\.(cgi|shtml|phtml|php)\$\">"
	eecho "\t\tSSLOptions +StdEnvVars"
	eecho "\t</FilesMatch>"
	eecho "\tBrowserMatch \"MSIE [2-6]\" \\"
	eecho "\t\tnokeepalive ssl-unclean-shutdown \\"
	eecho "\t\tdowngrade-1.0 force-response-1.0"
	eecho "\tBrowserMatch \"MSIE [17-9]\" ssl-unclean-shutdown"
fi
eecho "</VirtualHost>"
