#!/bin/bash
#set -x
PRG_Version="2022.11.15-01"
SCRIPT_Name="lmz-opsi-patch-00014"
# DEBUG 
#SCRIPT_DEBUG="false"
SCRIPT_DEBUG="true"
### ***************************************
###   Wichtige Operationen ganz am Anfang
### ***************************************
# Kommandozeilenparameter sichern
ARGS_ARRAY=("$@")
ARGS_NUM_ITEMS="$#"
# Zeile 15 - ab hier Bibliothek einbinden oder Code kopieren
lmz_bib_file="/etc/paedML/base/bin/lmz_funktionsbibliothek.sh"
source "${lmz_bib_file}" &>/dev/null
if [ $? -ne 0 ]; then
	printf "Es ist ein Fehler aufgetreten. Das Skript kann nicht ausgefuehrt werden.\n" 
	printf "Fehlernummer: 99990003\n"
	printf "Fehlertext:   Funktionsbibliothek konnte nicht geladen werden.\n"
	printf "Details:      \n"
	printf "  Dateiname:  '"${lmz_bib_file}"'\n"
	printf "  Skript:     '"${SCRIPT_Name}"'\n"
	printf "  Version:    '"${PRG_Version}"'\n"
	exit 14
fi
#
#
# Zeile 30 - ab hier fängt das eigentliche Patch-Programm an...

### PATCH *** Anfang
PATCH_Nummer="00014"
PATCH_VERSION="$PRG_Version"
PATCH_Titel="Automatisches Kopieren des opsi-CA-Zertifikats auf den Server SP01 (nur opsi 4.2)."
PATCH_DESCRIPTION="Es werden zwei Systemd-Units hinzugefügt:\n- opsi-copy-opsi-ca-certificate-to-sp01.service und \n- opsi-copy-opsi-ca-certificate-to-sp01.timer\nEventuell vorhandene Units werden ausgetauscht.\nStartzeit: 02:28 Uhr nachts."
### PATCH *** Ende

# Aufruf
# -a apply      Rückgabewert 0 = OK, 1 = Fehler
# -c chech      Rückgabewert 0 = OK, 1 = needs to be installed

