#!/bin/bash
#
# (c) 2004-2100 ;)
# Author: cutmasta AT netline-is D0T de
# 
#
# This Script modifys User Attribs.
#
#
# 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 ;;

      --attrib)
	  ac_prev=attrib ;;
      --attrib=*) 
	  ATTRIB=$ac_optarg ;;

      --value)
	  ac_prev=value ;;
      --value=*) 
	  VALUE=$ac_optarg ;;
      
      --file)
	  ac_prec=file ;;
      --file=*)
	  FILE=$ac_optarg ;;
     

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

Usage: $0 [Options]

Options:

  --username=NAME              The Username - eg. john
                          
  --attrib=NAME                The Attribute - eg. givenName

  --value=VALUE                The new Value of the Attribute - eg. Miller

  --file=FILE                  If given, custom ldif File is used for modifying! - eg. /home/f00/bar.ldif
                               See ldapmodify for Details!

EOF

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

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

if [ "$FILE" != "" ]
    then
    
    if [ -f $FILE ]
	
	then
	echo "Using $FILE for modifying."
	LDAP_MOD=`$LDAPMODIFY_BIN $LDAPCONN -f $FILE 2>&1 | $GREP_BIN ldap_modify | $AWK_BIN -F'(' {'print $2'} | $AWK_BIN -F')' {'print $1'}`
	
    else
	
	echo "${warn}File does not exists"
	echo -en "${esc}[m\017"
	echo -n ""
	exit 0
	
    fi
    
else
    
    ERROR=
    
    if [ "$USERNAME" = "" ]
	then
        ERROR="y"
        echo "Specify an Username!"
    fi
    
    if [ "$ATTRIB" = "" ]
	then
        ERROR="y"
        echo "Specify an Attribute!"
    fi
    
    if [ "$VALUE" = "" ]
	then
        ERROR="y"
        echo "Specify a Value!"
    fi
    
    
    if [ "$ERROR" = "y" ]
	then 
	echo "Please provide all needed Parameters!"
	echo "Try $0 --help"
	exit 0 ;
    fi

    
    
    echo "dn: uid=$USERNAME,$USER_BASEDN" > $TMPDIF
    echo "changetype: modify" >> $TMPDIF
    echo "replace: $ATTRIB" >> $TMPDIF
    echo "$ATTRIB: $VALUE" >> $TMPDIF
    
    LDAP_MOD=`$LDAPMODIFY_BIN $LDAPCONN -f $TMPDIF 2>&1 | $GREP_BIN ldap_modify | $AWK_BIN -F'(' {'print $2'} | $AWK_BIN -F')' {'print $1'}`
    
    rm $TMPDIF

fi

case "$LDAP_MOD" in
    
    "")
	echo "${done}LDAP Success!"
	;;
    17)
	echo "${warn}attribute type undefined"
	;;
    21)
	echo "${warn}invalid per syntax"
	;;
    34)
	echo "${warn}invalid DN"
	;;
    *)
	echo "${warn}Undefined ERROR - LDAP CODE $LDAP_INSERT"
	echo "${warn}See Server Log for Details!"
esac
