#!/bin/bash

#
# Copyright (C) 2022 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer.  If not, see COPYING.
#

set -e

ns8-bind-app nethserver-mail &

if [[ -f /etc/e-smith/db/configuration/defaults/webtop/type ]]; then
    ns8-bind-app nethserver-webtop5 "${WEBTOP_NODE_ID}" &
fi

if [[ -f /etc/e-smith/db/configuration/defaults/roundcubemail/type && -d '/var/lib/mysql/roundcubemail' ]]; then
    ns8-bind-app nethserver-roundcubemail "${ROUNDCUBE_NODE_ID}" &
fi

if [[ -f /etc/e-smith/db/configuration/defaults/sogod/type ]]; then
    ns8-bind-app nethserver-sogo "${SOGO_NODE_ID}" &
fi

# Getmail is not a service
if [[ -f /etc/e-smith/events/actions/nethserver-getmail-conf ]]; then
    ns8-bind-app nethserver-mail-getmail "${GETMAIL_NODE_ID}" &
fi

for pid in $(jobs -p); do
    wait "${pid}"
done