do_install() {
	local install_result
	local command_output
	local command_output2
	local retval
	local file_line
	install_result=0
	echo "Installiere '${SCRIPT_Name}' (${PRG_Version})..."
	schreibe_log "Installiere '${SCRIPT_Name}' (${PRG_Version})..."
	# -a apply      Rückgabewert 0 = OK, 1 = Fehler
	if [ -e /usr/bin/opsi-python ]; then
	# Python 3 - opsi 4.2 or later
		# 001 /etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer
		if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer" ]; then
			systemctl stop --quiet opsi-copy-opsi-ca-certificate-to-sp01.timer
			command_output=$(systemctl disable --quiet opsi-copy-opsi-ca-certificate-to-sp01.timer)
			retval=$?
			if [[ $retval -ne 0 ]]; then
				# Fehler
				schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' konnte nicht deaktiviert werden."
				schreibe_log "Fehlercode: ${retval}"
				schreibe_log "Fehlertext: ${command_output}"
				install_result=1
			else
				schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' wurde deaktiviert."
				# Datei löschen
				sudo rm -f "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer"
				if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer" ]; then
					# Fehler
					schreibe_log "Systemd-Unit-Datei 'opsi-copy-opsi-ca-certificate-to-sp01.timer' konnte nicht gelöscht werden."
				else
					schreibe_log "Systemd-Unit-Datei 'opsi-copy-opsi-ca-certificate-to-sp01.timer' wurde gelöscht."
				fi
			fi
		else
			schreibe_log "Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer' ist nicht (mehr) vorhanden."
		fi
		# 002 /etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service
		if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service" ]; then
			systemctl stop --quiet opsi-copy-opsi-ca-certificate-to-sp01.service
			command_output=$(systemctl disable --quiet opsi-copy-opsi-ca-certificate-to-sp01.service)
			retval=$?
			if [[ $retval -ne 0 ]]; then
				# Fehler
				schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01' konnte nicht deaktiviert werden."
				schreibe_log "Fehlercode: ${retval}"
				schreibe_log "Fehlertext: ${command_output}"
				install_result=1
			else
				schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.service' wurde deaktiviert."
				# Datei löschen
				sudo rm -f "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service"
				if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service" ]; then
					# Fehler
					schreibe_log "Systemd-Unit-Datei 'opsi-copy-opsi-ca-certificate-to-sp01.service' konnte nicht gelöscht werden."
				else
					schreibe_log "Systemd-Unit-Datei 'opsi-copy-opsi-ca-certificate-to-sp01.service' wurde gelöscht."
				fi
			fi
		else
			schreibe_log "Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service' ist nicht (mehr) vorhanden."
		fi
		# 003 opsi-copy-opsi-ca-certificate-to-sp01.service hinzufügen
		sudo cp -f "/etc/paedML/patches/bin/files/00014/opsi-copy-opsi-ca-certificate-to-sp01.service" "/etc/systemd/system"
		sudo chown root:root "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service"
		sudo chmod 644 "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service"
		if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service" ]; then
			schreibe_log "Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service' wurde neu angelegt."
		else
			schreibe_log "Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service' konnte nicht neu angelegt werden."
		fi
		# 004 opsi-copy-opsi-ca-certificate-to-sp01.timer hinzufügen
		sudo cp -f "/etc/paedML/patches/bin/files/00014/opsi-copy-opsi-ca-certificate-to-sp01.timer" "/etc/systemd/system"
		sudo chown root:root "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer"
		sudo chmod 644 "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer"
		if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer" ]; then
			schreibe_log "Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer' wurde neu angelegt."
		else
			schreibe_log "Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer' konnte nicht neu angelegt werden."
		fi
		# 005 Systemd-Unit aktivieren
		systemctl daemon-reload
		command_output=$(systemctl enable opsi-copy-opsi-ca-certificate-to-sp01.timer)
		retval=$?
		if [[ $retval -ne 0 ]]; then
			# Fehler
			schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' konnte nicht aktiviert werden."
			schreibe_log "Fehlercode: ${retval}"
			schreibe_log "Fehlertext: ${command_output}"
			install_result=1
		else
			schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' wurde aktiviert."
		fi
		# 006 Systemd-Unit starten
		command_output=$(systemctl start opsi-copy-opsi-ca-certificate-to-sp01.timer)
		retval=$?
		if [[ $retval -ne 0 ]]; then
			# Fehler
			schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' konnte nicht gestartet werden."
			schreibe_log "Fehlercode: ${retval}"
			schreibe_log "Fehlertext: ${command_output}"
			install_result=1
		else
			schreibe_log "Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' wurde gestartet."
		fi
	else
		# opsi 4.1
		schreibe "${GREEN}Fazit:${NC} Der Patch '${SCRIPT_Name}' wird auf opsi 4.1 nicht benoetigt.\n"
		return 0
	fi
	# 007 fertig
	if [[ "${install_result}" == "0" ]]; then
		return 0
	else
		return 1
	fi
}

