#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          weston
# Required-Start:    
# Required-Stop:     
# Should-Start:      
# Should-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start weston 
# Description:       Start weston with /etc/xdg/weston/weston.ini configuration
### END INIT INFO

# Create runtime directory
RUNTIME_DIR=/run/user/root
[ -d ${RUNTIME_DIR} ] || mkdir -p -m 700 ${RUNTIME_DIR}

export XDG_RUNTIME_DIR=${RUNTIME_DIR}
export ECORE_EVAS_ENGINE=wayland_shm
export ELM_ENGINE=wayland_shm
export GDK_BACKEND=wayland
export XDG_SESSION_ID=c1

WESTON_BIN=/usr/bin/weston
WESTON_ARGS=--tty=1
WESTON_PID=/var/run/weston.pid

start_weston() {
  printf "Starting weston: "
  start-stop-daemon -b -S -q -m -p ${WESTON_PID} --exec ${WESTON_BIN} -- $WESTON_ARGS
  echo "OK"
}

stop_weston() {
  printf "Stoping weston: "
  start-stop-daemon -K -q -p ${WESTON_PID}
  echo "OK"
}

case "$1" in
  start)
    start_weston
    ;;

  stop)
    stop_weston
    ;;

  restart|reload|force-reload)
    stop_weston
    start_weston  
    ;;

  *)
    echo "Usage: $0 start"
    exit 1
  ;;
esac
