#!/opt/tarantella/bin/bin/ttatcl 
#TCLX_LIBRARY=/opt/tarantella/bin/lib/tcl7.5
#export TCLX_LIBRARY

set ouBase "o=gestion.rima.telefonica.es"

set ttaCmd "/opt/tarantella/bin/tarantella object script"
set scrOuFile "/tmp/tta.scriptou"
set scrFile "/tmp/tta.script"
set scrOutFile "/tmp/tta.script.out"

set ouList {}
set cnList {}


proc getDS {ou} {
     global ouList cnList ttaCmd  scrFile scrOutFile scrOuFile fptta
     
     # Condition to stop recursive prozessing during developing
     #if {[llength $ouList] > 20 } { return }
     
     echo "get ou=[llength $ou] ouList=[llength $ouList] cnList=[llength $cnList]"
     echo "Scanning  $ou ... "
     
     set newScript ""
     foreach el $ou {
             system " echo 'list_contents --name \"$el\"\n '  >$scrOuFile "
     }
#     system " echo \" $newScript \" >$scrOuFile "
     
     set ouSub {}
     set ou {}
     catch { eval "exec cat $scrOuFile | $ttaCmd" } ttaResult
     foreach el [split $ttaResult \n] {
		# Contents of .../_ens/o=organization:
		if [regexp {Contents of .*_ens/(.*):} $el all ou]  { }
		if [regexp {^ou=} $el] { 
			lappend ouList $ou/$el 
			lappend ouSub $ou/$el
                        puts $fptta  "list_attributes --name \"$ou/$el\""
                        getDS $ou/$el
		}
		if [regexp {^cn=} $el] {
		        lappend cnList $ou/$el
			lappend ouSub $ou/$el
                        puts $fptta  "list_attributes --name \"$ou/$el\""
                        getDS $ou/$el
		}
     }
#     if [llength $ouSub] {getDS $ouSub}
}

system "> $scrFile"
set fptta [open $scrFile w]

getDS  $ouBase

close $fptta
echo --oOo--
echo "get ouList=[llength $ouList] cnList=[llength $cnList]"

system "cat $scrFile | $ttaCmd > tta.DataStore"
#system "rm -f $scrOuFile $scrOutFile"
#system "rm -f $scrFile $scrOuFile $scrOutFile"

