#
# Module SaveFile.tcl
#
# $Log: SaveFile.tcl,v $
# Revision 1.4  2006/10/26 09:39:17  vfom
# Added code for netstate server query
#
# Revision 1.3  2006/10/19 08:40:26  vfom
#
# Revision 1.2  2006/10/18 05:04:03  vfom
# Added code for including prefs and internal vars to config
#
# Revision 1.1.1.1  2006/09/21 10:57:02  vfom
# Loading TkNetmon to CVS
#
#
# This proc saves current map to file, parsable by NETMONd
#    Additional config directives hided from NETMONd by 
#    masqueraded as comments.
#
#  Resulted config file can be parsed by LoadFile.
#
proc SaveFile { f } {
  global activeFile

    if { $f == "noname.conf" } {
	set f [ tk_getSaveFile -title [msgcat::mc "Save As"]]
    }
    if { $f != "" } {
	set activeFile $f
    } else { 
	return 
    }
  uplevel #0 { 
   
    if { [ catch { open $activeFile w } fd ] } {
	error "$fd"
	return
    }
    alterFileHistory $activeFile

    set t [clock format [clock seconds]]
    set u $tcl_platform(user)
    if { $tcl_platform(platform) =="unix" } {
        set uname [open "|uname -mnrs" r]
        set h [read -nonewline $uname] ; close $uname
    } else {
	set h "localhost"
    }
    puts $fd "#  This is NETMONd & $appName configuration file"
    puts $fd "#"
    puts $fd "#  Created by $u at $t with $appName-$appVersion"
    puts $fd "#          on $h"
    puts $fd "#"
    puts $fd "RootDir      \"$rootDir\""
    puts $fd "Polling      $nodePoll"
    puts $fd "Timeout      $nodeTmo"
    puts $fd "Retries      $nodeRetr"
    puts $fd "TimeFmt      \"$timeFmt\""
    puts $fd "Saving       $dataSaveInterval"
    if {$userName != "" } {
	puts $fd "UserName     \"$userName\""
    }
    if {$groupName != "" } {
	puts $fd "GroupName    \"$groupName\""
    }
    if {$chRootDir != "" } {
	puts $fd "ChRootDir       \"$chRootDir\""
    }
    
    foreach i $::ifGroupList {
	set def [join $::ifGroupDefinition($i) ]
	puts $fd "Group \"$i\" { $def }"
    }        
    puts $fd "Port $::netStateServerPort {"
    puts $fd "\tTimeout $::netStateServerTimeout"
    foreach i $::netStateServerAccess {
	set x [split $i ":"]
	set perm [lindex $x 0]
	set rule [lindex $x 1]
	if { [string equal $perm permit] } {
	    puts $fd "\tPermit \"$rule\""
	} elseif { [string equal $perm deny] } {
	    puts $fd "\tDeny \"$rule\""
	}
    }    
    puts $fd "}"
    set allSnmpVars [list]
    foreach sublist $snmpVarsList {
	set nm [lindex $sublist 0 ]
	set oid [lindex $sublist 1 ]
	puts $fd "\$$nm\t$oid"
	lappend allSnmpVars $nm
    }
    foreach pmethod $pollMethods {
	if { [lsearch -exact $pollMethodsInternal $pmethod] >=0 } { continue }
	puts $fd "Method \"$pmethod\" {$pollMBodyArray($pmethod)}"
    }
    foreach smethod $saveMethods {
	if { [lsearch -exact $saveMethodsInternal $smethod] >=0 } { continue }
	puts $fd "Save \"$smethod\" {$saveMBodyArray($smethod)}"
    }
    foreach tmethod $trapMethods {
	if { [lsearch -exact $trapMethodsInternal $tmethod] >=0 } { continue }
	set Ents [lindex $trapMBodyArray($tmethod) 0 ]
	set Spc  [lindex $trapMBodyArray($tmethod) 1 ]
	set tbody "\n\tEnterprise $Ents\n\tSpecific   $Spc\n\tCommunity \"\$1\"\n"
	puts $fd "Trap \"$tmethod\" {$tbody}"
    }
    puts $fd  "SrcAddress\t\t\"$node_SrcIP\""
    puts $fd  "/*"
    puts $fd "Preferences {"    
    puts $fd "\twm geometry . \t\t\"[ wm geometry . ]\""
    puts $fd "\tset ::globalFG\t\t\"$::globalFG\""
    puts $fd "\tset ::globalBG\t\t\"$::globalBG\""
    puts $fd "\tBGImage \t\t\"$::background\""    
    puts $fd "\tset ::browserProg\t\"$::browserProg\""	
    puts $fd "\tset ::myLogFile\t\t\"$::myLogFile\""	
    puts $fd "\tset ::menuVisible\t\"$::menuVisible\""	
    puts $fd "\tset ::scrollVisible\t\"$::scrollVisible\""	
    puts $fd "\tset ::statusVisible\t\"$::statusVisible\""	
    puts $fd "\tset ::editEnable\t\t\"$::editEnable\""	
    puts $fd "\tset ::lineWidth\t\t\"$::lineWidth\""	
    puts $fd "\tset ::lineColor\t\t\"$::lineColor\""	
    puts $fd "\tset ::textAnchor\t\"$::textAnchor\""	
    puts $fd "\tset ::textJust\t\t\"$::textJust\""	
    puts $fd "\tset ::textFont\t\t\"$::textFont\""	
    puts $fd "\tset ::textFontColor\t\"$::textFontColor\""	
    puts $fd "\tset ::linkWidth\t\t\"$::linkWidth\""	
    puts $fd "\tset ::linkColor\t\t\"$::linkColor\""	
    puts $fd "\tset ::nodeIcon\t\t\"$::nodeIcon\""	
    puts $fd "\tset ::nodeUpSound\t\"$::nodeUpSound\""	
    puts $fd "\tset ::nodeDownSound\t\"$::nodeDownSound\""	
    puts $fd "\tset ::nodeFont\t\t\"$::nodeFont\""	
    puts $fd "\tset ::nodeFontColor\t\"$::nodeFontColor\""	
    puts $fd "\tset ::nodeIP\t\t\"$::nodeIP\""	
    puts $fd "\tset ::node_SrcIP\t\"$::node_SrcIP\""	
    puts $fd "\tset ::node_SnmpVer\t\"$::node_SnmpVer\""	
    puts $fd "\tset ::node_getCommunity\t\"$::node_getCommunity\""	
    puts $fd "\tset ::node_trapCommunity \"$::node_trapCommunity\""	
#   puts $fd "\tset ::nodePoll\t\t\"$::nodePoll\""	
#   puts $fd "\tset ::nodeRetr\t\t\"$::nodeRetr\""	
#   puts $fd "\tset ::nodeTmo\t\t\"$::nodeTmo\""	
#   puts $fd "\tset ::dataSaveInterval\t\t\"$::dataSaveInterval\""	
    puts $fd "\tset ::node_dblClickCmd\t\"$::node_dblClickCmd\""	
    puts $fd "\tset ::h_align\t\"$::h_align\""	
    puts $fd "\tset ::w_align\t\"$::w_align\""	
    puts $fd "\tset ::h_autoplacement\t\"$::h_autoplacement\""	
    puts $fd "\tset ::w_autoplacement\t\"$::w_autoplacement\""	
    puts $fd "\tset ::monitor_mode\t\"$::monitor_mode\""	
#   puts $fd "\tset ::rootDir\t\t\"$::root_dir\""	
    puts $fd "\tset ::netStateServer\t\"$::netStateServer\""	
#   puts $fd "\tset ::netStateServerPort\t\t\"$::netStateServerPort\""	
    puts $fd "\tset ::predIncludeFile\t\"$::predIncludeFile\""	
    puts $fd "\tset ::postIncludeFile\t\"$::postIncludeFile\""	
    puts $fd "\tset ::soundPlayCmd\t\"$::soundPlayCmd\""	
    puts $fd "\tset ::daemonRestartCmd\t\"$::daemonRestartCmd\""	
    
    if { [lindex $targetList 0] !={} } {
	set lsize [llength $targetList ]
	for {set i 0} {$i<$lsize} {incr i} {
	    set range [lindex $targetList $i]
    	    puts $fd  "\tAddTargets  \"[lindex $range 0]\" \"[lindex $range 1]\""
	}
    }
    if { [lindex $snmpList 0] !={} } {
	set lsize [llength $snmpList ]
	for {set i 0} {$i<$lsize} {incr i} {
	    set range [lindex $snmpList $i]
    	    puts $fd  "\tAddSnmpName  \"[lindex $range 0]\" \"[lindex $range 1]\""
	}
    }
    puts $fd "}\n#\n"
    puts $fd "ServiceMenus {"
    set ar_size [ array size menuConfigArray] 
    for {set i 0} {$i<$ar_size} {incr i} {
	puts $fd "	MenuItem $menuConfigArray($i)"
    }
    puts $fd "}"
    puts $fd "#"
    puts $fd "SqlDatabase {"
    puts $fd "\tset ::Sql::dbmethod \"$::Sql::dbmethod\""    
    puts $fd "\tset ::Sql::dbhost   \"$::Sql::dbhost\""    
    puts $fd "\tset ::Sql::dbuser   \"$::Sql::dbuser\""    
    puts $fd "\tset ::Sql::dbpwd    \"$::Sql::dbpwd\""    
    puts $fd "\tset ::Sql::dbname   \"$::Sql::dbname\""
    puts $fd "}"
    puts $fd "EventsLog {"
    if { [winfo exist .eventsLog ] } {
	set ::evLog::lastGeometry [ wm geometry .eventsLog  ]
    } 
    puts $fd  "\tset ::evLog::lastGeometry \"$::evLog::lastGeometry\""    
    if { [winfo exist .evLogFilters ] } {
	set ::evLog::lastGeometryFilters [ wm geometry .evLogFilters ]
    } 
    puts $fd  "\tset ::evLog::lastGeometryFilters \"$::evLog::lastGeometryFilters\""    
    if { [winfo exist .evLogFilters.viewFrame.tbl ] } {
	set ::evLog::tbl_cols_width  [.eventsLog.viewFrame.tbl width ]
    } 
    puts $fd  "\tset ::evLog::tbl_cols_width \"$::evLog::tbl_cols_width\""    
    puts $fd  "\tset ::evLog::direction \"$::evLog::direction\""    
    puts $fd  "\tset ::evLog::find \"$::evLog::find\""    
    puts $fd  "\tset ::evLog::scope_flt \"$::evLog::scope_flt\""    
    puts $fd  "\tset ::evLog::start_t \"$::evLog::start_t\""    
    puts $fd  "\tset ::evLog::stop_t \"$::evLog::stop_t\""    
    puts $fd  "\tset ::evLog::objects_flt_flag \"$::evLog::objects_flt_flag\""    
    puts $fd  "\tset ::evLog::objects_flt \"$::evLog::objects_flt\""    
    puts $fd  "\tset ::evLog::bottom_anchor \"$::evLog::bottom_anchor\""    
    puts $fd  "\tarray set ::evLog::cat_flags {[array get ::evLog::cat_flags]}"        
    puts $fd "}"
    puts $fd  "*/"
    puts $fd  "#"
    if { $::predIncludeFile!="" } {
	puts $fd "%include \"$::predIncludeFile\""
    } 
    puts $fd  "#"
    foreach i  [ .canvas find all ] {
	switch [ .canvas type $i  ] {
	    window {
		set node      [.canvas itemcget $i -window ]
		set icon      [$node cget -image]
		set coords    [.canvas coords $node.icon]
		set lup       [.canvas find withtag $node.up ]
		set font      [.canvas itemcget $lup -font ]
		set fontColor [.canvas itemcget $lup -fill ]
		puts $fd "#"
		puts $fd "Object \"$node_name($node)\" { "
		puts $fd "\tAddress      \"$node_ip($node)\" "
		if {[string compare $node_srcip($node) $node_SrcIP ]} {
		  puts $fd "\tSrcAddress   \"$node_srcip($node)\""	
		}
		if { $node_poll($node)!=$nodePoll } { puts $fd "\tPolling\t$node_poll($node)"}		
		if { $node_tmo($node)!=$nodeTmo } { puts $fd "\tTimeout\t$node_tmo($node)" }
		if { $node_retr($node)!=$nodeRetr } { puts $fd "\tRetries\t$node_retr($node)" }
		if { $node_dataSaveInterval($node)!=$dataSaveInterval } { 
		    puts $fd "\tSaving\t$node_dataSaveInterval($node)" 
		}
		if { $node_datadir($node)!="" } { puts $fd "\tDatadir\t\"$node_datadir($node)\"" }
		#
		foreach sublist $node_snmpvarslist($node) {
		    set t0 [lindex $sublist 0]
		    set t1 [lindex $sublist 1]
		    set t2 [lindex $sublist 2]
		    if { $t2 != "" } {
			puts $fd "\t\$$t0 $t1 = \"$t2\""			
		    } else {
			puts $fd "\t\$$t0 $t1"			
		    }
		}
		foreach t $node_snmptrapslist($node) {
		    if { [lsearch $::trapMethodsInternal $t] <0 } {
			puts $fd "\tTrap\t\"$t\"\t\"$node_snmptrap($node)\"" 
		    } else {
			puts $fd "\tTrap\t$t\t\"$node_snmptrap($node)\"" 
		    }
		}
		foreach sublist $node_methodslist($node) {
		    set t0 [lindex $sublist 0]
		    set t1 [lindex $sublist 1]
		    set t2 [lindex $sublist 2]
		    # regsub -all -- {([\w\.]+)} $t2 {"\1"} t3
		    if {[string equal $t0 Method ] && [lsearch $pollMethods $t1] >=0  }  {
			if {[lsearch $pollMethodsInternal $t1] >=0 }  {
			    if { [string equal $t1 snmp] } {
				puts $fd "\t$t0\t$t1\t$node_snmpver($node) $t2 {"
				foreach vlist $node_snmpvarslist($node) {
				    set vname [lindex $vlist 0]
				    set void [lindex $vlist 1]
				    if { $void !="" || [ lsearch $allSnmpVars $vname ] >=0 } {
					puts $fd "\t\t\$$vname "
				    }
				}
				puts $fd "\t}"
			    } else {
				if { [ string equal $t1 router ] } {
				    puts $fd "\t$t0\t$t1\t$node_snmpver($node) $t2"
				} else { 
				    puts $fd "\t$t0\t$t1\t$t2"
				}
			    }
			} else {
			    puts $fd "\t$t0\t\"$t1\"\t$t2"
			}
		    }
		    if {[string equal $t0 Save ] && [lsearch $saveMethods $t1] >=0  }  {
			if {[lsearch $saveMethodsInternal $t1] >=0 }  {
			    puts $fd "\t$t0\t$t1\t$t2"
			}  else {
			    puts $fd "\t$t0\t\"$t1\"\t$t2"
			}
		    }
		}
		#
		foreach sublist $node_iflist($node) {
		    set ifname [lindex $sublist 0]
		    set ifKey  [lindex $sublist 1]
		    set grpMod  [lindex $ifname 0 ]
		    if { [string match $grpMod "Group"] } {
			set ifname [join [ lrange $ifname 1 end ]]
			if { [lsearch $::ifGroupList $ifname ] <0 } {
			    puts stderr "SAVEFILE: IfGroup \"$ifname\" described in Object not in list of known groups. Skipping."
			    continue
			}
		    } else { 
			set grpMod "" 
		    }
    		    puts $fd "\tInterface $grpMod \"$ifname\" { " 
		    if { $if_descr($ifKey) !=""} {
			puts $fd "\t\tDescription \"$if_descr($ifKey)\""
		    }
		    if { $if_datadir($ifKey) !=""} {
			puts $fd "\t\tDatadir \"$if_datadir($ifKey)\""
		    }
		    if { $if_bandwidth($ifKey) !=""} {
			puts $fd "\t\tBandwidth $if_bandwidth($ifKey)"
		    }
		    foreach sublist2 $if_methodslist($ifKey) {
			set t0 [lindex $sublist2 0]
			set t1 [lindex $sublist2 1]
			set t2 [lindex $sublist2 2]
			# regsub -all -- {([\w\.]+)} $t2 {"\1"} t3
			if {[lsearch $saveMethods $t1] >=0 }  {
			    if {[lsearch $saveMethodsInternal $t1] >=0 }  {
				puts $fd "\t\t$t0\t$t1\t$t2"
			    }  else {
				puts $fd "\t\t$t0\t\"$t1\"\t$t2"
			    }
			}
		    }
		    puts $fd "\t}"
		    
		}		
		foreach sublist $node_srvlist($node) {
		    set srvname [lindex $sublist 0]
		    set srvKey  [lindex $sublist 1]
    		    puts $fd "\tService \"$srvname\" { "
		    if { $srv_descr($srvKey) !=""} {
			puts $fd "\t\tDescription \"$srv_descr($srvKey)\""
		    }
		    if { $srv_datadir($srvKey) !=""} {
			puts $fd "\t\tDatadir \"$srv_datadir($srvKey)\""
		    }
		    foreach sublist2 $srv_snmpvarslist($srvKey) {
			set t0 [lindex $sublist2 0]
			set t1 [lindex $sublist2 1]
			set t2 [lindex $sublist2 2]
			if { $t2 == "" } {
			    puts $fd "\t\t\$$t0 $t1"			
			} else {
			    puts $fd "\t\t\$$t0 $t1 = $t2"			
			}
		    }
		    foreach sublist2 $srv_methodslist($srvKey) {
			set t0 [lindex $sublist2 0]
			set t1 [lindex $sublist2 1]
			set t2 [lindex $sublist2 2]
			# regsub -all -- {([\w\.]+)} $t2 {"\1"} t3
			if {[string equal $t0 Save]} {
			    if {[lsearch $saveMethods $t1] >=0 }  {
				if {[lsearch $saveMethodsInternal $t1] >=0 }  {
				    puts $fd "\t\t$t0\t$t1\t$t2"
				}  else {
				    puts $fd "\t\t$t0\t\"$t1\"\t$t2"
				}
			    }
			} elseif { [string equal $t0 Method] } {
			    if {[lsearch $pollMethods $t1] >=0 }  {
				if {[lsearch $pollMethodsInternal $t1] >=0 }  {
				    puts $fd "\t\t$t0\t$t1\t$t2"
				}  else {
				    puts $fd "\t\t$t0\t\"$t1\"\t$t2"
				}
			    }
			}
		    }
		    puts $fd "\t}"
		}		
		foreach sublist $node_bgplist($node) {
		    set asn     [lindex $sublist 0]
		    set bgpKey  [lindex $sublist 1]
    		    puts $fd "\tBgp $asn { "
		    if { $bgp_descr($bgpKey) !=""} {
			puts $fd "\t\tDescription \"$bgp_descr($bgpKey)\""
		    }
		    if { $bgp_datadir($bgpKey) !=""} {
			puts $fd "\t\tDatadir \"$bgp_datadir($bgpKey)\""
		    }
		    foreach p $bgp_peerslist($bgpKey) {
			puts $fd "\t\tPeer \"$p\""		    
		    }
		    foreach sublist2 $bgp_methodslist($bgpKey) {
			set t0 [lindex $sublist2 0]
			set t1 [lindex $sublist2 1]
			set t2 [lindex $sublist2 2]
			# regsub -all -- {([\w\.]+)} $t2 {"\1"} t3
			if {[lsearch $saveMethods $t1] >=0 }  {
			    if {[lsearch $saveMethodsInternal $t1] >=0 }  {
				puts $fd "\t\t$t0\t$t1\t$t2"
			    }  else {
				puts $fd "\t\t$t0\t\"$t1\"\t$t2"
			    }
			}
		    }
		    puts $fd "\t}"
		}		
		puts $fd "\t/*"
		if {[string compare $node_action($node) $node_dblClickCmd ]} {
		  puts $fd "\tNodeAction \"$node_action($node)\""	
		}
		if {[string compare $node_snmpver($node) $node_SnmpVer ]} {
		  puts $fd "\tSnmpVersion \"$node_snmpver($node)\""	
		}
		if {[string compare $node_snmpget($node) $node_getCommunity ]} {
		  puts $fd "\tGetCommunity \"$node_snmpget($node)\""	
		}
		if {[string compare $node_snmptrap($node) $node_trapCommunity ]} {
		  puts $fd "\tTrapCommunity \"$node_snmptrap($node)\""	
		}
		if {[string compare $node_menu($node) "Default" ]} {
		  puts $fd "\tMenu\t\"$node_menu($node)\""	
#		}
#		if {$node_soundup($node) !="" } {
#		  puts $fd "\tUpSound\t\"$node_soundup($node)\""	
#		}
#		if {$node_sounddown($node) !="" } {
#		  puts $fd "\tDownSound\t\"$node_sounddown($node)\""	
#		}
		puts $fd "\tCoords       $coords"
		puts $fd "\tIcon         $::arImages($icon)"
		puts $fd "\tFont         {$font}"
		puts $fd "\tFontColor    {$fontColor}"
		puts $fd "\tDescription  {$node_descr($node)}"
		puts $fd "\t*/"
		puts $fd "}"
	    }
	    text {
		set tags [.canvas itemcget $i -tags]
		if {$tags==""} {
		    set coords      [.canvas coords $i]
		    set anchor      [.canvas itemcget $i -anchor]
		    set font   	    [.canvas itemcget $i -font]
		    set fontColor   [.canvas itemcget $i -fill]
		    set justify     [.canvas itemcget $i -justify ]
		    set body        [.canvas itemcget $i -text ]
		    # regsub  -all  \n [.canvas itemcget $i -text ] \\n body
		    puts $fd "/*"
    		    puts $fd "Text {"
		    puts $fd "\tCoords\t$coords"
		    puts $fd "\tAnchor\t$anchor"
		    puts $fd "\tFont\t{$font}"
		    puts $fd "\tFontColor\t{$fontColor}"
		    puts $fd "\tAjustment\t$justify"
		    puts $fd "\tBody\t{$body}"
		    puts $fd "}"
		    puts $fd "*/"
		}
	    }
	    line {
		set tags [.canvas itemcget $i -tags]
		if {$tags==""} {
		    set coords     [.canvas coords $i]
		    set width      [.canvas itemcget $i -width]
		    set color      [.canvas itemcget $i -fill]
		    puts $fd "#"
		    puts $fd "/*"
    		    puts $fd "Line {"
		    puts $fd "\tCoords\t$coords"
		    puts $fd "\tColor\t{$color}"
		    puts $fd "\tWidth\t$width"
		    puts $fd "}"
		    puts $fd "*/"
		} else {
		    set node1      [lindex $tags 0]
		    set node2      [lindex $tags 1]
		    set width      [.canvas itemcget $i -width]
		    set color      [.canvas itemcget $i -fill]
		    puts $fd "#"
		    puts $fd "/*"
    		    puts $fd "Link {"
		    puts $fd "\tNodes\t\"$node1\" \"$node2\""
		    puts $fd "\tColor\t{$color}"
		    puts $fd "\tWidth\t$width"
		    puts $fd "}"
		    puts $fd "*/"
		}
	    }
	}
    }
    if { $postIncludeFile!="" } {
	puts $fd "%include \"$postIncludeFile\""
	puts $fd "#"
    } 
    close $fd
    set activeFileChanged "no"
  }
}
#