#!/bin/bash

# Alias: NagVis linked Web GUI
# Menu: Addons
# Description:
#  When clicking on NagVis map icons the user is redirected to another
#  Web-GUI. By default this is the classic Nagios webinterface. With
#  this switch the targeted Web-GUI can be changed.

# Makes it possible to switch all the links from NagVis
# to the default Web GUI to the choosen GUI or a defined
# other one.

case "$1" in
    default)
        echo "check_mk"
    ;;
    choices)
        echo "nagios: Classic Nagios webinterface"
        echo "check_mk: The Check_MK's Multisite GUI"
        echo "thruk: Thruk Monitoring Webinterface"
        echo "none: Use NagVis default behaviour"
    ;;
    set)
        # Skip this hook when NagVis configuration directory does not exist
        if [ ! -d $OMD_ROOT/etc/nagvis ]; then
            exit 0
        fi

        CFG_FILE=$OMD_ROOT/etc/nagvis/conf.d/urls.ini.php
        GUI=$2

        # When a user sets NAGVIS_URL to "none", previous config files are
        # deleted and the hook finishes. NagVis will use it's defaults or the
        # configuration which has been made in the NagVis configuration manually.
        if [ "$GUI" == "none" ]; then
            if [ -f $CFG_FILE ]; then
                rm $CFG_FILE
            fi
            exit 0
        fi

        # Previous OMD versions offered the option "auto", which is not possible
        # anymore. Behave like the default value has been selected.
        if [ "$GUI" == "auto" ]; then
            GUI=check_mk
        fi

        if [ ! -d $OMD_ROOT/etc/nagvis/conf.d ]; then
            mkdir -p $OMD_ROOT/etc/nagvis/conf.d
        fi

        if [ "$GUI" == "check_mk" ]; then
            HTMLCGI="/$OMD_SITE/check_mk"
            HOSTURL="[htmlcgi]/view.py?view_name=host&site=&host=[host_name]"
            SERVICEURL="[htmlcgi]/view.py?view_name=service&site=&host=[host_name]&service=[service_description]"
            HOSTGROUPURL="[htmlcgi]/view.py?view_name=hostgroup&site=&hostgroup=[hostgroup_name]"
            SERVICEGROUPURL="[htmlcgi]/view.py?view_name=servicegroup&site=&servicegroup=[servicegroup_name]"
            ACTION_URLS="host_downtime_url=\"\"\nhost_ack_url=\"\"\nservice_downtime_url=\"\"\nservice_ack_url=\"\"\n"
        else
            HTMLCGI="/$OMD_SITE/$GUI/cgi-bin"
            HOSTURL="[htmlcgi]/status.cgi?host=[host_name]"
            HOSTGROUPURL="[htmlcgi]/status.cgi?hostgroup=[hostgroup_name]"
            SERVICEURL="[htmlcgi]/extinfo.cgi?type=2&host=[host_name]&service=[service_description]"
            SERVICEGROUPURL="[htmlcgi]/status.cgi?servicegroup=[servicegroup_name]&style=detail"
            ACTION_URLS=""
        fi

        # Now write the NagVis configuration. This is a dedicated file so we can easily write it out here
        cat > $CFG_FILE <<EOF
; <?php exit(1); ?>
; Do not edit this file. Your changes will be overwritten.
; This file is controlled by the OMD hook NAGVIS_URLS.
;
; Written by OMD hook NAGVIS_URLS at $(date).
[paths]
htmlcgi="$HTMLCGI"

[defaults]
hosturl="$HOSTURL"
hostgroupurl="$HOSTGROUPURL"
serviceurl="$SERVICEURL"
servicegroupurl="$SERVICEGROUPURL"
EOF
        echo -ne "$ACTION_URLS" >> $CFG_FILE
    ;;

esac
