#!/bin/bash
. /home/jesus/.hass
. /home/jesus/.autocompose/bin/activate
# Ruta del archivo donde se guardará el valor de startedAt
started_at_file="/home/jesus/nginx_started_at.txt"

# Obtener el ID del contenedor de nginx
container_id=$(docker ps -qf "name=nginx")

# Obtener el valor de startedAt del contenedor
started_at=$(docker inspect -f '{{.State.StartedAt}}' "$container_id")

# Si el archivo no existe, o si el startedAt es más reciente, se ha reiniciado
if [[ -f "$started_at_file" ]]; then
    # Leer la marca de tiempo almacenada en el archivo
    last_started_at=$(cat "$started_at_file")
    # Verificar si la marca de tiempo ha cambiado desde la última ejecución
    if [[ "$started_at" != "$last_started_at" ]]; then
	    echo "$started_at" > "$started_at_file"
	    hass-cli service call script.nginx_reiniciado &
    fi
fi
deactivate
