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

set file tta.DataStore
set shFile restore.sh
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]

system ">$shFile"

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 ] {
		set class [lindex [split $class ,] 0]

		switch  $class {
		 organization { set object new_orgunit }
		 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 $tfn; 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 "
         }


}
close $ouFileFp 
close $cnFileFp 
close $personFileFp 
close $serverFileFp 
close $appFileFp 
close $groupFileFp 
close $linkFileFp 


set fp [open $shFile w]
puts $fp "#!/bin/sh "
puts $fp "cat $ouFile | $ttaCmd object script"
puts $fp "cat $cnFile | $ttaCmd object script"
puts $fp "cat $personFile | $ttaCmd object script"
puts $fp "cat $serverFile | $ttaCmd object script"
puts $fp "cat $applFile | $ttaCmd object script"
puts $fp "cat $groupFile | $ttaCmd object script"
puts $fp "cat $linkFile | $ttaCmd object script"
close $fp
system "chmod 755 $shFile"
#system "cat $shFile"
echo "Run $shFile to Restore ENS !!!"
