#!/usr/local/bin/wish8.0
#!/usr/local/bin/wish4.1

# Set port to the value of the port you have chosen for MAT (default 2345)
set port 2345


# -------------------------- Do Not edit below this line ---------------
set DEBUG 1
set canvas_minX 300
set canvas_minY 200

# The canvas_level is a variable for determining what the canvas displays
# Level 10 = The hosts
# Level 20 = The configuration files
# Level 30 = The stats
# Level 40 = The DNS domain selector
# Level 50 = MAT tools
# Level 60 = Backup/Restore tools
# Level 70 = NIS Master
# Level 80 = NFS tools
set level 10

# The currentdomain variable is for teh DNS master 
set currentdomain "-"
 
set retain 0
set ttype 9999
set current 9999
set currentlineno 9999

# Hosts info for the management console
set mb_hosts "../hosts"
set active_hosts {}

set feature_list_file "../lib/features"
set feature_list {}

if { $argc == 0 } {
    set currenthost "localhost"
} else {
    set currenthost [ lindex $argv 0 ]
}
set stask " "
set browse 5
set SelectorLine 9999

# Default password data
set username "root"
# Do NOT set your own default password here.  It is too dangerous
set passwd "bye"
set new 0

# This is the authentication flag
set authenticated 0

# This is the upgrade buffer
set upgrade_buff {}

# This is the master list.  It Must be maintained
set master ""

# Font Declarations
#set norm { Helvitica 12 bold }
set norm {-*-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*}

lappend auto_path "../lib"

# This for auto-probing the hosts
set currentProbe 0

proc ReadHosts {} {
    global mb_hosts
    global active_hosts
    set active_hosts {}
    catch "set fid [ open $mb_hosts r ]" retval
    if { [string range $retval 0 3 ] == "file" } {
	while { [ gets $fid fout ] > 0 } {
	    lappend active_hosts $fout
	}
	close $fid
    } else {
	licen
    }
}


# Read-in the hosts file
ReadHosts


#
#---------------- Main Code Start ---------------------------
#
wm title . "MAT on: $currenthost"

frame .menu -relief raised -bd 2 
menubutton .menu.opt0 -text "File" -menu .menu.opt0.file -font $norm
menubutton .menu.opt5 -text "Agent" -menu .menu.opt5.file -font $norm 
menubutton .menu.opt10 -text "Browse" -menu .menu.opt10.menu -font $norm 
menubutton .menu.opt100 -text "Help" -menu .menu.opt100.menu -font $norm 

menu .menu.opt0.file
.menu.opt0.file add command -label "Quit" -command "destroy . " -foreground red -font $norm

menu .menu.opt5.file
.menu.opt5.file add command -label "Login" -command "getPasswd \$currenthost 0" -font $norm -foreground blue
.menu.opt5.file add separator
.menu.opt5.file add command -label "Upgrade" -command "upgrade \$currenthost" -font $norm -foreground red
.menu.opt5.file add separator
.menu.opt5.file add command -label "MAT Hosts" -command "set browse 90 ;repack 90" -font $norm
.menu.opt5.file add command -label "Probe" -command "AutoProbe ;set level 10 ;fillCan 10" -font $norm

menu .menu.opt10.menu
.menu.opt10.menu add radiobutton -label "Users" -variable browse -value 5 -font $norm -command { repack 5 }
.menu.opt10.menu add radiobutton -label "Groups" -variable browse -value 2 -font $norm -command { repack 2 }
.menu.opt10.menu add radiobutton -label "Hosts" -variable browse -value 1 -font $norm -command { 
    repack 1     
    set currentlineno 9999  }
