###############################################################################
# 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 8092 2016-02-21 10:19:32Z owes $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = squid
VER        = 3.4.14
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = $(PKG_NAME)-$(VER)
DL_FILE    = $(THISAPP).tar.xz
DL_FROM    = http://www.squid-cache.org/Versions/v3/3.4
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 = "Nov 28 2015"
TIMESTAMP = "16:24:46"

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

objects = $(DL_FILE)

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

$(DL_FILE)_MD5 = 4e7d7d062159484563ef11f69a0df50a

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) : $(firstword $(MAKEFILE_LIST)) $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar xf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(PKG_NAME)-3.4.4_more-disable-ipv6.patch
	# 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_lm/ntlm_smb_lm_auth.cc
	# ac_cv_func_tempnam=yes fix a compilation failure.
	# Unclear why we would be alone requiring this
	cd $(DIR_APP) && ac_cv_func_tempnam=yes ./configure \
		--prefix=/usr \
		--disable-arch-native \
		--sysconfdir=/etc/squid \
		--datadir=/usr/lib/squid \
		--libexecdir=/usr/lib/squid \
		--with-large-files \
		--enable-storeio=aufs,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-ntlm-fail-open \
		--enable-linux-netfilter \
		--enable-auth \
		--enable-auth-basic=NCSA,SMB,MSNT,LDAP,RADIUS \
		--enable-auth-ntlm=smb_lm \
		--enable-auth-negotiate=kerberos \
		--enable-external-acl-helpers=LDAP_group \
		--disable-ipv6 \
		--disable-wccp \
		--disable-wccpv2
	cd $(DIR_APP) && make -j $(PARALLELISM)

ifeq "$(RUNNING_TEST)" "yes"
	cd $(DIR_APP) && make -k check &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif

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

	ln -sf /var/ipcop/proxy/squid.conf /etc/squid/squid.conf
	ln -sf /var/ipcop/proxy/ntlm/msntauth.conf /etc/squid/msntauth.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

	@rm -rf $(DIR_APP)

	# Remove the error messages, we use language pack for those
	@rm -rf /usr/lib/squid/errors
	# Force rebuilding of language pack
	rm -f $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/squid-langpack-*

	@$(POSTBUILD)
