#!/bin/sh
#
# /etc/rc.d/rc.incusd
#
# start/stop/restart incusd as a daemon.


export INCUSD_GROUP=wheel
export INCUS_UI=/opt/incus/ui

# Use defaults from /etc/default/incusd
# (overrides anything set above).
#
if [ -r /etc/default/incus ]; then
  . /etc/default/incus
fi


incusd_start() {
  echo "Starting incusd:  /usr/sbin/incus --group=$INCUSD_GROUP --logfile=/var/log/incus/incusd.log (INCUS_UI=$INCUS_UI)"
  INCUS_EDK2_PATH=/usr/share/edk2-ovmf-x64 /usr/sbin/incusd --group=$INCUSD_GROUP --logfile=/var/log/incus/incusd.log 2>/dev/null &

}

incusd_stop() {
  if [ "$(pgrep -fc /usr/sbin/incusd)" -gt "0" ]; then
    echo "Terminating incusd"
    pkill -f /usr/sbin/incusd
  fi
}

incusd_restart() {
  incusd_stop
  sleep 1
  incusd_start
}

case "$1" in
'start')
  # We don't want to run this more than once, so just use restart to start it:
  incusd_restart
  ;;
'stop')
  incusd_stop
  ;;
'restart')
  incusd_restart
  ;;
*)
  incusd_start
esac
