#!/bin/sh

set -e

SELF=$(basename $(readlink -e "$0"))

# Don't bother to restart sshd when lo is configured.
if [ "${IFACE}" = lo ]; then
	echo "${SELF}: skip loopback '\$IFACE'"
	exit 0
fi

# Only run from ifup.
if [ "${AdministrativeState}" != 'configuring' -a "${AdministrativeState}" != 'configured' ]; then
	echo "${SELF}: AdministrativeState is '${AdministrativeState}', exit"
	exit 0
fi

# Is /usr mounted?
if [ ! -e /usr/sbin/sshd ]; then
	echo "${SELF}: '/usr/sbin/sshd' does not exists, exit"
	exit 0
fi

if [ ! -f /run/sshd.pid ] || \
   [ "$(ps -p "$(cat /run/sshd.pid)" -o comm=)" != sshd ]; then
	echo "${SELF}: SSH daemon is not running, exit"
	exit 0
fi

# We'd like to use 'reload' here, but it has some problems; see #502444.  On
# the other hand, repeated restarts of ssh make systemd unhappy
# (#756547/#757822), so use reload in that case.
if [ -d /run/systemd/system ]; then
	echo "${SELF}: reload OpenSSH service"
	systemctl reload --no-block ssh.service >/dev/null 2>&1 || true
else
	echo "${SELF}: restart OpenSSH service"
	invoke-rc.d ssh restart >/dev/null 2>&1 || true
fi

exit 0

