#!/bin/bash
#
# (c) 2004-2100 ;)
# Author: cutmasta AT netline-is D0T de
# 
#
# This Script dels OX User from the LDAP!
# TODO : DELETE personal adressbook completely if he has entries in that!
#
#
# 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
      
      --username)
	  ac_prev=username ;;
      --username=*) 
	  USERNAME=$ac_optarg ;;

      

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

Usage: $0 [Options]

Options:
  --username=NAME              The Username - eg. john

EOF

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

ERROR=

if [ "$USERNAME" = "" ]
then
        ERROR="y"
        echo "Specify an Username!"
fi


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

#LDAP_DELETE_ADR=`$LDAPDELETE_BIN $LDAPCONN "ou=addr,uid=$USERNAME,$USER_BASEDN" 2>&1 | $GREP_BIN Delete | $AWK_BIN -F'(' {'print $2'} | $AWK_BIN -F')' {'print $1'}`

## DELETE RECURSIVE ALL USER STUFF FROM LDAP
LDAP_DELETE=`$LDAPDELETE_BIN $LDAPCONN -r "uid=$USERNAME,$USER_BASEDN" 2>&1 | $GREP_BIN Delete | $AWK_BIN -F'(' {'print $2'} | $AWK_BIN -F')' {'print $1'}`


# delete the user from the adressadmins
echo "dn: $GLOBAL_ADDRESSBOOK_ADMINSDN " > $TMPDIF
echo "changetype: modify" >> $TMPDIF
echo "delete: member" >> $TMPDIF
echo "member: uid=$USERNAME,$USER_BASEDN" >> $TMPDIF

LDAP_DELETE_ADDRESSADMIN=`$LDAPMODIFY_BIN $LDAPCONN -f $TMPDIF 2>&1 | $GREP_BIN ldap_modify | $AWK_BIN -F'(' {'print $2'} | $AWK_BIN -F')' {'print $1'}`
rm $TMPDIF

OX_OBJECTS_DELETE=`/usr/lib/j2se1.4-sun/bin/java -Dopenexchange.propfile=/etc/open-xchange/groupware/system.properties -classpath /usr/share/java/open-xchange/intranet.jar:/usr/share/java/open-xchange/comfiretools.jar:/usr/share/java/open-xchange/nas.jar:/usr/share/java/postgresql74-jdbc/pg74-jdbc3.jar com.openexchange.groupware.deleteUserGroups deleteUser $USERNAME`

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

case "$LDAP_DELETE" in
    
    "")
	echo "${done}LDAP Success!"
	SQL_INSERT=`$SQL_BIN -h $DEFAULT_SQL_HOST -U $DEFAULT_SQL_USER -d $DEFAULT_SQL_DB -X -c "DELETE FROM $RIGHTS_TABLE WHERE login LIKE '$USERNAME'" | $AWK_BIN {'print $1'}`
	if [ "$SQL_INSERT" = "DELETE" ]
	then
	    echo "${done}SQL Success"
	else
	    echo "${warn}SQL Error"
	    exit 0
	fi
	;;
    32)
	echo "${info}No such User"
	;;
    34)
	echo "${warn}invalid DN"
	;;
    *)
	echo "${warn}Undefined ERROR - LDAP CODE $LDAP_INSERT"
	echo "${warn}See LDAP Log for Details!"
esac

echo -en "${esc}[m\017"
echo -n ""
