#!/bin/sh
#
# metadata_begin
# recipe: Tomcat
# tags: centos9,debian11,debian12,ubuntu2004,ubuntu2204,ubuntu2404,alma8,alma9,oracle8,oracle9,rocky8,rocky9
# revision: 9
# description_ru: Tomcat сервер. Запущен на порту 8080. Если виртуализация не openvz, то также доступен админ-интерфейс с паролем рута.
# description_en: Tomcat server. Listenin on 8080. Admin interface avalible if not openvz. Password same as root password.
# metadata_end
#
RNAME=Tomcat

set -x

LOG_PIPE=/tmp/log.pipe.$$
mkfifo ${LOG_PIPE}
LOG_FILE=/root/${RNAME}.log
touch ${LOG_FILE}
chmod 600 ${LOG_FILE}

tee < ${LOG_PIPE} ${LOG_FILE} &

exec > ${LOG_PIPE}
exec 2> ${LOG_PIPE}

killjobs() {
	test -n "$(jobs -p)" && kill "$(jobs -p)" || :
}
trap killjobs INT TERM EXIT

echo
echo "=== Recipe ${RNAME} started at $(date) ==="
echo

if [ -f /etc/redhat-release ]; then
	OSNAME=centos
else
	OSNAME=debian
fi

Service() {
	# $1 - name
	# $2 - command

	if [ -f /usr/bin/systemctl ]; then
		systemctl ${2} ${1}.service
	elif [ -f /lib/systemd/systemd ]; then
		systemctl ${2} ${1}.service
	else
		if [ "${2}" = "enable" ]; then
			if [ "${OSNAME}" = "debian" ]; then
				update-rc.d ${1} enable
			else
				chkconfig ${1} on
			fi
		else
			service ${1} ${2}
		fi
	fi
}

if [ "${OSNAME}" = "debian" ]; then
	export DEBIAN_FRONTEND="noninteractive"

	# Wait firstrun script
	while ps uxaww | grep  -v grep | grep -Eq 'apt-get|dpkg' ; do echo "waiting..." ; sleep 3 ; done
	apt-get update --allow-releaseinfo-change || :
	apt-get update
	which lsb-release 2>/dev/null || apt-get -y install lsb-release
    RELEASE=$(lsb_release -s -c)
	if  [ "${RELEASE}" = "bookworm" ] || [ "${RELEASE}" = "noble" ]; then
		pkglist="tomcat10 tomcat10-admin tomcat10-examples tomcat10-user"
	    servicename=tomcat10
	else
		pkglist="tomcat9 tomcat9-admin tomcat9-examples tomcat9-user"
	    servicename=tomcat9
	fi
	apt-get -y install vim ${pkglist}

else
	OSREL=$(rpm -qf --qf '%{version}' /etc/redhat-release | cut -d . -f 1)
	yum -y update
	# Setting proxy
	# shellcheck disable=SC2154
	if [ ! "($HTTPPROXYv4)" = "()" ]; then
		# Стрипаем пробелы, если они есть
		PR="($HTTPPROXYv4)"
		PR=$(echo ${PR} | sed "s/''//g" | sed 's/""//g')
		if [ -n "${PR}" ]; then
			echo "proxy=${PR}" >> /etc/yum.conf
		fi
	fi

	if [ "$OSREL" = "8" ]; then
		yum -y install epel-release || yum -y install oracle-epel-release-el8
	else
		yum -y install epel-release || yum -y install oracle-epel-release-el9
	fi

    pkglist="vim java-17-openjdk tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp"

	yum -y install ${pkglist} || yum -y install ${pkglist} || yum -y install ${pkglist}
	# Removing proxy
	sed -r -i "/proxy=/d" /etc/yum.conf

	servicename=tomcat
fi

# Setting admin password if macro exists
_tmppass="($PASS)"
if [ -n "${_tmppass}" ] && [ "${_tmppass}" != "()" ]; then
	if [ -f /etc/tomcat/tomcat-users.xml ]; then
		usersxml=/etc/tomcat/tomcat-users.xml
	elif [ -f /etc/tomcat9/tomcat-users.xml ]; then
		usersxml=/etc/tomcat9/tomcat-users.xml
	elif [ -f /etc/tomcat9/server.xml ]; then
		usersxml=/etc/tomcat9/server.xml
	elif [ -f /usr/share/tomcat/conf/tomcat-users.xml ]; then
		usersxml=/usr/share/tomcat/conf/tomcat-users.xml
	else
		usersxml=/etc/tomcat/server.xml
	fi
	sed -i -r "/<\/tomcat-users>/i <user name=\"admin\" password=\"${_tmppass}\" roles=\"manager-gui,admin-gui\"\/>" ${usersxml}
fi

Service ${servicename} restart
Service ${servicename} enable

if [ "${OSNAME}" = "centos" ]; then
	if [ -n "$(which firewall-cmd)" ] && Service firewalld status ; then
		firewall-cmd --add-port=8080/tcp
		firewall-cmd --add-port=8080/tcp --permanent
	fi
fi
