#!/bin/sh -
# Extra pre-upgrade for nosh-bundles
# vim: set filetype=sh:

# /run is now in the root.
test -e /var/run || ln -s -f -- /run /var/

# Convert from the old 1.16 and earlier directory structure.
install -d -m 0755 -- /etc/service-bundles
if test \! -L /etc/system-manager/targets && test -d /etc/system-manager/targets && test \! -e /etc/service-bundles/targets
then
	mv -i -- /etc/system-manager/targets /etc/service-bundles/
	ln -s -f -- ../service-bundles/targets /etc/system-manager
fi
if test \! -L /etc/sv && test -d /etc/sv && test \! -e /etc/service-bundles/services
then
	mv -i -- /etc/sv /etc/service-bundles/services
	ln -s -f -- service-bundles/services /etc/sv
fi
for d in / /usr/local/
do
	install -d -m 0755 -- "${d}"etc/system-control
	if test \! -L "${d}"etc/system-manager/presets && test -d "${d}"etc/system-manager/presets
	then
		mv -i -- "${d}"etc/system-manager/presets "${d}"etc/system-control/
		ln -s -f -- ../system-control/presets "${d}"etc/system-manager
	fi
done
# Convert from the old 1.40 and earlier directory structure.
if test \! -L /var/sv && test -d /var/sv && test \! -e /var/service-bundles/services
then
	install -d -m 0755 -- /var/service-bundles
	mv -i -- /var/sv /var/service-bundles/services
	ln -s -f -- service-bundles/services /var/sv
fi
if test \! -L /var/local/sv && test -d /var/local/sv && test \! -e /var/local/service-bundles/services
then
	install -d -m 0755 -- /var/local/service-bundles
	mv -i -- /var/local/sv /var/local/service-bundles/services
	ln -s -f -- service-bundles/services /var/local/sv
fi

pwescape() { system-control escape --format account -- "$@" ; }
altescape() { system-control escape --format old-alt -- "$@" ; }
case "`uname`" in
FreeBSD)	;;
*BSD)		pw() { local command="$1" account="$2" ; shift 2 ; "${command}" "$@" "${account}" ; } ;;
esac

relocate_var_service() {
        local e
	e=1
        if test -d "/var/service-bundles/services/$1" && test \! -d "/var/service-bundles/services/$2"
        then
                if system-control is-enabled "/var/service-bundles/services/$1"
		then
			e=0
			system-control disable "/var/service-bundles/services/$1"
		fi
		printf 1>&2 "Renaming service bundle directory: %s to %s\n" "$1" "$2"
                mv -i -- "/var/service-bundles/services/$1" "/var/service-bundles/services/$2"
                test $e -ne 0 || system-control enable "/var/service-bundles/services/$2"
        fi
}

discontinue_alias() {
	local I
	for I
	do
		test \! -L "/var/service-bundles/services/$I" || rm -f -- "/var/service-bundles/services/$I"
	done
}

discontinue() {
        local e
	local I
        for I
        do
                if e="`system-control find \"$I\" 2>/dev/null`"
                then
                        if system-control is-enabled "$e"
                        then
                                system-control disable "$e"
                        fi
                        if system-control is-loaded "$e"
                        then
                                system-control unload-when-stopped "$e"
                                system-control stop "$e"
                        fi
                fi
        done
}

# This relies upon stopping and starting the service under its old name.
# Always invoke this before renaming the log service bundle, therefore.
rename_log() {
	local e
	local pwescaped1="`pwescape "$1"`-log"
	local altescaped1="`altescape "$1"`-log"
	local pwescaped2="`pwescape "$2"`-log"
	if getent passwd "${altescaped1}" > /dev/null &&
	!  getent passwd "${pwescaped2}" > /dev/null
	then
		e=1
		printf 1>&2 "Renaming user: %s to %s\n" "${altescaped1}" "${pwescaped2}"
		if system-control is-loaded cyclog@"$1" &&
		   system-control is-active cyclog@"$1"
		then
			e=0
			system-control stop cyclog@"$1"
		fi
		pw usermod "${altescaped1}" -l "${pwescaped2}"
		pw groupmod "${altescaped1}" -l "${pwescaped2}"
		test $e -ne 0 || system-control start cyclog@"$1"
	fi
	if getent passwd "${pwescaped1}" > /dev/null &&
	!  getent passwd "${pwescaped2}" > /dev/null
	then
		e=1
		printf 1>&2 "Renaming user: %s to %s\n" "${pwescaped1}" "${pwescaped2}"
		if system-control is-loaded cyclog@"$1" &&
		   system-control is-active cyclog@"$1"
		then
			e=0
			system-control stop cyclog@"$1"
		fi
		pw usermod "${pwescaped1}" -l "${pwescaped2}"
		pw groupmod "${pwescaped1}" -l "${pwescaped2}"
		test $e -ne 0 || system-control start cyclog@"$1"
	fi
        if test -d "/var/log/sv/$1" && test \! -d "/var/log/sv/$2"
        then
		printf 1>&2 "Renaming log directory: %s to %s\n" "$1" "$2"
                mv -i -- "/var/log/sv/$1" "/var/log/sv/$2"
        fi
}

remove_ready_after_run() {
        local e
	local I
        for I
        do
                if e="`system-control find \"$I\" 2>/dev/null`"
                then
			find "${e}"/service -maxdepth 1 -name ready_after_run -mtime +1 -delete
                fi
        done
}

# These services have been discontinued.
discontinue \
	/var/sv/openvpn@server \
	/var/sv/openvpn@client \
        /var/sv/ldconfig \
	stty-sane@console \
	make-utx-log \
        cyclog@ip6addrctl@ipv4 \
	uhidd \
	moused \
	;

# These services are now always running
remove_ready_after_run \
	org.cups.cups-cancel \
	taiclock \
	certbot-renew \
	convert_varrun \
	phpsessionclean \
	;

# These services have been relocated.
relocate_var_service gnucron debian-cron
rename_log gnucron debian-cron	# Must run before relocate_var_service.
relocate_var_service cyclog@gnucron cyclog@debian-cron
relocate_var_service dbus dbus-daemon
rename_log dbus dbus-daemon	# Must run before relocate_var_service.
relocate_var_service cyclog@dbus cyclog@dbus-daemon
discontinue_alias cyclog@dbus dbus
relocate_var_service clamd clamav
rename_log clamd clamav	# Must run before relocate_var_service.
relocate_var_service cyclog@clamd cyclog@clamav
relocate_var_service console-fb-realizer@head0 console-kvt-realizer@ttyv8
rename_log console-fb-realizer@head0 console-kvt-realizer@ttyv8	# Must run before relocate_var_service.
relocate_var_service cyclog@console-fb-realizer@head0 cyclog@console-kvt-realizer@ttyv8
relocate_var_service cyclog@ip6addrctl@ipv6 ip6addrctl-log
rename_log ipropd_master ipropd-master	# Must run before relocate_var_service.
relocate_var_service ipropd_master ipropd-master
relocate_var_service cyclog@ipropd_master cyclog@ipropd-master
rename_log ipropd_slave ipropd-slave	# Must run before relocate_var_service.
relocate_var_service ipropd_slave ipropd-slave
relocate_var_service cyclog@ipropd_slave cyclog@ipropd-slave
