#!/bin/sh
set -e

USER_NAME=speech-dispatcher
HOME_DIR=/var/run/speech-dispatcher

if ! id -u $USER_NAME >/dev/null 2>&1; then
  adduser --quiet --system --ingroup audio \
          --home $HOME_DIR \
          --shell /bin/false --disabled-login  \
	  --gecos 'Speech Dispatcher' $USER_NAME
elif ! test -d $HOME_DIR; then
  if test -d /var/run/speechd; then
    mv /var/run/speechd $HOME_DIR
  else
    mkdir $HOME_DIR
  fi
  usermod --shell /bin/false $USER_NAME
fi

if [ -d /var/log/speech-dispatcher ]; then
  chown $USER_NAME /var/log/speech-dispatcher
fi

# Automatically added by dh_systemd_enable/11.3.5
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if deb-systemd-helper debian-installed 'speech-dispatcherd.service'; then
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'speech-dispatcherd.service' >/dev/null || true

		if deb-systemd-helper --quiet was-enabled 'speech-dispatcherd.service'; then
			# Create new symlinks, if any.
			deb-systemd-helper enable 'speech-dispatcherd.service' >/dev/null || true
		fi
	fi

	# Update the statefile to add new symlinks (if any), which need to be cleaned
	# up on purge. Also remove old symlinks.
	deb-systemd-helper update-state 'speech-dispatcherd.service' >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit/11.3.5
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# In case this system is running systemd, we need to ensure that all
	# necessary tmpfiles (if any) are created before starting.
	if [ -d /run/systemd/system ] ; then
		systemd-tmpfiles --create speech-dispatcher.conf >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installdeb/11.3.5
dpkg-maintscript-helper rm_conffile /etc/speech-dispatcher/clients/gnome-speech.conf 0.8.6-2\~ speech-dispatcher -- "$@"
# End automatically added section
# Automatically added by dh_installinit/11.3.5
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -x "/etc/init.d/speech-dispatcher" ]; then
		update-rc.d speech-dispatcher defaults-disabled >/dev/null || true
	fi
fi
# End automatically added section


# Versions earlier than this only had an init script whose startup was
# controlled by the default file
if [ "$1" = configure ] && dpkg --compare-versions "$2" le 0.8.8-4 ; then
  RUN=no

  [ ! -f /etc/default/speech-dispatcher.dpkg-remove ] || . /etc/default/speech-dispatcher.dpkg-remove
  [ ! -f /etc/default/speech-dispatcher.dpkg-backup ] || . /etc/default/speech-dispatcher.dpkg-backup

  if [ "$RUN" = yes ]; then
    update-rc.d speech-dispatcher enable
    [ "$(uname -s)" != Linux ] || deb-systemd-helper enable speech-dispatcherd.service
  else
    update-rc.d speech-dispatcher disable
    [ "$(uname -s)" != Linux ] || deb-systemd-helper disable speech-dispatcherd.service
  fi

fi

dpkg-maintscript-helper rm_conffile /etc/default/speech-dispatcher 0.8.8-4 -- "$@"
