#!/bin/ash
# *********************************************************************
# slapd-bind: experimental NoSQL shell back-end for slapd(8) BIND .
#
# Copyright (c) 2006,2007,2009 Carlo Strozzi
#
# 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; version 2 dated June, 1991.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# *********************************************************************

# Get local settings and apply defaults.
: ${TMPDIR:=/tmp} ${NOSQL_CHARSET:=utf-8}
: ${NOSQL_INSTALL:=@NOSQL_INSTALL@}
: ${SWU_INSTALL:=@SWU_INSTALL@}
: ${PATH:=@LOCAL_PATH@}

# If this program is called by slapd(8) we cannot be sure that
# the command PATH includes what we need so let's double-check.

which _awk >/dev/null || . $NOSQL_INSTALL/etc/nosql.rc

export NOSQL_INSTALL SWU_INSTALL PATH TMPDIR NOSQL_CHARSET

slapdExit () {
   echo "RESULT"
   if [ -z "$1" ]
   then
      set - 1
   else
      echo "matched: $1"
      set - 0
   fi
   echo "code: $1"
   exit $1
}

# Required arguments:
#
# 1. target directory
# 2. table symbolic name
# 3. database schema, expressed either in full- or relative-path

if [ $# -ne 3 ]
then
   echo "usage: slapd-bind <directory> <table> <schema>" >&2
   slapdExit
fi

# The target NoSQL database directory must be accessible.
cd "$1" || slapdExit

if [ -e "$2" ]
then :
else
   slapdExit
fi

if [ -e "$3" ]
then :
else
   slapdExit
fi

set -- $(_awk -- 'NR == 1 {if ($1 != "BIND") exit(rc=1)}
		  sub(/^dn: /,"") {print;exit} END{exit(rc)}')

slapdExit "$1"

# EOF
