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

set file $argv
set ttaCmd /opt/tarantella/bin/tarantella

set attribute ""
set tfn ""
set object ""

set ouFile tta.orgUnits
set cnFile tta.containers
set personFile tta.persons
set serverFile tta.server
set applFile tta.applications
set groupFile tta.group
set linkFile tta.link


# We are opening all neccessery files

set ouFileFp [open $ouFile w]
set cnFileFp [open $cnFile w]
set personFileFp [open $personFile w]
set serverFileFp [open $serverFile w]
set appFileFp [open $applFile w]
set groupFileFp [open $groupFile w]
set linkFileFp [open $linkFile w]

for_file line $file {

        regsub -all {""} $line {"} line

	if [regexp {Attributes for .*/_ens/(.*):} $line all newtfn ] {

		if [clength $tfn] { 
		  set fp $appFileFp
		  if [cequal $object new_group] { set fp $groupFileFp }
		  if [cequal $object new_host] { set fp $serverFileFp }
		  if [cequal $object new_orgunit] { set fp $ouFileFp }
		  if [cequal $object new_person] { set fp $personFileFp }

		  if [cequal $object new_container] { 
                      set fp $cnFileFp 
		      puts $fp "$object --name \"$tfn\" "
                  } else {
		      puts $fp "$object --name \"$tfn\" $attribute"
                  }
		}
		set tfn $newtfn
		set attribute ""
		continue

	}

	
	if [regexp {scottaWebtopContents: (.*)} $line all linkList ] {
		# add_links
		  regsub -all {,} $linkList { } linkList
		  puts $linkFileFp "add_link --name \"$tfn\" --link $linkList"
		 continue
	}
	if [regexp {links: (.*)} $line all linkList ] {
		# add_links
		  regsub -all {,} $linkList { } linkList
		  puts $linkFileFp "add_link --name \"$tfn\" --link $linkList"
		 continue
	}
	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 )"dd
		switch  $class {
                     organizationalunit,scottaauxorganizationalunit,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}
		}


		continue
	}
	if [regexp {(.*): (.*)} $line all attr value ] {
		# Ignore
		if [regexp {^windowclose$} $attr ] { continue }
		if [regexp {^share$} $attr ] { continue }
		if [regexp {^passtype$} $attr ] { continue }
		if [regexp {^Organizational Unit$} $attr ] { continue }

		# Modify

		if [regexp {^member$} $attr ] { regsub -all {,} $value { } value }
		if [regexp {^appserv$} $attr ] { regsub -all {,} $value { } value }
		if [regexp {^Name$} $attr ] 	 { set attr description }
		if [regexp {^scottaInheritWebtop$} $attr ] 	 { set attr inherit }

		    append attribute "--$attr $value "
         }


}
if [clength $tfn] {
                  set fp $appFileFp
                  if [cequal $object new_group] { set fp $groupFileFp }
                  if [cequal $object new_host] { set fp $serverFileFp }
                  if [cequal $object new_orgunit] { set fp $ouFileFp }
                  if [cequal $object new_person] { set fp $personFileFp }
 
                  if [cequal $object new_container] {
                      set fp $cnFileFp
                      puts $fp "$object --name \"$tfn\" "
                  } else {
                      puts $fp "$object --name \"$tfn\" $attribute"
                  }
}
close $ouFileFp 
close $cnFileFp 
close $personFileFp 
close $serverFileFp 
close $appFileFp 
close $groupFileFp 
close $linkFileFp 
