#!/bin/bash
############################################################################
#                                                                          #
# This file is part of the IPFire Firewall.                                #
#                                                                          #
# IPFire 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.                                      #
#                                                                          #
# IPFire 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 IPFire; if not, write to the Free Software                    #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA #
#                                                                          #
# Copyright (C) 2026 IPFire-Team <info@ipfire.org>.                        #
#                                                                          #
############################################################################
#
. /opt/pakfire/lib/functions.sh
/usr/local/bin/backupctrl exclude >/dev/null 2>&1

core=203

# Remove old core updates from pakfire cache to save space...
for (( i=1; i<=$core; i++ )); do
	rm -f /var/cache/pakfire/core-upgrade-*-$i.ipfire
done

# Remove files
rm -rfv \
	/usr/lib/security/pam_cap.so

# Remove dropped packages
for package in python3-attrs python3-idna python3-typing-extensions \
		python3-yaml; do \
        if [ -e "/opt/pakfire/db/installed/meta-${package}" ]; then
                stop_service "${package}"
                for i in $(</opt/pakfire/db/rootfiles/${package}); do
                        rm -rfv "/${i}"
                done
        fi
        rm -f "/opt/pakfire/db/installed/meta-${package}"
        rm -f "/opt/pakfire/db/meta/meta-${package}"
        rm -f "/opt/pakfire/db/rootfiles/${package}"
done

# Create Knot Resolver group
if ! getent group knot-resolver &>/dev/null; then
	groupadd -g 120 knot-resolver
fi

# Create Knot Resolver user
if ! getent passwd knot-resolver &>/dev/null; then
	useradd \
		-c "Knot Resolver User" \
		-d /var/empty \
		-g knot-resolver \
		-s /bin/false \
		-u 120 \
		knot-resolver
fi

# Create /var/run/knot-resolver
if [ ! -d "/var/run/knot-resolver" ]; then
	mkdir /var/run/knot-resolver
	chown knot-resolver:knot-resolver /var/run/knot-resolver
fi

# Extract files
extract_files

# Create leases.db if it does not exist, yet
if [ ! -e "/var/lib/knot-resolver/leases.db" ]; then
	sqlite3 "/var/lib/knot-resolver/leases.db" \
		< /usr/lib/knot-resolver/leases.schema
	chown knot-resolver:knot-resolver /var/lib/knot-resolver/leases.db
fi

# Stop Unbound & start Knot Resolver
if [ -x "/etc/init.d/unbound" ]; then
	/etc/init.d/unbound stop
	/etc/init.d/localnet start
	/etc/init.d/knot-resolver start
fi

# Remove Unbound
rm -rfv \
	/etc/rc.d/init.d/unbound \
	/etc/rc.d/rc0.d/K86unbound \
	/etc/rc.d/rc3.d/S11unbound \
	/etc/rc.d/rc6.d/K86unbound \
	/etc/unbound \
	/usr/lib/libunbound.so.* \
	/usr/sbin/unbound* \
	/var/cache/unbound \
	/var/lib/unbound

# Remove boost
rm -vf \
	/usr/lib/libboost*.so.1.89.0 \
	/usr/lib/libboost*-a64.so.1.90.0 \
	/usr/lib/libboost*-x64.so.1.90.0

# update linker config
ldconfig

# Update Language cache
/usr/local/bin/update-lang-cache

# Filesytem cleanup
/usr/local/bin/filesystem-cleanup

# Apply SSH configuration
/usr/local/bin/sshctrl

# Start services
/etc/init.d/fcron restart

# Restart IPsec
if grep -q "ENABLED=on" /var/ipfire/vpn/settings; then
	/etc/init.d/ipsec restart
fi

# Restart OpenVPN
/etc/init.d/openvpn-rw restart
/etc/init.d/openvpn-n2n restart

# Migrate DHCP configuration
if [ -e "/var/ipfire/dhcp/dhcpd.conf" ]; then
	sed -e "s/unbound-dhcp-leases-client/dhcp-lease/g" \
		-i /var/ipfire/dhcp/dhcpd.conf

	# Restart DHCP
	/etc/init.d/dhcp restart
fi

# Re-fetch any RPZs in background
/usr/local/bin/update-rpzs &

# Reload crontab
fcrontab -z

# This update needs a reboot...
touch /var/run/need_reboot

# Finish
/etc/init.d/fireinfo start
sendprofile

# Build initial ramdisks
dracut --regenerate-all --force
KVER="xxxKVERxxx"
case "$(uname -m)" in
	aarch64)
		mkimage -A arm64 -T ramdisk -C lzma -d /boot/initramfs-${KVER}.img /boot/uInit-${KVER}
		# dont remove initramfs because grub need this to boot.
		;;
esac

# Update grub config to display new core version
if [ -e /boot/grub/grub.cfg ]; then
	/usr/bin/install-bootloader
fi

sync

# Don't report the exitcode last command
exit 0
