###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2019  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# 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         = 4.0.9
VER_DB      = 4.0-$(DATE)
VER_FILTERS = 4.0.17
DATE        = 20131023

THISAPP    = foomatic-$(VER)
DL_FILE    = foomatic-filters-$(VER_FILTERS).tar.gz
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/foomatic-filters-$(VER_FILTERS)
TARGET     = $(DIR_INFO)/$(THISAPP)
PROG       = foomatic
PAK_VER    = 4

DEPS       = "cups ghostscript libtiff hplip"

export LIB_CUPS=/usr/lib/cups
export CUPS_BACKENDS=/usr/lib/cups/backends
export CUPS_FILTERS=/usr/lib/cups/filters
export CUPS_PPDS=/usr/share/cups/model

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

objects = $(DL_FILE) \
	foomatic-db-$(VER_DB).tar.gz \
	foomatic-db-engine-$(VER).tar.gz

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)
foomatic-db-$(VER_DB).tar.gz = $(DL_FROM)/foomatic-db-$(VER_DB).tar.gz
foomatic-db-engine-$(VER).tar.gz = $(DL_FROM)/foomatic-db-engine-$(VER).tar.gz

$(DL_FILE)_MD5 = b05f5dcbfe359f198eef3df5b283d896
foomatic-db-$(VER_DB).tar.gz_MD5 = ffe38b354cb06a1f3d5e3b01b7e58b0e
foomatic-db-engine-$(VER).tar.gz_MD5 = 5b33c1adb21e7b6f3652e9a18d0e6f4e

install : $(TARGET)

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

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

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

dist: 
	$(PAK)

###############################################################################
# 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)
	@rm -rf $(DIR_SRC)/foomatic-* && cd $(DIR_SRC) && \
		tar zxf $(DIR_DL)/$(DL_FILE) && \
		tar zxf $(DIR_DL)/foomatic-db-$(VER_DB).tar.gz && \
		tar zxf $(DIR_DL)/foomatic-db-engine-$(VER).tar.gz
	cd $(DIR_APP) && ./configure --prefix=/usr --sysconfdir=/etc
	cd $(DIR_APP) && make $(MAKETUNING)
	cd $(DIR_APP) && make install
	cd $(DIR_SRC)/foomatic-db-$(DATE) && ./configure --prefix=/usr --sysconfdir=/etc
	cd $(DIR_SRC)/foomatic-db-$(DATE) && make $(MAKETUNING)
	cd $(DIR_SRC)/foomatic-db-$(DATE) && make install
	cd $(DIR_SRC)/foomatic-db-engine-$(VER) && ./configure --prefix=/usr --sysconfdir=/etc
	cd $(DIR_SRC)/foomatic-db-engine-$(VER) && make $(MAKETUNING)
	cd $(DIR_SRC)/foomatic-db-engine-$(VER) && make install

	# Remove unwanted files
	rm -rfv /usr/lib/ppr \
		/etc/foomatic/filter.conf.sample \
		/usr/share/foomatic/templates

	-mkdir -pv /var/cache/foomatic

	-mkdir -pv /etc/foomatic
	echo "cups" > /etc/foomatic/defaultspooler

	-mkdir -pv /usr/lib/cups/filter
	ln -svf ../../../bin/foomatic-rip /usr/lib/cups/filter/foomatic-rip
	ln -svf ../../../bin/foomatic-ppdfile /usr/lib/cups/filter/foomatic-ppdfile

	@rm -rf $(DIR_SRC)/foomatic-*
	@$(POSTBUILD)
