#!/bin/bash

BASE="/usr/lib/check_mk_agent/plugins/zimbra.d"

source "${BASE}/common.sh"
source "${BASE}/cache.sh"
source "${BASE}/roles.sh"
source "${BASE}/mta.sh"
source "${BASE}/proxy.sh"
source "${BASE}/mailstore.sh"
source "${BASE}/zimbra_mailstore_mailbox_number.sh"
source "${BASE}/logger.sh"
source "${BASE}/ldap.sh"
source "${BASE}/ip_cache.sh"


main() {

    is_installed zimbra-core || exit 0


    echo '<<<zimbra_version>>>'
    echo "version:$(get_version)"


    ROLES="$(get_roles)"


    echo '<<<zimbra_roles>>>'

    echo "$ROLES" | while read -r role; do

        [ -n "$role" ] && echo "role:${role}"

    done



    if echo "$ROLES" | grep -qx "mta"; then

        echo '<<<zimbra_mta_queues>>>'
        collect_mta_queues

        echo '<<<zimbra_mta_services>>>'
        collect_mta_services

        echo '<<<zimbra_mta_deliverability>>>'
        collect_mta_deliverability

    fi



    if echo "$ROLES" | grep -qx "proxy"; then

        echo '<<<zimbra_proxy_services>>>'
        collect_proxy_services

    fi



    if echo "$ROLES" | grep -qx "mailstore"; then

        echo '<<<zimbra_mailstore_services>>>'
        collect_mailstore_services


        echo '<<<zimbra_mailbox_number>>>'
        collect_zimbra_mailstore_mailbox_number

    fi



    if echo "$ROLES" | grep -qx "logger"; then

        echo '<<<zimbra_logger_services>>>'
        collect_logger_services

    fi



    if echo "$ROLES" | grep -qx "ldap"; then

        LDAP_ROLE=$(get_ldap_role)


        echo '<<<zimbra_ldap_services>>>'
        collect_ldap_services


        if [ "$LDAP_ROLE" = "replica" ]; then

            echo '<<<zimbra_ldap_replication>>>'
            collect_ldap_replication

        fi

    fi

}


main


