#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

for file in /opt/etc/xochitl.env.d/*.env; do
    if [[ -r $file ]]; then
        echo "Sourcing $file"
        # shellcheck disable=SC1090
        source "$file"
    fi
done

# If for some reason, sync.service is no longer masked, re-mask it
# The package install should have handled this, but something may
# have changed it.
if [[ "x$(systemctl is-enabled sync.service)" != "xmasked" ]]; then
    systemctl mask sync.service
fi

if [[ "x$(systemctl is-enabled rm-sync.service)" != "xmasked" ]]; then
    systemctl mask rm-sync.service
fi

if [ -f /usr/bin/rm-sync ] && ! is-active manual-sync@rm-sync.service; then
    systemctl enable --now manual-sync@rm-sync.service
elif [ -f /usr/bin/sync ] && ! is-active manual-sync@sync.service; then
    systemctl enable --now manual-sync@sync.service
fi

exec -a /usr/bin/xochitl /usr/bin/xochitl "$@"
