#!/bin/bash

# Alias: Use Dokuwiki for user management
# Menu: Web GUI
# Description:
#  Dokuwiki - on OMD available on the URL /$OMD_SITE/wiki - is not only
#  a useful Wiki, but also implements a user management.
#  If you enable this option, you will have Nagios, NagVis, Multisite and
#  other web applications access the user database of Dokuwiki for authentication.
#  Otherwise users are managed via the file ~/etc/passwd.

case "$1" in
   default)
       echo "off"
   ;;
   choices)
       echo "on: enable - use Dokuwiki for user management"
       echo "off: disable - edit etc/htpasswd manually"
   ;;
   set)
       if [ "$2" == "on" ]
       then
         if [ ! -h ${OMD_ROOT}/etc/htpasswd ] || [ ! $(readlink ${OMD_ROOT}/etc/htpasswd) = "dokuwiki/users.auth.php" ]
         then
           mv ${OMD_ROOT}/etc/htpasswd ${OMD_ROOT}/etc/htpasswd.omd
           ln -s dokuwiki/users.auth.php ${OMD_ROOT}/etc/htpasswd
         else
           ln -sf dokuwiki/users.auth.php ${OMD_ROOT}/etc/htpasswd
         fi
       else
         # Prevent destroying links showing anywhere else
         if [ -h ${OMD_ROOT}/etc/htpasswd ] && [ $(readlink ${OMD_ROOT}/etc/htpasswd) = "dokuwiki/users.auth.php" ]
         then
           ln -sf htpasswd.omd ${OMD_ROOT}/etc/htpasswd
         fi
       fi
   ;;
esac
