#!/bin/bash
#
# /etc/rc.d/init.d/seatd
#
# Seat management daemon
#
# chkconfig: 5 32 10
# description: A seat management daemon
# processname: seatd
#
### BEGIN INIT INFO
# Provides: seatd
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 5
# Short-Description: Seat management daemon
# Description: A seat management daemon that grants Wayland support
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

prog=seatd
opts="-g seat"
exec=/usr/bin/$prog
lockfile=/var/lock/subsys/$prog
pidfile=/var/run/$prog
RETVAL=0

check() {
    [ `id -u` = 0 ] || exit 4
    test -x $exec || exit 5
}

start() {
    check
    if [ ! -f $lockfile ]; then
        gprintf "Starting %s: " "$prog"
        daemon "$exec $opts" &
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
          touch $lockfile
          ps aux | grep $exec | grep -v grep | tr -s " " | cut -d " " -f2 > $pidfile
        fi
        echo
    fi
    return $RETVAL
}

stop() {
    check
    gprintf "Stopping %s: " "$prog"
    killproc $exec
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      rm -f $lockfile
      rm -f $pidfile
      success; echo
    else
      failure; echo
    fi
    echo
    return $RETVAL
}

restart() {
    stop
    start
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
status)
    status $prog
    RETVAL=$?
    ;;
*)
    gprintf "Usage: %s {start|stop|restart|status}\n" "$0"
    RETVAL=2
esac

exit $RETVAL
