### BEGIN INIT INFO
# Provides:          Hue dockerized server
# Required-Start:    $remote_fs $syslog docker
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start hue server daemon
# Description:       Hue dockerized server
### END INIT INFO

DAEMON_PATH="/etc/hue_docker/conf/"
CONTAINER={{container}}
NAME={{service_name}}
DESC="Hue server dockerized startup script"
set -e

set -e
if ! service docker status;
  then
    printf "%s\n" "Service startup failed\n"
    printf "%s\n" "docker service should be started\n"
    exit 1
fi
case "$1" in
start)
    printf "%-50s" "Starting $NAME..."
    if [ "`docker ps | grep ${CONTAINER}`" ]; then
      printf "%s\n" "Service already running"
      exit 0
    else
      if docker start ${CONTAINER}  ; then
        printf "%s\n"
        printf "%s\n" "Service started [Ok]"
        printf "%s\n" "Container started [${CONTAINER}]"
        echo ''
        exit 0
      else
        printf "%s\n" "Fail to start container [${CONTAINER}]"
        exit 1
      fi
    fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if ! [ "`docker ps | grep ${CONTAINER}`" ]; then
            printf "%s\n" "Service not running"
            exit 3
        else
            echo "Service running"
            exit 0
        fi

;;
stop)
        printf "%-50s" "Stopping $NAME"
        if ! [ "`docker ps | grep ${CONTAINER}`" ]; then
            printf "%s\n" "Service already stopped"
            exit 1
        else
            docker stop ${CONTAINER}
            printf "%s\n" "Service stopped [Ok]"
            printf "%s\n" "Container stopped [${CONTAINER}]"
            exit 0
        fi
;;

restart)
    $0 stop
    $0 start
;;

# remove)
#         printf "You will have to run the container again"
#         printf "%s\n"
#         read -r -p "Are you sure ? [y/N] " response
#         case $response in
#             [yY][eE][sS]|[yY])
#                 printf "%-50s" "Removing (force) $NAME"
#                 docker rm -f $CONTAINER
#                 printf "%s\n" "Removed"
#                 exit 0
#                 ;;
#             *)
#                 printf "%s\n" "Not removed"
#                 exit 1
#                 ;;
#         esac
# ;;


*)
        echo "Usage: $N {status|start|stop|restart}"
        exit 1
esac