.menu.opt10.menu add radiobutton -label "Mail Aliases" -variable browse -value 8 -font $norm -command { repack 8 }
.menu.opt10.menu add radiobutton -label "Mounts" -variable browse -value 3 -font $norm -command { repack 3 }
#.menu.opt10.menu add radiobutton -label "Tasks" -variable browse -value 0 -font $norm -command { repack 0 }
.menu.opt10.menu add radiobutton -label "Motd" -variable browse -value 7 -font $norm -command { repack 7 }
.menu.opt10.menu add radiobutton -label "DNS Client" -variable browse -font $norm -value 6 -command { repack 6 }
#.menu.opt10.menu add radiobutton -label "Inittab" -variable browse -value 4 -font $norm -command { repack 4 }
#.menu.opt10.menu add radiobutton -label "Samba Shares" -variable browse -value 9 -font $norm -command { repack 9 }
#.menu.opt10.menu add radiobutton -label "Sudo'ers" -variable browse -value 11 -font $norm -command { repack 11 }
.menu.opt10.menu add radiobutton -label "Crontab" -variable browse -value 12 -font $norm -command { repack 12 }
.menu.opt10.menu add separator
.menu.opt10.menu add radiobutton -label "MAT Hosts" -variable browse -value 90 -font $norm -command { repack 90 }
.menu.opt10.menu add radiobutton -label "MAT User" -variable browse -value 91 -font $norm -command { repack 91 }
.menu.opt10.menu add radiobutton -label "MAT Logs" -variable browse -value 92 -font $norm -command { repack 92 }
.menu.opt10.menu add separator

menu .menu.opt100.menu
.menu.opt100.menu add command -label "About" -font $norm -command "about"
.menu.opt100.menu add command -label "MAT" -font $norm -command "help help"

pack .menu.opt0 .menu.opt5 .menu.opt10 -side left -fill x 
pack .menu.opt100 -anchor e
pack .menu -side top -fill x

frame .tp -relief raised -bd 2 
label .tp.title -text "Host" -font $norm
pack .tp.title
# The canvas is defined in fillcan
frame .tp.host -relief sunken -bd 2 
scrollbar .tp.host.vscroll -orient vertical -command ".tp.host.canvas yview"
pack .tp.host.vscroll -side left -fill y
pack .tp.host -expand 1 -fill both
pack .tp -fill both -expand 1


# Generic frame user for everything 
frame .general -relief raised -bd 2
# Generic text window
label .general.label -text "-"  -font $norm
pack .general.label -side top -fill x
text .general.textlabel -width 80 -height 1 -relief flat
text .general.text -width 80 -height 12 -yscrollcommand {.general.textscroll set }
scrollbar .general.textscroll -orient vertical -command ".general.text yview"
pack .general.textscroll -side left -fill y
pack .general.textlabel -fill x  
pack .general.text -fill both -expand 1
pack .general -fill both -expand 1
frame .general.frame 


