###############################################################################
# This file is part of the IPCop Firewall.                                    #
#                                                                             #
# IPCop 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 2 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# IPCop 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 IPCop; if not, write to the Free Software                        #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA    #
#                                                                             #
# Makefiles are based on LFSMake, which is                                    #
# Copyright (C) 2002 Rod Roard <rod@sunsetsystems.com>                        #
#                                                                             #
# Modifications by:                                                           #
# ??-12-2003 Mark Wormgoor < mark@wormgoor.com>                               #
#          - Modified Makefile for IPCop build                                #
#                                                                             #
# $Id: squid 4598 2010-05-21 15:03:46Z gespinasse $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = squid
VER        = 2.7.STABLE9
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = $(PKG_NAME)-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = http://www.squid-cache.org/Versions/v2/2.7
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)
ERRMSG_DIR = /usr/lib/squid/errors

# Used to include same timestamp for all
# This is the release date
DATESTAMP = "Mar 10 2010"
TIMESTAMP = "00:40:07"

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

objects = $(DL_FILE)

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)

$(DL_FILE)_MD5 = 3c6642c85470b1079207d43bba25a819

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
###############################################################################


IPCOP_ERR_STYLE_CODE = <style type="text/css"><!--body{background-color:\#e0e0e0;margin-left:0px;margin-right:0px;margin-top:0px;margin-bottom:0px;margin-width:0px;margin-height:0px;font-family:verdana,sans-serif}pre{font-family:sans-serif}--></style>

IPCOP_ERR_BODY_CODE  = <!-- tag to disable the built-in %s footer --> <table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color:\#000000"> <tr><td><img src="http://%h:81/images/blacklogo.png" width="480" height="72" alt="IPCop Firewall" /></td></tr> </table> <blockquote>

IPCOP_ERR_POST_CODE  = <p></p> <hr noshade size="1px"> <small><address>Generated %T by %h (%s)</address></small> </blockquote> <p></p> </body> </html>


$(TARGET) : $(firstword $(MAKEFILE_LIST)) $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	# Use release date instead of __DATE__ and __TIME__ so everyone that compile has same md5
	cd $(DIR_APP) && sed -i -e 's|__DATE__|$(DATESTAMP)|' \
			-e 's|__TIME__|$(TIMESTAMP)|' helpers/ntlm_auth/SMB/ntlm_auth.c

	cd $(DIR_APP) && ./configure	--prefix=/usr \
					--sysconfdir=/etc/squid \
					--datadir=/usr/lib/squid \
					--libexecdir=/usr/lib/squid \
					--enable-storeio=null,aufs,coss,diskd,ufs \
					--enable-removal-policies=heap,lru \
					--enable-delay-pools \
					--enable-follow-x-forwarded-for \
					--enable-useragent-log \
					--enable-referer-log \
					--enable-arp-acl \
					--enable-http-violations \
					--enable-ident-lookups \
					--enable-auth=basic,ntlm \
					--enable-ntlm-fail-open \
					--enable-linux-netfilter \
					--enable-basic-auth-helpers=NCSA,SMB,MSNT,LDAP,squid_radius_auth \
					--enable-ntlm-auth-helpers=SMB \
					--enable-external-acl-helpers=ldap_group
	cd $(DIR_APP) && make -j $(PARALLELISM)

	rm -f /etc/squid/squid.conf
	cd $(DIR_APP) && make install

	ln -sf /var/ipcop/proxy/squid.conf /etc/squid/squid.conf

	mkdir -p /var/log/cache
	mkdir -p /var/log/squid
	touch /var/log/squid/access.log
	chown -R squid:squid /var/log/squid /var/log/cache

	# Duplicating built-in error files for IPCop modification
	rm -rf $(ERRMSG_DIR).ipcop
	cp -r $(ERRMSG_DIR) $(ERRMSG_DIR).ipcop

	# Adding IPCop design to the built-in error files
	# sed step 1)
		- replace spaces between tags with line feeds
		- remove all empty lines
		- remove the H1 header tag (if any)
	# sed step 2)
		- replace the entire STYLE tag
	# sed step 3)
		- disable built-in footer
		- insert a table with IPCop graphics after the BODY tag
	# sed step 4)
		- add a new footer and close the BODY and HTML tags
	# sed step 5)
		- replace spaces between tags with line feeds
		- remove all empty lines

	for LANGUAGE in $(ERRMSG_DIR).ipcop/* ; do \
		if test -d $${LANGUAGE} ; then \
			for ERRMSG_FILE in $${LANGUAGE}/* ; do \
				if test -f $${ERRMSG_FILE} ; then \
					sed -i -e 's/> />\n/g' -e '/^$$/d' -e 's/^<h1>.*<\/h1>//I' $${ERRMSG_FILE} ; \
					sed -i -e '/<STYLE/ c\$(IPCOP_ERR_STYLE_CODE)' $${ERRMSG_FILE} ; \
					sed -i -e '/<BODY>/ a\$(IPCOP_ERR_BODY_CODE)' $${ERRMSG_FILE} ; \
					sed -i -e '$$a\$(IPCOP_ERR_POST_CODE)' $${ERRMSG_FILE} ; \
					sed -i -e 's/> />\n/g' -e '/^$$/d' $${ERRMSG_FILE} ; \
				fi ; \
			done ; \
		fi ; \
	done

	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
