#!/bin/sh
##################################################################################################################
# Copyright 2021-2025 Open Text
#
# The only warranties for products and services of services of Open Text and its affiliates and licensors (“Open Text”) 
# are as may be set forth in the express warranty statements accompanying such products and services. Nothing
# herein should be construed as constituting an additional warranty. Open Text shall not be liable for technical
# or editorial errors or omissions contained herein. The information contained herein is subject to change without
# notice.
##################################################################################################################
#This Scrpit will be called from Yast upgrade
# An example command line 'iprint_update_apache'
#

HTTPD_CONF_DIR="/etc/opt/novell/iprint/httpd/conf"
IPRINT_G="${HTTPD_CONF_DIR}/iprint_g.conf"
IPRINT_SSL="${HTTPD_CONF_DIR}/iprint_ssl.conf"
ERR_LOG="/var/opt/novell/log/iprint_update_apache.log"
CONSTSTRING1='Allow from all'
CONSTSTRING2='Order deny,allow'
CONSTSTRING3='Order allow,deny'
CONSTSTRING4='Require all granted'

function restartApache()
{
	systemctl restart apache2 >> $ERR_LOG
	systemctl status apache2 >> $ERR_LOG
	if [ $? -ne 0 ];then
	   echo "Failed to start apache check apache log files." | tee -a $ERR_LOG
	else
		echo "Apache started successfully." | tee -a $ERR_LOG
	fi
}

function UpdateIprint_gConf()
{
	echo "Fixing Iprint_g.conf Started..." >> $ERR_LOG
	grep "${CONSTSTRING1}" ${IPRINT_G} > /dev/null
	if [ $? == 0 ];then
	echo "Modifying \"Allow from all\" to \"Require all granted\" in Iprint_g.conf ..." | tee -a $ERR_LOG
	sed -i s/"${CONSTSTRING1}"/"${CONSTSTRING4}"/ ${IPRINT_G}
	fi

	grep  "${CONSTSTRING2}"  ${IPRINT_G} > /dev/null
	if [ $? == 0 ];then
		echo "Removing \"Order deny, allow\" in Iprint_g.conf ..." | tee -a $ERR_LOG
		sed -i /"${CONSTSTRING2}"/d ${IPRINT_G}
	fi

	grep  "${CONSTSTRING3}"  ${IPRINT_G} > /dev/null
	if [ $? == 0 ];then
		echo "Removing \"Order allow, deny\" in Iprint_g.conf ..." | tee -a $ERR_LOG
		sed -i /"${CONSTSTRING3}"/d ${IPRINT_G}
	fi
	echo "Fixing Iprint_g.conf Ended..." | tee -a $ERR_LOG
}

function UpdateIprint_sslConf()
{
	echo "Fixing Iprint_ssl.conf started..." | tee -a $ERR_LOG
	grep  "${CONSTSTRING2}"  ${IPRINT_SSL} > /dev/null
	if [ $? == 0 ];then
		echo "Removing \"Order deny,allow\" in Iprint_ssl.conf ..." | tee -a $ERR_LOG
		sed -i /"${CONSTSTRING2}"/d ${IPRINT_SSL}
	fi
	grep  "${CONSTSTRING1}"  ${IPRINT_SSL} > /dev/null
	if [ $? == 0 ];then
		echo "Removing \"Allow from all\" in Iprint_ssl.conf ..." | tee -a $ERR_LOG
		sed -i /"${CONSTSTRING1}"/d ${IPRINT_SSL}
	fi
	echo "Fixing Iprint_ssl.conf Ended..." | tee -a $ERR_LOG
}

InitLog()
{
    echo "Log file path: $ERR_LOG"
    echo " " >> $ERR_LOG
    echo "=============== "$(date)" ===============" | tee -a $ERR_LOG
}

echo "Execution of iprint_update_apache Started" | tee -a $ERR_LOG
#Init Log file
InitLog

#This upgrade is from Apache 2.2 to apache 2.4
#Modify iprint_ssl.conf
echo "Modifying iprint_ssl.conf for Apache 2.2 upgrade apache 2.4" | tee -a $ERR_LOG
UpdateIprint_sslConf
#Modify iprint_g.conf
echo "Modifying  iprint_g.conf for Apache 2.2 upgrade apache 2.4" | tee -a $ERR_LOG
UpdateIprint_gConf
#Restarting apapche
echo "Restarting apache" | tee -a $ERR_LOG
restartApache
echo "Execution of iprint_update_apache Ended" | tee -a $ERR_LOG