#!/bin/bash
#
# File: ldap-control
# Description: Control a slapd server
# Author: Bill MacAllister <bill@ca-zephyr.org>
# Copyright: 2023 CZ Software

##############################################################################
# Helpers
##############################################################################

function display_help {
    echo "Usage: ldap-control [stop|start|restart|status|rstat|manual]"
}

function add_sentenial {
    if [ ! "$SLAPD_SENTINEL_FILE" = "" ]
    then
        echo "touching $SLAPD_SENTINEL_FILE ..."
        touch $SLAPD_SENTINEL_FILE
    fi
}

function delete_sentenial {
    if [ ! "$SLAPD_SENTINEL_FILE" = "" ]
    then
        if [ -e $SLAPD_SENTINEL_FILE ]
        then
            echo "Removing $SLAPD_SENTINEL_FILE"
            rm $SLAPD_SENTINEL_FILE
        fi
    fi
}

function show_sentenial {
    if [ ! "$SLAPD_SENTINEL_FILE" = "" ]
    then
         if [ -e $SLAPD_SENTINEL_FILE ]
         then
             echo "$SLAPD_SENTINEL_FILE exists"
         fi
    fi
}

##############################################################################
# Main routine
##############################################################################

# Swallow remctl sub-command
if [ "$1" = "control" ]
then
    shift
fi

case $1 in
    help)
        display_help
        exit 1
    ;;
    manual)
        /usr/bin/pod2text $0
        exit 1
        ;;
esac

. /etc/default/slapd

case $1 in
     stop)
         echo "Stopping slapd ..."
         /bin/systemctl stop slapd
         add_sentenial
         ;;
     start)
         delete_sentenial
         echo "Starting slapd ..."
         /bin/systemctl start slapd
         /bin/systemctl status slapd
         ;;
     restart)
         delete_sentenial
         echo "Restarting slapd ..."
         /bin/systemctl restart slapd
         /bin/systemctl status slapd
         ;;
     status)
         show_sentenial
         /bin/systemctl status slapd
         ;;
     rstat)
         export KRB5CCNAME=/run/service-ldap.tgt
         # Assume this host is the slave for now
         shost=`hostname -f`
         # Lookup the master host
         rcmd="ldapsearch -LLL -Q -h $shost -o ldif-wrap=no -b cn=config"
         repl=`$rcmd 'olcSyncrepl=*' olcSyncrepl | grep olcSyncrepl:`
         mhost=${repl#*//}
         mhost=${mhost%:*}
         # Command to lookup the context CSN
         lcmd='ldapsearch -LLL -Q -b dc=ca-zephyr,dc=org -s base'
         if [ $mhost = "" ]
         then
             this_csn=`$lcmd -h $shost contextCSN | grep contextCSN:`
             echo "$this_csn"
         else
             slave_csn=`$lcmd -h $shost contextCSN | grep contextCSN:`
             master_csn=`$lcmd -h $mhost contextCSN | grep contextCSN:`
             echo " slave: $slave_csn"
             echo "master: $master_csn"
             if [ ! "$slave_csn" == "$master_csn" ]
             then
                 echo "ERROR: Replication problem"
             fi
         fi
         ;;
     *)
         display_help
         echo "ERROR: unknown command ($1)"
         ;;
esac

exit

##############################################################################
# Documentation
##############################################################################
DOCS=<<__END_OF_DOCS__

=head1 NAME

ldap-control - this script is a remctl target for managing slapd

=head1 SYNOPSIS

ldap-control [stop|start|restart|status|manual]

=head1 DESCRIPTION

This controls a slapd process.  The intention is that the script will
be used under remctl control.

Note, this script will swallow the first command line arguement if it
is 'control' assuming that it is a remctl sub-command.

=head1 OPTIONS

=over 4

=item stop

Stop the slapd server and set the sentinel file.

=item start

Remove the sentinel file, start slapd, and display the status.

=item restart

Re-start slapd and display the status.

=item status

If the sentinel file exists display a message.  Display slapd status
whether or not the sentinel file exists.

=item rstat

Replication status.

=item manual

This documentation.

=back

=head1 AUTHOR

Bill MacAllister <bill@ca-zephyr.org>

=head1 COPYRIGHT

Copyright 2023 CZ Software

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

__END_OF_DOCS__
