#!/bin/bash
#
# (c) 2004-2100 ;)
# Author: cutmasta AT netline-is D0T de
# "Jus7 s0m3 l4m3 b4sh Pr0n"
# 
#
# This Script adds a OXResource to LDAP !
#
#
# GLOBAL CONFIGFILE
prefix=/usr
exec_prefix=${prefix}

GLOBAL_CONF="/etc/open-xchange/admintools.conf"

if [ -f $GLOBAL_CONF ]
    then
    . $GLOBAL_CONF
    else
    echo "Config File $GLOBAL_CONF not found."
    exit 0
fi

ac_prev=
for ac_option
do 
  if test -n "$ac_prev"; then
    eval "$ac_prev=\$ac_option"
    ac_prev=
    continue
  fi
  case "$ac_option" in
      -*=*) ac_optarg=`echo "$ac_option" | $SED_BIN 's/[-_a-zA-Z0-9]*=//'` ;;
      *) ac_optarg= ;;
  esac
  
  case "$ac_option" in
      
      --name)
	  ac_prev=name ;;
      --name=*) 
	  RESNAME=$ac_optarg ;;

      --av)
	  ac_prev=av ;;
      --av=*) 
	  RESAV=$ac_optarg ;;

      -help | --help | -? | --?)
	  
	  cat <<EOF

Usage: $0 [Options]

Options:
  --name=NAME                  The Resource Name - eg. car1
                          
  --av=BOOL             Is the Resource available - TRUE or FALSE

EOF

	  exit 0
	  
	  ;;
      
      *)    
	  echo "Unknown command $ac_option"
	  echo "Try $0 --help"
	  exit 1
	  ;;
  esac
  
done

ERROR=

if [ "$RESNAME" = "" ]
then
        ERROR="y"
        echo "Specify an Resource Name!"
fi

if [ "$RESAV" = "" ]
then
        ERROR="y"
        echo "Specify if the Resource is avaiable!"
fi

if [ "$ERROR" = "y" ]
then 
	echo "Please provide all needed Parameters!"
	echo "Try $0 --help"
	exit 0 ;
fi

echo "dn: resourceName=$RESNAME,$RESOURCES_BASEDN" > $TMPDIF
echo "objectClass: top" >> $TMPDIF
echo "objectClass: OXResourceObject" >> $TMPDIF
echo "resourceName: $RESNAME" >> $TMPDIF
echo "resourceAvailable: $RESAV" >> $TMPDIF

LDAP_INSERT=`$LDAPADD_BIN $LDAPCONN -f $TMPDIF 2>&1 | $GREP_BIN ldap_add | $AWK_BIN -F'(' {'print $2'} | $AWK_BIN -F')' {'print $1'}`

esc=`echo -en "\033"`
warn="${esc}[1;31m"
done="${esc}[1;32m"
info="${esc}[1;33m"

case "$LDAP_INSERT" in
    
    "")
	echo "${done}Success!"
	;;
    21)
	echo "${warn}invalid per syntax"
	;;
    34)
	echo "${warn}invalid DN"
	;;
    68)
	echo "${info}entry Already exists!"
	;;
    *)
	echo "${warn}Undefined ERROR - LDAP CODE $LDAP_INSERT"
	echo "${warn}See LDAP Log for Details!"
esac
echo -en "${esc}[m\017"
echo -n ""

rm $TMPDIF
