#!/bin/bash

REPO=http://pub.3gteam.hu/chr-httpd

CHRUD=/chroot-upgrade

if ! [ -d "${CHRUD}" ]; then
	mkdir ${CHRUD}
fi

HTCHRDIRS=`find /chroot/ -mindepth 1 -maxdepth 1 -type d -name 'httpd*' |sort |grep -v 'httpd52' |grep -v 'httpd53' |grep -v 'httpd54' | grep -v 'httpd$' | grep -v 'httpd-old'`

for htchrdir in $HTCHRDIRS; do
	ht=`basename $htchrdir`
	phpv=${ht/httpd/}
	daemon=apache2-chroot-${phpv}

	if [ -f /chroot/${ht}/etc/debian_version ]; then
		orig_debver=`cat /chroot/${ht}/etc/debian_version | sed 's/\..*//'`
		case $orig_debver in
			13)
				orig_deb=trixie
				;;
			12)
				orig_deb=bookworm
				;;
			11)
				orig_deb=bullseye
				;;
		esac

		if [ "$orig_debver" == "11" -a -f /chroot/${ht}/etc/apt/sources.list.d/php.list -o "$orig_debver" == "12" -o "$orig_debver" == "13" ]; then

			echo "*** $ht - $orig_deb ($orig_debver} ***"

			if [ -f /chroot/${ht}/etc/apt/sources.list.d/php.list ]; then
				echo "sury distro found"
				if ! [ -f /chroot/${ht}/etc/apt/keyrings/php.gpg ]; then
					wget --no-check-certificate -O /chroot/${ht}/etc/apt/keyrings/php.gpg https://packages.sury.org/php/apt.gpg
					echo "deb [signed-by=/etc/apt/keyrings/php.gpg] https://packages.sury.org/php/ ${orig_deb} main" > /chroot/${ht}/etc/apt/sources.list.d/php.list
				fi
			else
				echo "debian native distro found"
			fi

			if [ "$orig_debver" == "11" ]; then
				echo "deb http://archive.debian.org/debian/ bullseye main contrib non-free"					> /chroot/${ht}/etc/apt/sources.list
				echo "# deb-src http://archive.debian.org/debian/ bullseye main contrib non-free"				>> /chroot/${ht}/etc/apt/sources.list
				echo														>> /chroot/${ht}/etc/apt/sources.list
				echo "deb http://archive.debian.org/debian-security bullseye-security main contrib non-free"			>> /chroot/${ht}/etc/apt/sources.list
				echo "# deb-src http://archive.debian.org/debian-security bullseye-security main contrib non-free"		>> /chroot/${ht}/etc/apt/sources.list
				echo														>> /chroot/${ht}/etc/apt/sources.list
				echo "# bullseye-updates"											>> /chroot/${ht}/etc/apt/sources.list
				echo "deb http://archive.debian.org/debian/ bullseye-updates main contrib non-free"				>> /chroot/${ht}/etc/apt/sources.list
				echo "# deb-src http://archive.debian.org/debian/ bullseye-updates main contrib non-free"			>> /chroot/${ht}/etc/apt/sources.list
			fi

			chroot /chroot/${ht} apt-get update
			#chroot /chroot/${ht} apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --with-new-pkgs upgrade
			chroot /chroot/${ht} apt-get -y --with-new-pkgs upgrade

			echo "Restarting ${daemon}"

			case "$(readlink -f /proc/1/exe)" in
				*systemd*)
					systemctl restart ${daemon}
					;;
				*)
					/etc/init.d/${daemon} restart
					;;
			esac

		else
			echo "*** replace - $ht - $orig_deb ($orig_debver} - replace ***"
			if ! [ -d ${CHRUD}/${ht} ]; then
				echo "Getting $ht"
				wget -O ${CHRUD}/${ht}.tar.gz ${REPO}/${ht}.tar.gz
				cd ${CHRUD}
				tar -xzvf ${ht}.tar.gz
				rm ${ht}.tar.gz

				cat ${CHRUD}/${ht}/etc/debian_version

				cp /chroot/${ht}/etc/hostname ${CHRUD}/${ht}/etc/hostname
				cp /chroot/${ht}/etc/hosts ${CHRUD}/${ht}/etc/hosts
				cp /chroot/${ht}/etc/resolv.conf ${CHRUD}/${ht}/etc/resolv.conf

				cp /chroot/${ht}/etc/cprot-captcha/config.php ${CHRUD}/${ht}/etc/cprot-captcha/config.php

				mv ${CHRUD}/${ht}/etc/apache2 ${CHRUD}/${ht}/etc/apache2-sbln
				cp -r /chroot/${ht}/etc/apache2 ${CHRUD}/${ht}/etc/apache2

			else
				echo "${CHRUD}/${ht} already exists"
			fi
		fi
	else
		echo "ERROR: /chroot/${ht} is not a Debian chroot!"
		exit 99
	fi
done

UHTCHRDIRS=`find ${CHRUD}/ -mindepth 1 -maxdepth 1 -type d -name 'httpd*' |sort`

for uhtchrdir in $UHTCHRDIRS; do
	ht=`basename $uhtchrdir`
	echo "*** $ht ***"
	if [ -d ${CHRUD}/${ht}/etc/apache2 ]; then

		echo "$ht replace to realpath_turbo.open_basedir"
		#grep basedir ${CHRUD}/${ht}/etc/apache2/* -R |grep -v 'realpath_turbo.open_basedir'

		find ${CHRUD}/${ht}/etc/apache2/ -type f -exec sed -i 's/php_admin_value open_basedir/php_admin_value realpath_turbo.open_basedir/g' {} \;
		find ${CHRUD}/${ht}/etc/apache2/ -type f -exec sed -i 's/php_admin_value realpath_cache_basedir/php_admin_value realpath_turbo.open_basedir/g' {} \;

	fi
done


exit 0
