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

# $Id: update-server,v 1.34 2004/05/11 10:32:59 taru Exp $

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"

  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

  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 -f "$ULTRAPOSSUM_INIT_LDIF_IN"; then
    echo -n "Configuring initial LDIF... " 1>&2
    sed -e "s/#SUFFIX#/$SUFFIX/" < $ULTRAPOSSUM_INIT_LDIF_IN > $INITLDIF
    echo "done" 1>&2
  else
    /bin/rm -f $INITLDIF
  fi

  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
  else
    index_new=`grep index $ULTRAPOSSUM_MASTER_SLAPD_CONF`
    if test "x$ULTRAPOSSUM_INDEX" != "x$index_new"; then
      if test "`$MODULEDIR/server/startup status`" = "running"; then
        echo "W: index configuration is changed while server is running" 1>&2
      else
        echo -n "Indexing database... " 1>&2
        slapindex -f $SLAPDMASTERCONF -b $SUFFIX
        echo "done" 1>&2
      fi
    fi
  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
  # ad-hoc: this must be common sanity check not update-server
  if test "x$SUFFIX" = "x"; then
      echo "E: SUFFIX must not be empty." 1>&2
      exit 1
  fi
  if test "x$DBDIRECTORY" = "x"; then
      echo "E: DBDIRECTORY must not be empty." 1>&2
      exit 1
  fi
  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
    # FIXME: this is not correct
    if test "x`grep suffix $SLAPDCONF | awk -F' ' '{print $2;}' | tr -d '\"' | egrep -v '^#'`" = "x$SUFFIX"; then
      echo "E: You have already configured $SUFFIX other than UltraPossum 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 -f "$ULTRAPOSSUM_SLAPD_CONFIN"; then
    echo "E: backend '$SLAPD_BACKEND_DB_TYPE' not supported" 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 "$DIRECTORY/ultrapossum-environ"; then
      val=`getvalue $DIRECTORY/ultrapossum-environ SUFFIX`
      if test "x$val" != "x$SUFFIX"; then
        echo "E: db directory holds another namingContext: $val in $DIRECTORY" 1>&2
        exit 1
      fi
      val=`getvalue $DIRECTORY/ultrapossum-environ SLAPD_BACKEND_DB_TYPE | cut -d= -f2- | sed -e 's/"\(.*\)"/\1/'`
      if test "x$val" != "x$SLAPD_BACKEND_DB_TYPE"; then
        echo "E: db directory holds another backend type: $val in $DIRECTORY" 1>&2
        exit 1
      fi
    else
      echo "W: db directory already exists: $DIRECTORY" 1>&2
    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 $INITLDIF
  /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

