#!/bin/bash

. ${TOOLKIT}-functions.sh

# select source install files based on apache2 major version
#
A2_VERSION=$(apache2_version || :)
case $A2_VERSION in
  2.2.*)	search_path_append "$MODRUNDIR/2.2" ;;
  2.4.*)	search_path_append "$MODRUNDIR/2.4" ;;
  "")		# packages not yet installed, we made a guess from distro id, since
  		# we switched to apache 2.4 starting from Devuan, and override by env
		# value returned by 'apache2_version' function
		#
  		case $DISTRIB_FULL_ID in
    		  Devuan*)	search_path_append "$MODRUNDIR/2.4"
		  		export A2_VERSION="2.4"
				;;
    		  *)		search_path_append "$MODRUNDIR/2.2"
		  		export A2_VERSION="2.2"
		 		;;
		esac
		;;
  *)		echo " ERROR: apache2 version $A2_VERSION UNSUPPORTED" >&2
  		exit 1
		;;
esac



# build allowed statemens for applications
#
allowed=`jtconf perms.applications_iplist`	|| exit_missing_define perms.applications_iplist

(
	echo "[temp]"
	echo "  allow	^^BLOCK^^"
	case $allowed in
	  [Aa][Ll][Ll]*) ;;
	  *) echo "		$(apache2_add_deny All)"
	esac
	for ip in $allowed
	do
		echo "		$(apache2_add_allow $ip)"
	done
	echo "^^BLOCK^^"
	echo
) >>$MODLOCALDB

# the ssl section uses chain certificate?
#
chain=$(jtconf cert.www.chain_fullpath 2>/dev/null) || :
if [ "X$chain" != "X" ]
then
	(
	  echo "[temp]"
	  echo "  ssl_chain_directive	SSLCertificateChainFile $chain"
	) >>$MODLOCALDB
else
	(
	  echo "[temp]"
	  echo "  ssl_chain_directive"
	) >>$MODLOCALDB
fi

exit 0
