#!/usr/bin/env bash

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() {
  for cf in `/bin/ls $APPSDIR`
  do
    echo $cf
  done
}

case "$1" in
	list)
		getapps
		;;
	schema)
		getapps | while read cf
		do
		  . $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

