#!/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.
#

# Usage:
#    update-ultrapossum <configure|remove>
#

set -e

eval `ultrapossum-config init`

tmp=`tempfile`
trap "rm -f $tmp; eval `ultrapossum-config term`" 0

configure_slapd() {
  echo -n "Configuring OpenLDAP for $TYPE... " 1>&2

  install -d $CONFDIR

  add_startmark "##" "SCHEMA" > $tmp
  $MODULEDIR/server/apps schema >> $tmp
  add_endmark "##" "SCHEMA" >> $tmp

  add_end_vaconf $SLAPDCONF $tmp "SCHEMA"

  if test -f "$ULTRAPOSSUM_MASTER_SLAPD_CONF"; then
    index=`grep index $ULTRAPOSSUM_MASTER_SLAPD_CONF 2> /dev/null || true`
    update=1
  fi

  cp /dev/null $ULTRAPOSSUM_SLAPD_CONF
  cp /dev/null $ULTRAPOSSUM_MASTER_SLAPD_CONF
  chmod 600 $ULTRAPOSSUM_SLAPD_CONF $ULTRAPOSSUM_MASTER_SLAPD_CONF

  $MODULEDIR/server/slapd.sh $ULTRAPOSSUM_SLAPD_CONFIN slave \
	  > $ULTRAPOSSUM_SLAPD_CONF
  $MODULEDIR/server/slapd.sh $ULTRAPOSSUM_SLAPD_CONFIN master \
    	> $ULTRAPOSSUM_MASTER_SLAPD_CONF

  index_new=`grep index $ULTRAPOSSUM_MASTER_SLAPD_CONF || true`
  if test "x$update" != "x" && test -d $DIRECTORY && test "x$index" != "x$index_new"; then
    slapindex -f $SLAPDMASTERCONF -v -b $SUFFIX
  fi

  add_startmark "##" "DB" > $tmp
  echo "include	$ULTRAPOSSUM_SLAPD_CONF" >> $tmp
  add_endmark "##" "DB" >> $tmp

  add_end_vaconf $SLAPDCONF $tmp "DB"

  if test `uname` = "SunOS"; then
    grep -v pidfile < $SLAPDCONF > $SLAPDMASTERCONF
    echo "pidfile $SLAPD_PIDFILE" >> $SLAPDMASTERCONF
  else
    sed "s!^pidfile[[:space:]][[:space:]]*.*!pidfile $SLAPD_PIDFILE!" \
  	< $SLAPDCONF > $SLAPDMASTERCONF
  fi

  add_startmark "##" "DB" > $tmp
  echo "include	$ULTRAPOSSUM_MASTER_SLAPD_CONF" >> $tmp
  add_endmark "##" "DB" >> $tmp
  add_end_vaconf $SLAPDMASTERCONF $tmp "DB"

  oldmark=$ULTRAPOSSUMMARK
  for mark in `grep "## " $SLAPDMASTERCONF | grep -- "-DB START" | sed 's/.* \(.*\)\-DB.*/\1/'`
  do
    ULTRAPOSSUMMARK=$mark
    project=`echo $mark | sed 's/ULTRAPOSSUM\(.*\)/\1/'`
    add_startmark "##" "DB" > $tmp
    echo "include       $CONFDIR/$project/ultrapossum.master.conf" >> $tmp
    add_endmark "##" "DB" >> $tmp
    add_end_vaconf $SLAPDMASTERCONF $tmp "DB"
  done
  ULTRAPOSSUMMARK=$oldmark

  cp /dev/null $ULTRAPOSSUMLDIF
  chmod 600 $ULTRAPOSSUMLDIF
  $MODULEDIR/server/ldif.sh > $ULTRAPOSSUMLDIF

  cp /dev/null $APPSLDIF
  chmod 600 $APPSLDIF
  $MODULEDIR/server/apps ldif > $APPSLDIF

  echo "done" 1>&2

  if ! test -d "`dirname $SLAPD_PIDFILE`"; then
    echo -n "Creating `dirname $SLAPD_PIDFILE` for pidfile... " 1>&2
    install -d `dirname $SLAPD_PIDFILE`
    echo "done" 1>&2
  fi

  if ! test -d "$DIRECTORY"; then
    $MODULEDIR/server/createdb.sh
  fi

  if test "x$HOST" = "x$MASTER"; then
    if test "x$SLURPDSLAVES" = "x"; then
      if test -f "$RPLDIR/$HOST/replica/slurpd.replog" ||
          test -f "$REPLOGFILE"
      then
        echo -n "Removing replog... " 1>&2
        /bin/rm -f $RPLDIR/$HOST/replica/slurpd.replog 2> /dev/null
        /bin/rm -f $REPLOGFILE 2> /dev/null
        echo "done" 1>&2
      fi
      /bin/rm -f $DIRECTORY/*.ldif 2> /dev/null
    else
      /bin/ls $DIRECTORY/*.ldif 2> /dev/null | while read f
      do
        b=`basename $f .ldif`
        if ! include $b "$SLURPDSLAVES"; then
          if test -f $RPLDIR/$HOST/replica/slurpd.replog; then
            egrep -v "^replica: $b" $RPLDIR/$HOST/replica/slurpd.replog > $RPLDIR/$HOST/replica/slurpd.replog.tmp
            mv $RPLDIR/$HOST/replica/slurpd.replog.tmp $RPLDIR/$HOST/replica/slurpd.replog
          fi
          echo -n "Removing obsolete LDIF for $b... " 1>&2
         /bin/rm -f $f
          echo "done" 1>&2
        fi
      done
    fi

    for host in $SLURPDSLAVES
    do
      if ! test -f "$DIRECTORY/$host.ldif"; then
        echo -n "Creating $host LDIF... " 1>&2
        touch $DIRECTORY/$host.ldifT
        chmod 600 $DIRECTORY/$host.ldifT
        slapcat -b $SUFFIX -f $SLAPDMASTERCONF >  $DIRECTORY/$host.ldifT
        mv $DIRECTORY/$host.ldifT $DIRECTORY/$host.ldif
        echo "done" 1>&2
      fi
    done
  fi

}

sanity_check() {
  sanity_error=0
  if test -f $SLAPDCONF; then
    if ! grep "^pidfile" $SLAPDCONF > /dev/null; then
      echo "E: no pidfile directive found in $SLAPDCONF" 1>&2
      exit 1
    fi
  else
    echo "E: $SLAPDCONF not found... You have to install it first." 1>&2
    sanity_error=1
  fi
  if test "x$TYPE" = "xclient"; then
    echo "E: $HOST not found neither MASTER or SLAVES" 1>&2
    exit 1
  fi
  if test "x$SYNCBACKUPS" != "x" && test "x$SLAPD_PROVIDES_SYNCBACKUP" = "x"
  then
    echo "E: can't detect slapd supports syncbackup." 1>&2
    exit 1
  fi
  if test -d "$DIRECTORY"; then
    if ! test -f "$ULTRAPOSSUM_SLAPD_CONF"; then
      echo "W: db directory already exists: $DIRECTORY" 1>&2
    else
      if ! test -r  "$ULTRAPOSSUM_SLAPD_CONF"; then
        echo "W: no permission to check whether db directory is sane" 1>&2
      else
        dir=`egrep '^[ \t]*directory' $ULTRAPOSSUM_SLAPD_CONF | sed 's/directory//' | tr -d '" '`
        if test "x$dir" = "x$DIRECTORY"; then
          suffix=`grep '^[ \t]*suffix' $ULTRAPOSSUM_SLAPD_CONF | sed 's/suffix//' | tr -d '" '`
	  actsuffix=`slapcat -b $suffix | head -1 | cut -d' ' -f2-`
	  if test "x$actsuffix" != "x$SUFFIX"; then
            echo "E: db directory holds another namingContext: $actsuffix in $DIRECTORY" 1>&2
	    exit 1
	  fi
	fi
      fi
    fi
  fi
  if test "$sanity_error" != "0"; then
    exit 1
  fi
}

configure() {
  sanity_check
  if test "x`$MODULEDIR/server/startup status`" = "xrunning"; then
    oSLURPDSLAVES=$SLURPDSLAVES
    eval `$MODULEDIR/server/runtime SLURPDSLAVES`
    if test "x$oSLURPDSLAVES" != "x$SLURPDSLAVES"; then
      echo "E: Slurpd settings are changed but UltraPossum server running... You have to stop it" 1>&2
      exit 1
    fi
    SLURPDSLAVES=$oSLURPDSLAVES
  fi
  configure_slapd
}

remove() {
  /bin/rm -f $ULTRAPOSSUMLDIF $ULTRAPOSSUM_SLAPD_CONF
  /bin/rm -f $APPSLDIF $SLAPDMASTERCONF
  /bin/rm -f $ULTRAPOSSUM_MASTER_SLAPD_CONF
  if test -f "$SLAPDCONF"; then
    strip_vaconf $SLAPDCONF "SCHEMA"
    strip_vaconf $SLAPDCONF "DB"
  fi
}

case "x$1" in
	xconfigure)
		configure
		ultrapossum-config set .status ULTRAPOSSUM_MODULE_SERVER=installed
		/bin/rm -f $CONFSTATUS
	;;
	xremove)
		remove
		ultrapossum-config remove .status ULTRAPOSSUM_MODULE_SERVER
		/bin/rm -f $CONFSTATUS
	;;
	xsanity)
		sanity_check
	;;
	x)
		echo "Usage: $0 <configure|remove|sanity>" 1>&2
		exit 1
	;;
	x*)
		echo "Unknown argument: $1" 1>&2
		exit 1
	;;
esac

