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

# $Id: apps,v 1.8 2004/05/14 11:08:56 taru Exp $

set -e

if test "x$1" = "x"; then
  echo "Usage: $0 <schema|ldif|list>"
  exit 1
fi

eval `ultrapossum-config init`
trap "eval `ultrapossum-config term`" 0

getapps() {
  echo "$ULTRAPOSSUM_APPS" | tr ' ' '\n'
}

case "$1" in
	list)
		getapps
		;;
	schema)
		getapps | while read cf
		do
		  schemas=""
		  . $APPSDIR/$cf
		  if test -f $APPSCONFDIR/$cf.cf; then
		    . $APPSCONFDIR/$cf.cf
		  fi
		  for schema in $schemas
		  do
		    echo "include $schema" 
		  done
		done
		;;
	ldif)
		getapps | while read cf
		do
		    . $APPSDIR/$cf
		    if test -f $APPSCONFDIR/$cf.cf; then
		      . $APPSCONFDIR/$cf.cf
		    fi
		    for app in $apps
		    do
		      pass=$(eval "echo \$${app}Password")
		      if test "x$pass" = "x"; then
		        pass=$ROOTPW
		      fi
		      cat <<EOF
dn: cn=$app,$APPLICATION
objectClass: top
objectClass: person
cn: $app
sn: $app
userPassword: `slappasswd -s $pass`
EOF
		      echo ""
		    done
		done
		;;
	*)
		echo "Unknown argument $1" 1>&2
		exit 1
		;;
esac