do_check() {
	local check_result
	local retval
	check_result="0"
	echo "Pruefe '${SCRIPT_Name}' (${PRG_Version})..."
	schreibe_log "Pruefe '${SCRIPT_Name}' (${PRG_Version})..."
	# -c check      Rückgabewert 0 = OK, 1 = needs to be installed
	if [ -e /usr/bin/opsi-python ]; then
		# Python 3 - opsi 4.2 or later
		# 001 /etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer
		if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer" ]; then
			# Dateien vergleichen
			cmp --silent "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer" "/etc/paedML/patches/bin/files/00014/opsi-copy-opsi-ca-certificate-to-sp01.timer"
			retval=$?
			if [[ $retval -ne 0 ]]; then
				schreibe "NTBI: Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer' ist nicht mit der Vorlage identisch. Sie muss ausgetauscht werden.\n"
				check_result="1"
			else
				schreibe "OK: Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer' ist mit der Vorlage identisch.\n"
			fi
		else
			schreibe "NTBI: Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.timer' fehlt.\n"
			check_result="1"
		fi
		# 002 /etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service
		if [ -e "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service" ]; then
			# Dateien vergleichen
			cmp --silent "/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service" "/etc/paedML/patches/bin/files/00014/opsi-copy-opsi-ca-certificate-to-sp01.service"
			retval=$?
			if [[ $retval -ne 0 ]]; then
				schreibe "NTBI: Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service' ist nicht mit der Vorlage identisch. Sie muss ausgetauscht werden.\n"
				check_result="1"
			else
				schreibe "OK: Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service' ist mit der Vorlage identisch.\n"
			fi
		else
			schreibe "NTBI: Datei '/etc/systemd/system/opsi-copy-opsi-ca-certificate-to-sp01.service' fehlt.\n"
			check_result="1"
		fi
		# 003 systemctl is-active --quiet opsi-copy-opsi-ca-certificate-to-sp01.timer
			systemctl is-active --quiet opsi-copy-opsi-ca-certificate-to-sp01.timer
			retval=$?
			if [[ $retval -ne 0 ]]; then
				schreibe "NTBI: Der Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' ist nicht aktiv.\n"
				check_result="1"
			else
				schreibe "OK: Der Systemd-Unit 'opsi-copy-opsi-ca-certificate-to-sp01.timer' ist aktiv.\n"
		fi
	else
		# opsi 4.1
		schreibe "${GREEN}Fazit:${NC} Der Patch '${SCRIPT_Name}' wird auf opsi 4.1 nicht benoetigt.\n"
		return 0
	fi	
	# fertig
	if [[ "${check_result}" == "0" ]]; then
		schreibe "${GREEN}Fazit:${NC} Der Patch '${SCRIPT_Name}' wird nicht benoetigt bzw. wurde bereits installiert.\n"
		return 0
	else
		schreibe "${YELLOW}Fazit:${NC} Der Patch '${SCRIPT_Name}' sollte installiert werden.\n"
		return 1
	fi	
}

parseParameters() {
# Analyse der Kommandozeilenparameter
# Quelle: https://dzone.com/articles/bash-snippet-parsing-command-line-arguments-and-as
#         angepasst.
	local key
	local temp_pds
	local retval
	if [[ $# -eq 1 ]]; then
		# Argument(e) vorhanden
		key="${1}"
		#echo $key
		case "${key}" in
		-a|--apply)
			do_install
			retval=$?
			exit $retval
			;;
		-c|--check)
			do_check
			retval=$?
			exit $retval
			;;
		-h|--help)
			echo "lmz-opsi-patch: '${PATCH_Nummer}'  (Version ${PRG_Version})."		
			echo "Kommandos:"
			echo "  -a, --apply                               Diesen Patch manuell installieren."
			echo "  -c, --check                               Diesen Patch manuell überprüfen."
			echo "  -i, --info                                Gibt Informationen aus."
			echo ""
			echo "Bitte wenden Sie das Tool 'lmz-opsi-patches' an, um Patches zu installieren."
			echo " lmz-opsi-patches --apply '${PATCH_Nummer}'           ...installiert diesen Patch."
			echo " lmz-opsi-patches --info '${PATCH_Nummer}'            ...gibt weitere Informationen"
			echo "                                               zu diesem Patch aus."
			echo ""
			echo ""
			;;
		-i|--info)
			printf "Patchnummer:  ${PATCH_Nummer}\n"
			printf "Titel:        ${PATCH_Titel}\n"
			printf "Version:      ${PRG_Version}\n"
			# description
			temp_pds="${PATCH_DESCRIPTION//\"}"
			temp_pds="${temp_pds//\\n/\\n              }"
			printf "Beschreibung: ${temp_pds}\n"
			;;
		*) # unknown option
			echo "No valid parameters given."
			exit 55
			;;
		esac
	else
		# keine Argumente uebergeben
		echo "No parameters given."
		exit 55 
	fi
}

# MAIN

parseParameters $*

exit 0


