#!/bin/sh
### BEGIN INIT INFO
# Provides:          fetch-ldap-cert
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Should-Start:      $network apache apache2
# Should-Stop:       $network apache apache2
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Fetch LDAP SSL public key from the server
# Description:
### END INIT INFO
#
# Author: Petter Reinholdtsen <pere@hungry.com>
# Date:   2007-06-09

set -e

. /lib/lsb/init-functions

CERTFILE=/etc/ldap/ssl/ldap-server-pubkey.pem
LDAPSERVER=ldap

do_start() {
    if [ ! -f $CERTFILE ] && [ -f /etc/ldap/ldap.conf ] &&
       grep -q /etc/ldap/ssl/ldap-server-pubkey.pem /etc/ldap/ldap.conf ; then
	[ "$VERBOSE" != no ] && log_action_begin_msg "Fetching LDAP SSL certificate."
	ldap-server-getcert $LDAPSERVER > $CERTFILE.new
	chmod 644 $CERTFILE.new
	if test -s $CERTFILE.new ; then
	    mv $CERTFILE.new $CERTFILE
	    [ "$VERBOSE" != no ] && log_action_end_msg 0
	else
	    rm $CERTFILE.new
	    log_action_end_msg 1
	fi
    fi
}

case "$1" in
    start)
	do_start
	;;
    stop)
	;;
    restart|force-reload)
	;;
    *)
	echo "Usage: $0 {start|stop|restart|force-reload}"
	exit 2
esac
exit 0
