#!/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 scrAttFile "/tmp/tta.scriptAtt"
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 scrAttFile
     
     # Condition to stop recursive prozessing during developing
     #if {[llength $ouList] > 20 } { return }
     
#     echo "get ou=[llength $ou] ouList=[llength $ouList] cnList=[llength $cnList]"
     echo "get ouList=[llength $ouList] cnList=[llength $cnList]"
     echo "Scanning  $ou ... " 
     
     system "echo 'list_contents --name \"$ou\"\n' >$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] {
                   set newScript "list_attributes --name \"$ou/$el\"\n"
                   set object ""
                   system "echo 'list_attributes --name \"$ou/$el\"\n' >$scrAttFile"
                   eval "exec cat $scrAttFile | $ttaCmd >$scrOutFile" 
                   for_file line $scrOutFile {
                      regsub -all {""} $line {"} line
	              if [regexp -nocase {objectclass: (.*)} $line all class ] {
		        set class [lindex [split $class ,] 0]
		        switch  $class {
		             organizationalunit { set object new_orgunit }
		             container { set object new_container }
         		     inetorgperson { set object new_person}
	         	     scottagroupofnames { set object new_group}
		             scottahtmldocument { set object new_doc}
		             scottaxapplication { set object new_xapp}
		             scottagroupofnames { set object new_group}
		             scottacharacterapplication { set object new_charapp}
		             scottawindowsapplication { set object new_windowsapp }
		             scottaauxhost { set object new_host }
		             device { set object new_host }
		             default { set object undef ; echo UNDEF \"$class\" in $line; exit}
		        }
                      }
	            }
                    puts $fptta  "list_attributes --name \"$ou/$el\""
		    if [cequal $object new_container] { 
                        lappend ouSub "$ou/$el"
                        getDS "$ou/$el"
                    } else {
                        echo $el
		        lappend cnList "$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 $scrFile $scrOuFile $scrOutFile $scrAttFile"

