#!/bin/sh
# postinst script for omd
#
# see: dh_installdeb(1)

set -e

OMD_ROOT="/omd/versions/4.60-labs-edition"
MONITORING_PLUGIN_PATH="$OMD_ROOT/lib/monitoring-plugins"
SUID_PLUGINS="check_icmp;cap_net_raw+ep check_dhcp;cap_net_bind_service+ep"
PROGRAM_PATH="$OMD_ROOT/bin"
APACHE_OMD_CONF="/etc/apache2/conf.d/zzz_omd.conf"
SYSCTL_OMD_CONF="/etc/sysctl.d/01-omd.conf"

# Source debconf library.
. /usr/share/debconf/confmodule

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

running_on_debian() {
    grep -q '^ID=debian' /etc/os-release
}

setperm() {
    local user="$1"
    local group="$2"
    local mode="$3"
    local file="$4"
    shift 4
    # only do something when no setting exists
    if ! dpkg-statoverride --list "$file" >/dev/null 2>&1; then
      chown "$user":"$group" "$file"
      chmod "$mode" "$file"
    fi
}

setcapabilities() {
    local user="$1"
    local group="$2"
    local mode="$3"
    local cap="$4"
    local file="$5"

    setperm "$user" "$group" "$mode" "$file"

    if command -v setcap > /dev/null; then
        if ! setcap "$cap" "$file"; then
            echo "Setcap failed on $file, falling back to setuid" >&2
            return 1
        fi
    else
        echo "Setcap is not installed, falling back to setuid" >&2
        return 1
    fi

    return 0
}

case "$1" in
  configure)
    ln -sfn /opt/omd /omd
    mkdir -p /etc/bash_completion.d
    update-alternatives --install /omd/versions/default \
       omd "$OMD_ROOT" 60 \
       --slave /usr/bin/omd omd.bin $OMD_ROOT/bin/omd \
       --slave /etc/init.d/omd omd.init "$OMD_ROOT/share/omd/omd.init" \
       --slave /etc/bash_completion.d/omd omd.bash_completion "$OMD_ROOT/lib/omd/omd_bash_completion" \
       --slave /usr/share/man/man8/omd.8.gz omd.man8 "$OMD_ROOT/share/man/man8/omd.8.gz"

    # -- looking for group omd, create it, if not exist
    if ! getent group omd > /dev/null ; then
      echo 'Adding system group omd' 1>&2
      addgroup --system --force-badname omd > /dev/null
    fi

    GROUP_ID=$( getent group omd | awk -F: '{print $3}' )

    PLUGPERM="4754"
    for PLUGIN in $SUID_PLUGINS; do
      cap=${PLUGIN#*;}
      PLUGIN=${PLUGIN%;*}
      setcapabilities root omd 0754 "$cap" "$MONITORING_PLUGIN_PATH/$PLUGIN" ||
      setperm root omd $PLUGPERM "$MONITORING_PLUGIN_PATH/$PLUGIN"
    done

    BLACKBOX_EXPORTER="$OMD_ROOT/bin/blackbox_exporter"
    # something is utterly broken on debian systems
    # compiling and running with cap_net_raw makes blackbox_exporter segfault
    #
    # blackbox_export[5628] vsyscall attempted with vsyscall=none ip:ffffffffff600000 cs:33 sp:7ffd7b312318 ...
    # blackbox_export[5628]: segfault at ffffffffff600000 ip ffffffffff600000 sp 00007ffd7b312318 error 15
    # Code: Bad RIP value.
    if ! running_on_debian ; then
        setcapabilities root omd 0754 cap_net_raw+ep "$BLACKBOX_EXPORTER" || true
    else
        ping_group_range=$( sysctl -n net.ipv4.ping_group_range || true )
        # if /etc/sysctl.d exists and the range is kernels default, then take this setting
        if [ -d "$( dirname $SYSCTL_OMD_CONF )"  ] && [ "$(echo $ping_group_range)" = "1 0" ] ; then
            printf '%s\n' "net.ipv4.ping_group_range = $GROUP_ID $GROUP_ID" > "$SYSCTL_OMD_CONF"
            sysctl -p "$SYSCTL_OMD_CONF" || echo "warning: activating $SYSCTL_OMD_CONF via sysctl failed" >&2
        fi
    fi

    db_stop
    # -- create apache config include if not exist
    if test -d /etc/apache2/conf-available; then
      # On e.g. ubuntu 13.10 conf.d is not loaded anymore, use conf-available
      APACHE_OMD_CONF="/etc/apache2/conf-available/zzz_omd.conf"
    fi

    if ! test -e $APACHE_OMD_CONF; then
      echo "IncludeOptional /omd/apache/*.conf" > $APACHE_OMD_CONF

      # -- enable conf include, when available
      ! test -x /usr/sbin/a2enconf || a2enconf zzz_omd
    fi

    # -- enable apache modules
    if ! test -e /etc/apache2/mods-enabled/proxy_http.load; then
       a2enmod proxy_http	# also enables dependend modules
    fi
    if ! test -e /etc/apache2/mods-enabled/rewrite.load; then
       a2enmod rewrite
    fi
    if ! test -e /etc/apache2/mods-enabled/ssl.load; then
        a2enmod ssl
        a2ensite default-ssl
    fi

    ls -1 /etc/apache2/sites-available/*default*.conf 2>/dev/null | \
      while read file; do
        if ! grep /omd/apache/ $file >/dev/null 2>&1; then
          sed -i -e 's|</VirtualHost>|\n    IncludeOptional /omd/apache/*.include\n</VirtualHost>|g' $file
        fi
      done

    echo "New default version is 4.60-labs-edition."
    update-alternatives --set omd "$OMD_ROOT"

    if [ -d /usr/lib/systemd/system/. ]; then
        ln -sfn /omd/versions/default/share/omd/omd.service /usr/lib/systemd/system/omd.service
        systemctl disable omd.service
        systemctl enable omd.service
    fi

    ;;

  abort-upgrade|abort-remove|abort-deconfigure)
    exit 0
    ;;

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.



exit 0
