# Utilise l'image de base Home Assistant
ARG BUILD_FROM
FROM ${BUILD_FROM}

# Installation des dépendances
RUN apk add --no-cache \
    curl \
    bash \
    git \
    openssh-client \
    nginx \
    file

# Détection de l'architecture et téléchargement de la bonne version
RUN ARCH=$(uname -m) && \
    case "${ARCH}" in \
        x86_64)   COOK_ARCH="x86_64-unknown-linux-musl" ;; \
        aarch64)  COOK_ARCH="aarch64-unknown-linux-musl" ;; \
        armv7l)   COOK_ARCH="arm-unknown-linux-musleabihf" ;; \
        armv6l)   COOK_ARCH="arm-unknown-linux-musleabihf" ;; \
        *)        COOK_ARCH="i686-unknown-linux-musl" ;; \
    esac && \
    echo "Téléchargement de CookCLI pour l'architecture: ${COOK_ARCH}" && \
    curl -L "https://github.com/cooklang/CookCLI/releases/latest/download/cook-${COOK_ARCH}.tar.gz" | tar xz && \
    mv cook /usr/local/bin/ && \
    chmod a+x /usr/local/bin/cook && \
    # Vérification du binaire
    file /usr/local/bin/cook && \
    /usr/local/bin/cook --version

# Copie des fichiers
COPY rootfs /

# Rendre tous les scripts exécutables
RUN find /etc -name "run" -type f -exec chmod +x {} \; \
    && find /usr -name "*.sh" -type f -exec chmod +x {} \;

ENTRYPOINT ["/init"]