#!/bin/sh
#
#  This code was developped by SECIOSS (http://www.secioss.co.jp/).
#
#                 Copyright (C) 2007 SECIOSS CORPORATION
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License
#  as published by the Free Software Foundation.

source '/usr/local/lism/etc/lismcmd.conf'

ACTIVEATTR=lismClusterActive
MASTERATTR=lismClusterMaster
DATAATTR=lismClusterNode
DN="cn=cluster,$SUFFIX"

data=$2

function addcluster() {
    if [ -n "$1" ]; then
        opt=";opt-$1"
    fi

    modlist="add: $ACTIVEATTR$opt"
    for d in `echo $data | sed "s/,/\n/"`; do
        modlist="$modlist
$ACTIVEATTR$opt: $d"
    done

    ldapmodify -x -H $URI -D $BINDDN -w $BINDPW \
<< CLUSTER
dn: $DN
changetype: modify
$modlist
CLUSTER
}

function deletecluster() {
    modlist="delete: $ACTIVEATTR"
    for d in `echo $data | sed "s/,/\n/"`; do
        modlist="$modlist
$ACTIVEATTR: $d"
    done

    ldapmodify -x -H $URI -D $BINDDN -w $BINDPW \
<< CLUSTER
dn: $DN
changetype: modify
$modlist
CLUSTER
}

function readcluster() {
    ldapsearch -x -LLL -H $URI -D $BINDDN -w $BINDPW -b $DN -s base
}

function usage() {
    echo $"Usage: $0 {add|addonly|delete|read} [data]"
    exit 1
}

case "$1" in
    add)
        if [ -z "$data" ]; then
            usage
        fi
        addcluster
        ;;
    addonly)
        if [ -z "$data" ]; then
            usage
        fi
        addcluster nosync
        ;;
    delete)
        if [ -z "$data" ]; then
            usage
        fi
        deletecluster
        ;;
    read)
        readcluster
        ;;
    *)
        usage
esac

exit 0
