###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# This program 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           #
# (at your option) any later version.                                         #
#                                                                             #
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################


###############################################################################
# Definitions
###############################################################################

include Config

VER        = 2.2.10

THISAPP    = httpd-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
ifeq "$(PASS)" "C"
	TARGET     = $(DIR_INFO)/$(THISAPP)-config
else
	TARGET     = $(DIR_INFO)/$(THISAPP)
endif

###############################################################################
# Top-level Rules
###############################################################################

objects = $(DL_FILE) \
	httpd-2.2.2-config-1.patch

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)
httpd-2.2.2-config-1.patch = $(DL_FROM)/httpd-2.2.2-config-1.patch

$(DL_FILE)_MD5 = 6697772ba5f8d34988fe297417ccaacc
httpd-2.2.2-config-1.patch_MD5 = e02a3ec5925eb9e111400b9aa229f822

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

md5 : $(subst %,%_MD5,$(objects))

###############################################################################
# Downloading, checking, md5sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_MD5,$(objects)) :
	@$(MD5)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
ifeq "$(PASS)" "C"
	# DO THIS IN AN EXTRA STEP BECAUSE PHP AND SUBVERSION WILL FAIL.

	cp -rf $(DIR_CONF)/httpd/* /etc/httpd/conf
	ln -sf $(CONFIG_ROOT)/main/hostname.conf /etc/httpd/conf/

	# Copy all html/cgi-bin files
	mkdir -p /srv/web/ipfire/{cgi-bin,html}
	mkdir -p /var/updatecache/{download,metadata}
	cp -aR $(DIR_SRC)/html/* /srv/web/ipfire

	# Change CONFIG_ROOT in cgi-scripts
	for i in /srv/web/ipfire/cgi-bin/{*,logs.cgi/*,vpn.cgi/*}; do \
	    if [ -f $$i ]; then \
	        sed -i "s+CONFIG_ROOT+$(CONFIG_ROOT)+g" $$i; \
	    fi; \
	done
	chown -R root:root /srv/web/ipfire
	chmod -R 755 /srv/web/ipfire/cgi-bin
	chmod -R 644 /srv/web/ipfire/html
	chmod 755 /srv/web/ipfire/html /srv/web/ipfire/html/{index.cgi,redirect.cgi,dial.cgi,images,include,themes,themes/*,themes/*/*}
else
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_DL)/httpd-2.2.2-config-1.patch

	### Add IPFire's layout, too
	echo "# IPFire layout" >> $(DIR_APP)/config.layout
	echo "<Layout IPFire>" >> $(DIR_APP)/config.layout
	echo "    prefix:             /usr" >> $(DIR_APP)/config.layout
	echo "    exec_prefix:        /usr" >> $(DIR_APP)/config.layout
	echo "    bindir:             /usr/bin" >> $(DIR_APP)/config.layout
	echo "    sbindir:            /usr/sbin" >> $(DIR_APP)/config.layout
	echo "    libdir:             /usr/lib" >> $(DIR_APP)/config.layout
	echo "    libexecdir:         /usr/lib/apache" >> $(DIR_APP)/config.layout
	echo "    mandir:             /usr/share/man" >> $(DIR_APP)/config.layout
	echo "    sysconfdir:         /etc/httpd/conf" >> $(DIR_APP)/config.layout
	echo "    datadir:            /srv/web/ipfire" >> $(DIR_APP)/config.layout
	echo "    installbuilddir:    /usr/lib/apache/build" >> $(DIR_APP)/config.layout
	echo "    errordir:           /srv/web/ipfire/error" >> $(DIR_APP)/config.layout
	echo "    iconsdir:           /srv/web/ipfire/icons" >> $(DIR_APP)/config.layout
	echo "    htdocsdir:          /srv/web/ipfire/htdocs" >> $(DIR_APP)/config.layout
	echo "    manualdir:          /srv/web/ipfire/manual" >> $(DIR_APP)/config.layout
	echo "    cgidir:             /srv/web/ipfire/cgi-bin" >> $(DIR_APP)/config.layout
	echo "    includedir:         /usr/include/apache" >> $(DIR_APP)/config.layout
	echo "    localstatedir:      /srv/web/ipfire" >> $(DIR_APP)/config.layout
	echo "    runtimedir:         /var/run" >> $(DIR_APP)/config.layout
	echo "    logfiledir:         /var/log/httpd" >> $(DIR_APP)/config.layout
	echo "    proxycachedir:      /var/cache/apache/proxy" >> $(DIR_APP)/config.layout
	echo "</Layout>" >> $(DIR_APP)/config.layout

	cd $(DIR_APP) && ./configure --enable-layout=IPFire \
				--enable-ssl --enable-mods-shared=all
	cd $(DIR_APP) && make $(MAKETUNING)
	cd $(DIR_APP) && make install
	chown -v root:root /usr/lib/apache/httpd.exp \
	    /usr/sbin/{apxs,apachectl,dbmmanage,envvars{,-std}} \
	    /usr/share/man/man1/{dbmmanage,ht{dbm,digest,passwd}}.1 \
	    /usr/share/man/man8/{ab,apachectl,apxs,htcacheclean,httpd}.8 \
	    /usr/share/man/man8/{logresolve,rotatelogs,suexec}.8
	@rm -rf $(DIR_APP)
endif
	@$(POSTBUILD)