frame .commonframe -relief raised -bd 2
button .commonframe.new -text "New" -foreground #008000 -activeforeground #004000  -font $norm -command { 
    if { $level == 10 } {
	MatHost 
    } elseif {$browse == 0 } {
	Tasks $currenthost 9999 0 
    } elseif {$browse == 1 } {
	Hosts $currenthost 9999 0 
    } elseif {$browse == 2 } {
	Groups $currenthost 9999 0 
    } elseif {$browse == 3 } {
	Fstab $currenthost 9999 0 
    } elseif {$browse == 4 } {
	
    } elseif {$browse == 5 } {
	 User $currenthost 9999 0 
    } elseif {$browse == 6 } {
	DnsClient $currenthost 0 0 
    } elseif {$browse == 7 } {
	# Motd updater
	set cmd "put local motd"
	set returnval [queryHost $currenthost $cmd 1 "ext" "put" "t" ".general.text" ]
	if { [lindex [split [lindex $returnval end ] " " ] 0 ] == "ERROR:" } {
	    error 260
	}
    } elseif {$browse == 8 } {
	Mail $currenthost 9999 0 
    } elseif {$browse == 11 } {
	Sudo $currenthost 9999 0 
    } elseif {$browse == 12 } {
	CronEdit $currenthost 9999 0 
    } elseif {$browse == 41 } {
	DnsEdit $currenthost $currentdomain 9999 0 
    } elseif {$browse == 70 } {
	NISUser $currenthost 9999 0 
    } elseif {$browse == 71 } {
	NISGroups $currenthost 9999 0 
    } elseif {$browse == 71 } {
	NISHosts $currenthost 9999 0 
    } elseif {$browse == 91 } {
	MatUser $currenthost 9999 0 
    }

} 
button .commonframe.del -text "Delete Selected" -foreground red -activeforeground red -font $norm -command {
    if { $SelectorLine != 9999  && $browse != 7 } {
#	set master [ lreplace $master $SelectorLine $SelectorLine "-gone- - - - - -\n" ]
	set returnval {}
	if { $browse == 0 } {

	} elseif { $browse == 1 } {
	    set returnval [queryHost $currenthost "del hosts $SelectorLine" 1 ]
	} elseif { $browse == 2 } {
	    set returnval [queryHost $currenthost "del group $SelectorLine" 1 ]
	} elseif { $browse == 3 } {
	    set returnval [queryHost $currenthost "del fstab $SelectorLine" 1 ]
	} elseif { $browse == 4 } {
	    set returnval [queryHost $currenthost "del inittab $SelectorLine" 1 ]
	} elseif { $browse == 5 } {
	    set returnval [queryHost $currenthost "del users $SelectorLine" 1 ]
	} elseif { $browse == 8 } {
	    set returnval [queryHost $currenthost "del alias $SelectorLine" 1 ]
	} elseif { $browse == 6 } {
	    DnsClient $currenthost 0 0
	    set returnval 0
	} elseif { $browse == 11 } {
	    set returnval [queryHost $currenthost "del sudo $SelectorLine" 1 ]
	} elseif { $browse == 12 } {
	    global master
	    set newcron [ lreplace $master $SelectorLine $SelectorLine {} ]
	    # Update comment if needed
	    set tmp2 [expr $SelectorLine - 1 ] 
	    set tmp [ lindex $master $tmp2 ]
	    if { [string range $tmp 0 1 ] == "#v" } {
		set upgrade_buff [ lreplace $newcron $tmp2 $tmp2 {} ]
	    } else {
		set upgrade_buff $newcron
	    }
	    set cmd "put local crontab"
	    set returnval [queryHost  $currenthost $cmd 1 "ext" "put" "v" "upgrade_buff" ]
	    if { [lindex [split $returnval " " ] 0 ] == "\{ERROR:" } {
		error 361
		puts stdout "ERROR:  Unable to upload the crontab to $currenthost"
	    } else {
		set cmd "croninstall cd"
		set returnval [queryHost  $currenthost $cmd 1 ]
		if { [lindex [split $returnval " " ] 0 ] == "\{ERROR:" } {
		    error 362
		    puts stdout "ERROR:  Unable to update crontab on $currenthost"
		}
	    }
	} elseif { $browse == 41 } {
	    set moddata [split [ lindex $master $modline ] ":" ]
	    set arg "[ lindex $moddata 0 ]::::"
	    set returnval [queryHost $currenthost "DNS set host $currentdomain $arg" 1 ]
	} elseif { $browse == 70 } {
	    set login [lindex [split [.general.text get [expr $SelectorLine + 1].0 [expr $SelectorLine + 2].0 ] ] 0]
	    puts stdout "Deleting $login"
	    set returnval [queryHost $currenthost "NIS del pass $login" 1 ]
	} elseif { $browse == 71 } {
	    set delgrp [lindex [split [.general.text get [expr $SelectorLine + 1].0 [expr $SelectorLine + 2].0 ] ] 0]
	    puts stdout "Deleting $delgrp"
	    set returnval [queryHost $currenthost "NIS del group $delgrp" 1 ]
	} elseif { $browse == 72 } {
	    set delhost [lindex [split [.general.text get [expr $SelectorLine + 1].0 [expr $SelectorLine + 2].0 ] ] 0]
	    puts stdout "Deleting $delhost"
	    set returnval [queryHost $currenthost "NIS del host $delhost" 1 ]
	} elseif { $browse == 90 } {
	    set edit_host [ lindex [split [ lindex $master $SelectorLine ] ":" ] 0 ]
	    global mb_hosts
	    global active_hosts
	    # Write out new file
	    set fid [ open $mb_hosts w ]
	    foreach data $active_hosts {
		if { $edit_host == [lindex [ split $data ":" ] 0 ] } {
		    # Write nothing
		} else {
		    puts $fid $data
		}
	    }
	    close $fid
	    ReadHosts
	    fillCan 10
	}
	if { [lindex [split $returnval " " ] 0 ] == "\{ERROR:" } {
	    error 71
	} else {
	    .general.text configure -state normal
	    .general.text tag configure l$SelectorLine -underline 0
	    eval ".general.text delete [.general.text tag ranges l$SelectorLine ]"
	    .general.text tag delete l$SelectorLine
	    .general.text configure -state disabled
	    set SelectorLine 9999
	    repack $browse
	}

    } else {
	if { $browse == 6 } {
	    DnsClient $currenthost 0 0
	} else {
	    error 4 
	}
    }
}

