#!/bin/sh

device=/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0

port=8080
appdata=/home/hass/z2m_data

image=ghcr.io/koenkk/zigbee2mqtt
name=zigbee2mqtt

start() {
  docker run \
    --detach \
    --name ${name} \
    --restart=unless-stopped \
    --device=${device}:/dev/ttyACM0 \
    --publish ${port}:8080 \
    --env TZ=`cat /etc/timezone` \
    --volume ${appdata}:/app/data \
    --volume /run/udev:/run/udev:ro \
    ghcr.io/koenkk/zigbee2mqtt
}

stop() {
  docker stop ${name}
  docker rm ${name}
}

restart() {
  stop
  start
}

update() {
  docker pull ${image}
}

upgrade() {
  restart
  docker image prune -f
}

case "${1}" in
  start|stop|restart|update|upgrade)
    ${1}
    ;;
  *)
    echo "Usage: ${0} start|stop|restart|update|upgrade"
    ;;
esac
