#!/bin/bash

#install ssl package, if the command line parameter is not "noAptGet"
if test ! "$1" = "noAptGet"
then
	apt-get update
	apt-get install libapache-mod-ssl
fi

if [ -d /etc/apache/conf.d ]
then
	cp /m23/bin/ssl-data/* /etc/apache/conf.d/
fi

/m23/bin/mkCert.sh $2

#If an additional IP is given, make it be used as default
if [ $2 ]
then
	cd /m23/data+scripts/packages/baseSys/
	mv $2/* .
	cd /etc/apache/m23
	mv $2/* .
fi

if [ -f /etc/init.d/apache ]
then
	/etc/init.d/apache restart
fi

if [ -f /etc/init.d/apache2 ]
then
	#Enable SSL module
	ln -s /etc/apache2/mods-available/ssl.* /etc/apache2/mods-enabled
	/etc/init.d/apache2 restart
	
	if [ `grep 443 -c /etc/apache2/ports.conf` -eq 0 ]
	then
		echo "Listen 443" >> /etc/apache2/ports.conf
	fi

	# Check, if apache2ctl is present
	type apache2ctl &> /dev/null
	if [ $? -eq 0 ]
	then
		# Check, if Apache 2.4 is installed
		if [ $(apache2ctl -v | grep version | grep -c '/2\.4') -gt 0 ]
		then
			sed -i '/.*shmcb.*/d' /etc/apache2/mods-enabled/ssl.conf
			sed -i -e '/.*Order allow,deny/d' -e 's/Allow from all/Require all granted/g' /etc/apache2/sites-enabled/m23.conf
		fi
	fi
fi