#!/bin/bash
#
# (c) 2004-2100 ;)
# Author: cutmasta AT netline-is D0T de
# "Jus7 s0m3 l4m3 b4sh Pr0n"
# 
#
# This Script adds a a OXResouce to a OXResourceGroup
#
#
#
# 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
      
      --res)
	  ac_prev=res ;;
      --res=*) 
	  RES=$ac_optarg ;;

      --group)
	  ac_prev=group ;;
      --group=*)
	  GROUP=$ac_optarg ;;


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

Usage: $0 [Options]

Options:

  --res=NAME                 The Resourcename - eg. car1

  --group=NAME               The Resource Groupname - eg. companys-cars

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

ERROR=

if [ "$RES" = "" ]
then
        ERROR="y"
        echo "Specify a Resourcename!"
fi

if [ "$GROUP" = "" ]
then
        ERROR="y"
        echo "Specify a Resource Groupname!"
fi

if [ "$ERROR" = "y" ]
    then
    echo "Provide all Parameter."
    echo "See $0 --help for details."
    exit 0
fi

CURRENT_GID=`$LDAPSEARCH_BIN $LDAPCONN -b $RESOURCE_GROUPS_BASEDN "(resourceGroupName=$RESGROUP)" resourceGroupName | $GREP_BIN resourceGroupName | $SED_BIN -e 's/^resourceGroupName://' | $SORT_BIN -nr | $HEAD_BIN -n 1 | $AWK_BIN {'print $1'}`


########## old,
#CURRENT_GID=`$LDAPSEARCH_BIN $LDAPCONN -b $RESOURCE_GROUPS_BASEDN "(resourceGroupName=$GROUP)" resourceGroupName | $GREP_BIN resourceGroupName | $SED_BIN -e 's/^resourceGroupName://' | $SORT_BIN -nr | $HEAD_BIN -n 3 | $AWK_BIN -Fdn: {'print $2'}`

RES_EXIST=`$LDAPSEARCH_BIN $LDAPCONN -b $RESOURCES_BASEDN "(resourceName=$RES)" resourceName | $GREP_BIN resourceName | $SED_BIN -e 's/^resourceName://' | $SORT_BIN -nr | $GREP_BIN resourceName=$RES,`

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

if [ "$CURRENT_GID" = "" ]
then
    echo "${info}Group not found!"
    echo "Exiting"
    echo -en "${esc}[m\017"
    echo -n ""
    exit 0
fi

if [ "$RES_EXIST" = "" ]
then
    echo "${info}Resource not found!"
    echo "Exiting"
    echo -en "${esc}[m\017"
    echo -n ""
    exit 0
fi

##here we add the resource to the group
echo "dn: resourceGroupName=$GROUP,$RESOURCE_GROUPS_BASEDN" > $TMPDIF
echo "changetype: modify" >> $TMPDIF
echo "add: resourceGroupMember" >> $TMPDIF
echo "resourceGroupMember: $RES" >> $TMPDIF

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

rm $TMPDIF

LDAP_ERROR="y"

case "$LDAP_MOD_RESGRP" in
    
    "")
	echo "${done}OK"
	echo -en "${esc}[m\017"
	echo -n ""
	;;
    17)
	echo "${warn}attribute type undefined"	
	;;
    20)
	echo "${info}$RES is already in $GROUP"
	;;
    34)
	echo "${warn}invalid DN"
	
	;;
    *)
	echo "${warn}Undefined ERROR - LDAP CODE $LDAP_INSERT"
	echo "${warn}See Server Log for Details!"
	
esac
