1#! /bin/sh
2### BEGIN INIT INFO
3# Provides:		gpio-manager
4# Required-Start:	$remote_fs $syslog
5# Required-Stop:	$remote_fs $syslog
6# Default-Start:	2 3 4 5
7# Default-Stop:		1
8# Short-Description:	Centralized GPIO manager daemon
9### END INIT INFO
10#
11# -*- coding: utf-8 -*-
12# Debian init.d script for gpio-manager
13# Copyright (c) 2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
14
15# set -e
16
17# Source function library.
18. /etc/init.d/functions
19
20DAEMON=/usr/bin/gpio-manager
21NAME=gpio-manager
22PIDFILE=/var/run/gpio-manager/pid
23DESC="GPIO manager daemon"
24
25test -x $DAEMON || exit 0
26test "$ENABLED" != "0" || exit 0
27
28do_start()
29{
30	mkdir -p "`dirname $PIDFILE`"
31	if [ -e $PIDFILE ]; then
32		PIDDIR=/proc/$(cat $PIDFILE)
33		if [ -d ${PIDDIR} -a "$(readlink -f ${PIDDIR}/exe)" = "${DAEMON}" ]; then
34			echo "$DESC already started; not starting."
35		else
36			echo "Removing stale PID file $PIDFILE."
37			rm -f $PIDFILE
38		fi
39	fi
40
41	echo -n "Starting $DESC: "
42	start-stop-daemon --user gpio-manager --background --start --quiet --make-pidfile --pidfile $PIDFILE --exec $DAEMON
43	echo "$NAME."
44}
45
46do_stop()
47{
48	echo -n "Stopping $DESC: "
49	start-stop-daemon --stop --quiet --pidfile $PIDFILE
50	echo "$NAME."
51	rm -f $PIDFILE
52}
53
54case "$1" in
55	start)
56		do_start
57	;;
58	stop)
59		do_stop
60	;;
61	status)
62		status $DAEMON
63		exit $?
64	;;
65	restart)
66		do_stop
67		sleep 1
68		do_start
69	;;
70	*)
71		echo "Usage: /etc/init.d/$NAME {start|stop|status|restart}" >&2
72		exit 1
73	;;
74esac
75
76exit 0
77