#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2012 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

# Documentation/Howto:
# http://www.ruzee.com/blog/2009/01/apache-virtual-hosts-a-clean-setup-for-php-developers 
# http://phphints.wordpress.com/2009/01/10/improving-php-performance-with-apache2-mpm-worker-mod_fcgid-2 
# http://blog.rvi-media.de/php/apache-sehr-extravertiert-signatur-verbergen-und-token-anpassen

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_APACHE2_SERVERNAME=${OMV_APACHE2_SERVERNAME:-"openmediavault-webgui"}
OMV_APACHE2_SITE_OPENMEDIAVAULT_CONFIG=${OMV_APACHE2_SITE_OPENMEDIAVAULT_CONFIG:-"/etc/apache2/sites-available/openmediavault-webgui"}
OMV_APACHE2_SITE_OPENMEDIAVAULT_LOGLEVEL=${OMV_APACHE2_SITE_OPENMEDIAVAULT_LOGLEVEL:-"warn"}
OMV_APACHE2_PORTS_CONFIG=${OMV_APACHE2_PORTS_CONFIG:-"/etc/apache2/ports.conf"}
OMV_APACHE2_WEBGUI_CONFIG_DIR=${OMV_APACHE2_WEBGUI_CONFIG_DIR:-"/etc/apache2/openmediavault-webgui.d"}
OMV_APACHE2_WEBGUI_DEFAULT_CONFIG=${OMV_APACHE2_WEBGUI_DEFAULT_CONFIG:-"${OMV_APACHE2_WEBGUI_CONFIG_DIR}/default.conf"}
OMV_APACHE2_SECURITY_CONFIG=${OMV_APACHE2_SECURITY_CONFIG:-"/etc/apache2/conf.d/security"}
OMV_APACHE2_FCGIDIOTIMEOUT=${OMV_APACHE2_FCGIDIOTIMEOUT:-"300"} # Apache default is 40

cat <<EOF > ${OMV_APACHE2_SECURITY_CONFIG}
ServerTokens Prod
ServerSignature Off
TraceEnable Off
EOF

# Create the generic openmediavault-webgui configuration file
xmlstarlet sel -t -m "//webadmin" \
  -o "ServerName ${OMV_APACHE2_SERVERNAME}" -n \
  -o "DocumentRoot ${OMV_DOCUMENTROOT_DIR}" -n \
  -o "DirectoryIndex index.php" -n \
  -o "SuexecUserGroup openmediavault openmediavault" -n \
  -o "Alias /extjs/ /usr/share/javascript/extjs3/" -n \
  -o "Alias /images/ ${OMV_DOCUMENTROOT_DIR}/images/" -n \
  -o "# Set maximum HTTP request length to 25 MiB" -n \
  -o "FcgidMaxRequestLen 26214400" -n \
  -o "FcgidIOTimeout ${OMV_APACHE2_FCGIDIOTIMEOUT}" -n \
  -o "&lt;Directory ${OMV_DOCUMENTROOT_DIR}/&gt;" -n \
  -o "    Options FollowSymLinks" -n \
  -o "    &lt;FilesMatch \.php$&gt;" -n \
  -o "        FcgidWrapper ${OMV_DOCUMENTROOT_DIR}/php-fcgi .php" -n \
  -o "        SetHandler fcgid-script" -n \
  -o "        Options +ExecCGI" -n \
  -o "    &lt;/FilesMatch&gt;" -n \
  -o "    Order Allow,Deny" -n \
  -o "    Allow from All" -n \
  -o "    AllowOverride None" -n \
  -o "&lt;/Directory&gt;" -n \
  -o "LogLevel ${OMV_APACHE2_SITE_OPENMEDIAVAULT_LOGLEVEL}" -n \
  -o "ErrorLog \${APACHE_LOG_DIR}/${OMV_APACHE2_SERVERNAME}_error.log" -n \
  -o "CustomLog \${APACHE_LOG_DIR}/${OMV_APACHE2_SERVERNAME}_access.log combined" \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_APACHE2_WEBGUI_DEFAULT_CONFIG}

# Create '/etc/apache2/sites-available/openmediavault-webgui' file
xmlstarlet sel -t -m "//webadmin" \
  -i "enablessl[. = '1']" \
	  -v "concat('&lt;VirtualHost *:',sslport,'&gt;')" -n \
	  -o "    Include ${OMV_APACHE2_WEBGUI_CONFIG_DIR}/*.conf" -n \
	  -o "    SSLEngine on" -n \
	  -v "concat('    SSLCertificateFile ${OMV_CERTIFICATE_DIR}/certs/',sslcertificateref,'.crt')" -n \
	  -v "concat('    SSLCertificateKeyFile ${OMV_CERTIFICATE_DIR}/private/',sslcertificateref,'.key')" -n \
	  -o "&lt;/Virtualhost&gt;" -n \
  -b \
  -i "enablessl[. = '0'] or forcesslonly[. = '0']" \
	  -v "concat('&lt;VirtualHost *:',port,'&gt;')" -n \
	  -o "    Include ${OMV_APACHE2_WEBGUI_CONFIG_DIR}/*.conf" -n \
	  -o "    SSLEngine off" -n \
	  -o "&lt;/Virtualhost&gt;" \
  -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_APACHE2_SITE_OPENMEDIAVAULT_CONFIG}

# Create '/etc/apache2/ports.conf' file
xmlstarlet sel -t -m "//webadmin" \
  -i "enablessl[. = '0'] or forcesslonly[. = '0']" \
	  -v "concat('NameVirtualHost *:',port)" -n \
	  -v "concat('Listen ',port)" -n \
  -b \
  -i "enablessl[. = '1']" \
	-o "&lt;IfModule mod_ssl.c&gt;" -n \
	-v "concat('NameVirtualHost *:',sslport)" -n \
	-v "concat('Listen ',sslport)" -n \
	-o "&lt;/IfModule&gt;" -n \
  -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_APACHE2_PORTS_CONFIG}
