###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2024  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        = 6.3.0

THISAPP    = knot-resolver-$(VER)
DL_FILE    = $(THISAPP).tar.xz
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(THISAPP)

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

objects = $(DL_FILE)

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

$(DL_FILE)_BLAKE2 = 5745be9d031d58e7a9175d29307fb7453b76a6ef65a3b9970b9e5c256a38b2f13e5f4eb5bd126c44e4263b5dbebef2f5addf9c495c80e4a8d4ebe5c2169c0a9c

install : $(TARGET)

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

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

b2 : $(subst %,%_BLAKE2,$(objects))

###############################################################################
# Downloading, checking, b2sum
###############################################################################

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

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

$(subst %,%_BLAKE2,$(objects)) :
	@$(B2SUM)

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

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar axf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/knot-resolver-6.3-policy-add-flag-to-force-TCP.patch
	cd $(DIR_APP) && meson setup \
		--prefix=/usr \
		-Duser=knot-resolver \
		-Dgroup=knot-resolver \
		-Dkeyfile_default="/var/lib/knot-resolver/root.keys" \
		-Dinstall_root_keys=enabled \
		-Dquic=disabled \
		builddir/

	# Compile kresd
	cd $(DIR_APP) && ninja -C builddir/ $(MAKETUNING)

	# Build Python components
	cd $(DIR_APP) && python3 setup.py build

	# Install everything
	cd $(DIR_APP) && ninja -C builddir/ install
	cd $(DIR_APP) && python3 setup.py install --root=/

	# Install our configuration file
	install -v -m 644 $(DIR_SRC)/config/knot-resolver/config.yaml \
		/etc/knot-resolver/config.yaml

	# Install our configuration helper script
	install -v -m 644 $(DIR_SRC)/config/knot-resolver/config.lua \
		/usr/lib/knot-resolver/config.lua

	# Install the leases module
	install -v -m 644 $(DIR_SRC)/config/knot-resolver/leases.lua \
		/usr/lib/knot-resolver/kres_modules/leases.lua

	# Install the leases schema
	install -v -m 6444 $(DIR_SRC)/config/knot-resolver/leases.schema \
		/usr/lib/knot-resolver/leases.schema

	# Create the leases database
	sqlite3 "/var/lib/knot-resolver/leases.db" \
		< /usr/lib/knot-resolver/leases.schema
	chown knot-resolver:knot-resolver /var/lib/knot-resolver/leases.db

	# Install the DHCP leases helper script
	install -v -m 755 $(DIR_SRC)/config/knot-resolver/dhcp-lease \
		/usr/sbin/dhcp-lease

	# Change the ownership of /var/lib/knot-resolver
	chown knot-resolver:knot-resolver /var/lib/knot-resolver

	# Create a directory for zone files
	-mkdir -pv /var/lib/knot-resolver/zones
	chown knot-resolver:knot-resolver /var/lib/knot-resolver/zones

	# Create cache directory
	-mkdir -pv /var/cache/knot-resolver
	chown knot-resolver:knot-resolver /var/cache/knot-resolver

	# Allow kresd to bind to ports even though it is not running as root
	setcap "cap_net_bind_service=+ep" /usr/sbin/kresd

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