#!/bin/bash

set -e

source ./core/.env
source ./drupal/.env

create_mta_sts_policy() {
    local template_path="./core/mta-sts/.well-known/mta-sts.txt.template"
    local output_path="./core/mta-sts/.well-known/mta-sts.txt"
    local mailcow_conf="./mailcow/mailcow.conf"
    local mailcow_hostname=""

    if [ ! -f "$template_path" ]; then
        return 0
    fi

    if [ -f "$mailcow_conf" ]; then
        mailcow_hostname=$(grep '^MAILCOW_HOSTNAME=' "$mailcow_conf" | cut -d= -f2)
    fi

    if [ -z "$mailcow_hostname" ] && [ -n "$DOMAIN" ]; then
        mailcow_hostname="mail.${DOMAIN}"
    fi

    if [ ! -f "$output_path" ]; then
        mkdir -p "$(dirname "$output_path")"
        sed "s/{{MAILCOW_HOSTNAME}}/${mailcow_hostname}/g" "$template_path" > "$output_path"
        echo "Created MTA-STS policy at ${output_path}"
    else
        echo "MTA-STS policy already exists at ${output_path}"
    fi
}

create_mta_sts_policy

if [ ! -d "./drupal/drupal/root" ]; then
    echo "Creating Drupal infrastructure..."
    mkdir -p ./drupal/drupal/root
    docker run --rm drupal:$DRUPAL_VERSION-php$DRUPAL_PHP_VERSION-fpm-$DRUPAL_OS_VERSION tar -cC /opt/drupal/ . | tar -xC ./drupal/drupal/root
    docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE USER $DRUPAL_DB_USER WITH PASSWORD '$DRUPAL_DB_PASSWORD';"
    docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE DATABASE $DRUPAL_DB_NAME OWNER $DRUPAL_DB_USER;"
    docker exec postgres psql -U $POSTGRES_USER -d $DRUPAL_DB_NAME -c "GRANT ALL PRIVILEGES ON DATABASE $DRUPAL_DB_NAME TO $DRUPAL_DB_USER;"
    echo "Drupal infrastructure created"
else
    echo "Drupal infrastructure already exists"
fi

echo "Creating Hedgehog infrastructure..."
source ./hedgedoc/.env
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE USER $HEDGEDOC_DB_USER WITH PASSWORD '$HEDGEDOC_DB_PASSWORD';"
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE DATABASE $HEDGEDOC_DB_NAME OWNER $HEDGEDOC_DB_USER;"
docker exec postgres psql -U $POSTGRES_USER -d $HEDGEDOC_DB_NAME -c "GRANT ALL PRIVILEGES ON DATABASE $HEDGEDOC_DB_NAME TO $HEDGEDOC_DB_USER;"
echo "Hedgehog infrastructure created"

echo "Creating Nextcloud infrastructure..."
source ./nextcloud/.env
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE USER $NEXTCLOUD_DB_USER WITH PASSWORD '$NEXTCLOUD_DB_PASSWORD';"
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE DATABASE $NEXTCLOUD_DB_NAME OWNER $NEXTCLOUD_DB_USER;"
docker exec postgres psql -U $POSTGRES_USER -d $NEXTCLOUD_DB_NAME -c "GRANT ALL PRIVILEGES ON DATABASE $NEXTCLOUD_DB_NAME TO $NEXTCLOUD_DB_USER;"
echo "Nextcloud infrastructure created"

echo "Creating OnlyOffice infrastructure..."
source ./onlyoffice/.env
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE USER $ONLYOFFICE_DB_USER WITH PASSWORD '$ONLYOFFICE_DB_PASSWORD';"
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE DATABASE $ONLYOFFICE_DB_NAME OWNER $ONLYOFFICE_DB_USER;"
docker exec postgres psql -U $POSTGRES_USER -d $ONLYOFFICE_DB_NAME -c "GRANT ALL PRIVILEGES ON DATABASE $ONLYOFFICE_DB_NAME TO $ONLYOFFICE_DB_USER;"
echo "OnlyOffice infrastructure created"

echo "Creating OpenProject infrastructure..."
source ./openproject/.env
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE USER $OPENPROJECT_DB_USER WITH PASSWORD '$OPENPROJECT_DB_PASSWORD';"
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE DATABASE $OPENPROJECT_DB_NAME OWNER $OPENPROJECT_DB_USER;"
docker exec postgres psql -U $POSTGRES_USER -d $OPENPROJECT_DB_NAME -c "GRANT ALL PRIVILEGES ON DATABASE $OPENPROJECT_DB_NAME TO $OPENPROJECT_DB_USER;"
echo "OpenProject infrastructure created"

echo "Creating Forgejo infrastructure..."
source ./forgejo/.env
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE USER $FORGEJO_DB_USER WITH PASSWORD '$FORGEJO_DB_PASSWORD';"
docker exec postgres psql -U $POSTGRES_USER -d postgres -c "CREATE DATABASE $FORGEJO_DB_NAME OWNER $FORGEJO_DB_USER;"
docker exec postgres psql -U $POSTGRES_USER -d $FORGEJO_DB_NAME -c "GRANT ALL PRIVILEGES ON DATABASE $FORGEJO_DB_NAME TO $FORGEJO_DB_USER;"
echo "Forgejo infrastructure created"

