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

set -e

# 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


setup_apache2() {
  echo "Configuring apache2 vhost ..."

  # enable modules we need
  if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
      . /usr/share/apache2/apache2-maintscript-helper
      apache2_invoke enmod alias      || exit $?
      apache2_invoke enmod fcgid      || exit $?
      apache2_invoke enmod auth_basic || exit $?
      apache2_invoke enmod rewrite    || exit $?

      # enable thruk itself
      [ -f /etc/apache2/conf-enabled/thruk.conf ]                   || apache2_invoke enconf thruk
      [ -f /etc/apache2/conf-enabled/thruk_cookie_auth_vhost.conf ] || apache2_invoke enconf thruk_cookie_auth_vhost
  fi

  # activate cookie in existing default virtual hosts
  set +e
  find /etc/apache2/sites-available/ -iname '*default*' -print | \
  while read file; do
      if ! grep thruk_cookie_auth.include $file >/dev/null 2>&1; then
          sed -i -e 's|</VirtualHost>|\n    Include /usr/share/thruk/thruk_cookie_auth.include\n</VirtualHost>|g' $file
      fi
  done
  set -e

  # reload new apache config
  deb-systemd-invoke condreload apache2 || true

  echo "Thruk have been configured for http://$(hostname)/thruk/."
  echo "The default user is 'thrukadmin' with password 'thrukadmin'. You can usually change that by 'htpasswd /etc/thruk/htpasswd thrukadmin'"
}


case "$1" in
    configure)
        # restore themes, plugins and ssi
        if [ -d /tmp/thruk_update/themes/. ]; then
          # do not remove the new default themes
          test -h /tmp/thruk_update/themes/Light || mv /etc/thruk/themes/themes-enabled/Light /etc/thruk/themes/themes-enabled/.Light
          test -h /tmp/thruk_update/themes/Dark  || mv /etc/thruk/themes/themes-enabled/Dark  /etc/thruk/themes/themes-enabled/.Dark
          rm -f /etc/thruk/themes/themes-enabled/*
          cp -rp /tmp/thruk_update/themes/* /etc/thruk/themes/themes-enabled/
          test -h /etc/thruk/themes/themes-enabled/.Light && mv /etc/thruk/themes/themes-enabled/.Light /etc/thruk/themes/themes-enabled/Light
          test -h /etc/thruk/themes/themes-enabled/.Dark  && mv /etc/thruk/themes/themes-enabled/.Dark  /etc/thruk/themes/themes-enabled/Dark
          # remove broken/old symlinks
          for link in /etc/thruk/themes/themes-enabled/*; do test -e $link/. || rm $link; done
        fi
        if [ -d /tmp/thruk_update/plugins/. ]; then
          rm -f /etc/thruk/plugins/plugins-enabled/*
          cp -rp /tmp/thruk_update/plugins/* /etc/thruk/plugins/plugins-enabled/
        fi
        # remove broken/old symlinks
        for link in /etc/thruk/plugins/plugins-enabled/*; do test -e $link/. || rm $link; done
        echo "thruk plugins enabled:" $(ls /etc/thruk/plugins/plugins-enabled/)
        if [ -d /tmp/thruk_update/ssi/. ]; then
          rm -f /etc/thruk/ssi/*
          cp -rp /tmp/thruk_update/ssi/* /etc/thruk/ssi/
        fi
        rm -rf /tmp/thruk_update

        # set permissions
        mkdir -p /var/lib/thruk \
                 /var/log/thruk \
                 /var/cache/thruk/reports \
                 /etc/thruk/bp \
                 /etc/thruk/panorama \
                 /etc/thruk/thruk_local.d
        chown -R www-data: /var/lib/thruk \
                           /var/cache/thruk \
                           /var/log/thruk \
                           /etc/thruk/plugins/plugins-enabled \
                           /etc/thruk/thruk_local.conf \
                           /etc/thruk/bp \
                           /etc/thruk/panorama \
                           /etc/thruk/thruk_local.d
        chmod 755 /usr/share/thruk/fcgid_env.sh
        chmod 755 /usr/share/thruk/thruk_authd.pl
        chmod 755 /usr/bin/thruk

        # Depending on which webserver is installed, set up the basics
        # (so far only supports apache2)
        [ -x /usr/sbin/apache2 ] && setup_apache2 $@

        # create empty crontab
        crontab -l -u www-data 2>/dev/null | crontab -u www-data -

        # cleanup tmp files
        thruk -a livecachestop --local >/dev/null 2>&1 || :
        rm -rf /var/cache/thruk/*

        # update cron entries
        thruk -a clearcache,installcron --local > /dev/null
    ;;

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

    *)
        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.

#DEBHELPER#

exit 0
