#!/bin/bash

# Check if .env file exists
if [ ! -f .env ]; then
    echo "Error: .env file not found in /var/www/owncloud"
    exit 1
fi

# Create network if it doesn't exist
if ! docker network ls | grep -q 'web '; then
    docker network create --driver=overlay web
fi

# Automatically export all variables from .env file
set -a
source .env
set +a

# Deploy the stack
envsubst < docker-stack.yml | docker stack deploy selfhosted-owncloud --compose-file=docker-stack.yml --with-registry-auth --resolve-image always