#!/bin/sh

# Source debconf library.
. /usr/share/debconf/confmodule




#DebianVersionSpecific
# if [ $(grep -c 'Debian GNU/Linux 11' /etc/issue) -gt 0 ] || [ $(grep -c 'Debian GNU/Linux 10' /etc/issue) -gt 0 ] || [ $(grep -c 'Debian GNU/Linux 9' /etc/issue) -gt 0 ] || [ $(grep -c 'Ubuntu 18.04' /etc/issue) -gt 0 ] || [ $(grep -c 'Linux Mint' /etc/issue) -gt 0 ][ $(grep -c 'Ubuntu 20.04' /etc/issue) -gt 0 ] || [ $(grep -c 'Ubuntu 22.04' /etc/issue) -gt 0 ] || [ $(grep -c Jammy /etc/issue) -gt 0 ]
# then

	# Make a directory for the sysVinit scripts that should not be found by the faulty systemd compatibility layer
	mkdir -p /etc/initd

	# Make the directory for symlinking the .service file to get started at the right "runlevel"
	mkdir -p /etc/systemd/system/multi-user.target.wants


	for service in m23fetchjob
	do
		# systemd .service file name
		serviceFile="/etc/systemd/system/${service}systemd.service"

		# Move the sysVinit script so that systemd's compatibility layer will not find them
		mv /etc/init.d/$service /etc/initd/

	echo "[Unit]
SourcePath=/etc/initd/$service
Description=LSB: Fetches jobs from the m23 server
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=forking
Restart=no
TimeoutSec=10min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
#RemainAfterExit=yes
RemainAfterExit=no
#SuccessExitStatus=5 6
ExecStart=/etc/initd/$service start
ExecStop=/etc/initd/$service stop

[Install]
WantedBy=multi-user.target" > $serviceFile

	# Link the .service file to the right "runlevel" directory
	ln -s $serviceFile /etc/systemd/system/multi-user.target.wants

	done
# fi




# if [ $(grep -c 'Debian GNU/Linux 9' /etc/issue) -gt 0 ]
# then
# 	# Convert the sysVinit scripts to systemd .service files stored in /tmp
# 	/lib/systemd/system-generators/systemd-sysv-generator
# 
# 	# Make a directory for the sysVinit scripts that should not be found by the faulty systemd compatibility layer
# 	mkdir -p /etc/initd
# 	
# 	# Use a special version for xenial
# # 	mv /tmp/m23-xorg-configurator-xenial /etc/init.d/m23-xorg-configurator
# 
# #m23-xorg-configurator
# 	# Run thru the sysVinit scripts that should be converted
# 	for service in m23fetchjob
# 	do
# 		# systemd .service file name
# 		serviceFile="/tmp/$service.service"
# 
# 		# If there is no .service file generated => skip the further processing
# 		if [ ! -f $serviceFile ]
# 		then
# 			continue
# 		fi
# 
# 		# Move the sysVinit script so that systemd's compatibility layer will not find them
# 		mv /etc/init.d/$service /etc/initd/
# 
# 		# Adjust the timeout and the path to the sysVinit script
# 		sed -i -e 's/5min/10min/g' -e 's/init\.d/initd/g' $serviceFile
# 
# 		# Add the missing lines to make the .service file accepted by systemd
# echo "
# [Install]
# WantedBy=multi-user.target" >> $serviceFile
# 
# 		# Enable the .service file
# 		mv $serviceFile /etc/systemd/system
# 		systemctl daemon-reload
# 		systemctl enable $service
# 	done
# fi

db_stop
