#!/opt/tarantella/bin/bin/ttatcl 
# STTA Rev. 1.3 Copyright (c) 2001-2003 Jesús Pérez Lorenzo  --- license GNU GPL

set ouBase [lindex $argv 0]

if { ![info exists env(TTA_OBJSCRIPT_CMD)] } {
   echo "TTA_OBJSCRIPT_CMD not set in environment !!!"
   exit 2
}
set ttaCmd $env(TTA_OBJSCRIPT_CMD)
if { [info exists env(STTA_WKDIR)] } {
    set wkDir $env(STTA_WKDIR)
} else {
    set wkDir "/tmp"
}
set scrOuFile $wkDir
set scrAttFile $wkDir
set scrFile $wkDir 
set scrOutFile $wkDir
append scrOuFile "/tta.scriptou"
append scrAttFile "/tta.scriptAtt"
append scrFile "/tta.script"
append scrOutFile "/tta.script.out"
echo " $ttaCmd ..."
echo " $scrOuFile ..."
echo " $scrFile ..."
echo " $scrOutFile ..."
exit

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 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"
                   system "cat $scrAttFile | $ttaCmd >$scrOutFile" 
                   for_file line $scrOutFile {
                      regsub -all {""} $line {"} line
	              if [regexp -nocase {objectclass: (.*)} $line all class ] {
                         # if split is need for some reason ...
		         # set class0 [lindex [split $class ,] 0]
		         # set class1 [lindex [split $class ,] 1]
		         # set class2 [lindex [split $class ,] 2]
		         # set class3 [lindex [split $class ,] 3]
                         # echo "$class ( $class0 - $class1 - $class2 -- $class3 )"
		         switch  $class {
		             organizationalunit,top { set object new_orgunit }
		             container,top { set object new_container }
                             inetorgperson,scottaauxperson,organizationalperson,person,top { set object new_person}
		             scottahtmldocument,top { set object new_doc}
		             scottaxapplication,scottaapplication,top { set object new_xapp}
		             scottagroupofnames,top { set object new_group}
		             scottacharacterapplication,scottaapplication,top { set object new_charapp}
                             scottawindowsapplication,scottaapplication,top { set object new_windowsapp }
                             scottawindowsapplication,scottaxapplication,scottaapplication,top { set object new_windowsapp }
		             device,scottaauxhost,top { 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]"

set ttaDataStore [lindex $argv 1]
system "cat $scrFile | $ttaCmd > $ttaDataStore "
# let us put endfile newline
system "echo >> $ttaDataStore "
system "rm -f $scrFile $scrOuFile $scrOutFile $scrAttFile"

