#!/bin/sh

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


# Linux Mint 13 Maya
if [ $(grep 'packages.linuxmint.com' /etc/apt/sources.list | grep -c maya) -eq 1 ]
then
	rm /etc/init/m23-xorg-configurator*
else
	# Linux Mint 17 Qiana
	if [ $(grep 'packages.linuxmint.com' /etc/apt/sources.list | grep -c qiana) -gt 0 ] || [ $(grep -c 'Xenial' /etc/issue) -gt 0 ]
	then
		# Remove sysvinit files
		find /etc/rc* | grep hwcheck$ | xargs rm 2> /dev/null
	else
		#Check, if the system uses upstart (Ubuntu)
		if [ `dpkg --get-selections | grep upstart | grep -c -v deinstall` -gt 0 ]
		then
			find /etc/rc* | grep hwcheck$ | xargs rm 2> /dev/null
			find /etc/rc* | grep m23-xorg-configurator$ | xargs rm 2> /dev/null
		fi
	fi
fi


if [ $(grep -c 'Xenial' /etc/issue) -gt 0 ]
then
	# Remove upstart files
	rm /etc/init/m23*

	find /etc/rc* | grep m23-xorg-configurator$ | xargs rm 2> /dev/null

	# Use a special version for xenial
	mv /tmp/m23-xorg-configurator-xenial /etc/init.d/m23-xorg-configurator

	# Make sure the sysVinit files are created in the correct runlevels
	update-rc.d m23-xorg-configurator defaults
	systemctl enable m23-xorg-configurator
fi







if [ $(grep -c 'Debian GNU/Linux 9' /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.service"

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

	echo "
# Generated by m23-initscripts

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

[Service]
Type=forking
Restart=no
TimeoutSec=10min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
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
