#!/bin/sh
#
# monitor-resize daemon for displays that can change size
#
# chkconfig:   345 72 28
# description: perform auto-resize request for all displays all the time

### BEGIN INIT INFO
# Provides:		   monitor-resize
# Required-Start:	$local_fs $syslog
# Required-Stop:	$local_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:	perform auto-resize request for all displays all the time
### END INIT INFO

exec="/usr/sbin/monitor-resize"
prog="monitor-resize"
pidfile="/var/run/monitor-resize/monitor-resize.pid"
#port="/dev/virtio-ports/com.redhat.spice.0"

[ -e /etc/default/$prog ] && . /etc/default/$prog
[ ! -d /var/run/$prog ] && mkdir -p /var/run/$prog

. /lib/lsb/init-functions

is_running() {
   ps -eo pid,command:80 | awk -v n="$( cat $pidfile 2>/dev/null )" '$1 == n' | grep -q $prog
}

lockfile=/var/lock/$prog

start() {
   [ -x $exec ] || exit 5
   # In case the previous running monitor-resize crashed
   rm -f /tmp/kill_monitor-resize.tmp
   if ! is_running ; then
      log_daemon_msg "Starting monitor-resize daemon" "$prog"
      if start-stop-daemon --background --start --pidfile $pidfile --make-pidfile --chuid 0:0 --quiet --oknodo --exec $exec -- --nopidfile --daemon $MR_EXTRA_ARGS ; then
         log_end_msg 0
      else
         log_end_msg 1
      fi
   else
      log_daemon_msg "Starting monitor-resize daemon" "$prog"
      log_end_msg 0
   fi
}

stop() {
   touch /tmp/kill_monitor-resize.tmp
   log_daemon_msg "Stopping monitor-resize daemon" "$prog"
   if start-stop-daemon --stop --remove-pidfile --quiet --oknodo --pidfile $pidfile --signal TERM; then
      log_end_msg 0
   else
      log_end_msg 1
   fi
}

restart() {
   stop
   start
}

reload() {
   restart
}

force_reload() {
   restart
}

status() {
   status_of_proc -p $pidfile $exec $prog && exit 0 || exit $?
}

case "$1" in
   start)
      $1
      ;;
   stop)
      $1
      ;;
   restart)
      $1
      ;;
   reload)
      $1
      ;;
   force-reload)
      force_reload
      ;;
   status)
      status
      ;;
   condrestart|try-restart)
      status || exit 0
      restart
      ;;
   *)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
      exit 2
esac
exit $?
