#!/bin/bash

# First run
#mkdir /opt/appdaemon && cd /opt/appdaemon
#git clone https://github.com/home-assistant/appdaemon.git .

# All subsequent runs: update to latest app daemon, build, run
cd /opt/appdaemon
git checkout master
git pull
LATEST=`git tag | sort -r | grep -v b | head -n1`
git checkout ${LATEST}
docker build -t appdaemon:latest -t appdaemon:${LATEST} .

docker container stop home-assistant-dashboard
docker container rm home-assistant-dashboard

docker run -it --name home-assistant-dashboard \
    --restart unless-stopped \
    --network docker-private \
    -e DEBUG=1 \
    -e HA_URL="http://home-assistant:8123" \
    -l traefik.frontend.rule="Host:home-automation-dashboard.domain.tld;;ReplacePathRegex: ^/$ /Main" \

    -l traefik.frontend.passHostHeader=true \
    -v /etc/localtime:/etc/localtime:ro \
    -v /var/home-assistant/appdaemon:/conf \
    appdaemon:latest