button .commonframe.mod -text "Modify Selected" -foreground #a00000 -activeforeground #800000 -font $norm -command { 
    if { $SelectorLine != 9999 && $browse != 6 } {
	set modline $SelectorLine 
	set moddata [ lindex $master $modline ]
    } else {
	set modline 9999 
	set moddata 9999
    }
    if { $browse == 0 } {
	Tasks $currenthost $modline $moddata
    } elseif { $browse == 1 } {
	Hosts $currenthost $modline $moddata
    } elseif { $browse == 2 } {
	Groups $currenthost $modline $moddata
    } elseif { $browse == 3 } {
	Fstab $currenthost $modline $moddata
    } elseif { $browse == 4 } {
	
    } elseif { $browse == 5 } {
	User $currenthost $modline $moddata
    } elseif { $browse == 6 } {
	DnsClient $currenthost 0 0
    } elseif { $browse == 8 } {
	Mail $currenthost $modline $moddata
    } elseif { $browse == 11 } {
	Sudo $currenthost $modline $moddata
    } elseif { $browse == 12 } {
	CronEdit $currenthost $modline $moddata
    } elseif { $browse == 70 } {
	set login [lindex [split [.general.text get [expr $SelectorLine + 1].0 [expr $SelectorLine + 2].0 ] ] 0]
	NISUser $currenthost $login $moddata
    } elseif { $browse == 71 } {
	set delgrp [lindex [split [.general.text get [expr $SelectorLine + 1].0 [expr $SelectorLine + 2].0 ] ] 0]
	NISGroups $currenthost $delgrp $moddata
    } elseif { $browse == 72 } {
	set delhost [lindex [split [.general.text get [expr $SelectorLine + 1].0 [expr $SelectorLine + 2].0 ] ] 0]
	NISHosts $currenthost $delhost $moddata
    } elseif { $browse == 90 } {
	MatHost $moddata
    } elseif { $browse == 91 } {
	MatUser $currenthost $modline $moddata
    } 
    if { $browse != 7 } {	
	repack $browse
    }
}

button .commonframe.search -text "Search for:" -font $norm -command { 
    if { [ .commonframe.what get ] != {} } {
	set match [ .general.text search -nocase [ .commonframe.what get ] 0.0 end ]
	if { $match != {} } {
	    .general.text see $match
	}
    }
}
entry .commonframe.what -width 12

bind .commonframe.what <Return> {
    .commonframe.search invoke
}

button .commonframe.b1 
button .commonframe.b2

pack .commonframe.new .commonframe.del .commonframe.mod .commonframe.search .commonframe.what -side left -anchor w -padx 3
pack .commonframe -fill x


#
# -------------- Display host info ----------------------
#
proc hostinfo { host } {
    set returndata [queryHost $host hostinfo 0 ]
    HostInfo $host $returndata
}
    

# Probe Hosts
#AutoProbe

fillCan $level

#------------------ Bindings ---------------------------

#  All old bindings are in the bindinge file

bind .tp.host.canvas <Button-1> {
    set curX %x
    set curY %y
}






