#!/usr/bin/env bash
#
# Copyright (C) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#

set -e

eval `ultrapossum-config init`
trap "eval `ultrapossum-config term`" 0

SLAPD=${SLAPD:-$ldaplibexecdir/slapd}
SLURPD=${SLURPD:-$ldaplibexecdir/slurpd}

configure_server() {
  if test "x$ULTRAPOSSUM_MODULE_SERVER" != "xinstalled"; then
    $UPDATEDIR/update-server configure
  fi
  update-ultrapossum reconfigure
}

case "x$1" in
    xmaster_stop|xmaster_status|xslurp_stop|xslurp_status)
      ldap$1
      ;;

    xmaster_start|xslurp_start)
      configure_server
      ldap$1
      ;;

    xstatus)
      if test -f "$ULTRAPOSSUMRUN"; then
        if test -r "$ULTRAPOSSUMRUN"; then
          . $ULTRAPOSSUMRUN
	else
	  echo "Warning: no permission to read ultrapossum runtime variable" 1>&2
	fi
      fi
      if test `ldapmaster_status` = "running" ||
	 test `ldapslave_status` = "running"; then
        echo "running"
      else
        echo "stopped"
      fi
      ;;
    
    xstart|xstop)
      if test "$1" = "start"; then
        configure_server
      fi
      if test "$1" = "stop" && test -f "$ULTRAPOSSUMRUN"; then
        . $ULTRAPOSSUMRUN
      fi
      if test "$TYPE" = "master"; then
        ldapmaster_service_$1
      else
        ldapslave_$1
      fi
      if test "$1" = "start"; then
	install -d `dirname $ULTRAPOSSUMRUN`
        /bin/cp $CONFSTATUS $ULTRAPOSSUMRUN
      else
        /bin/rm -f $ULTRAPOSSUMRUN
      fi
      ;;
    *)
      echo "Unknown argument: $1" 1>&2
      exit 1
      ;;
esac
