
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc update_software {} {
    global env do_update update_done
    
    switch -glob [exec uname -s] {
	SunOS*5.* { set arch sol }
	SunOS*4.* { set arch sun }
	IRIX*     { set arch sgi }
	AIX*      { set arch ibm }
	HP-UX*    { set arch hp }
	OSF1*     { set arch alpha }
	default   { error "Architecture is unknown." }
    }
    set acn $env(ICEM_ACN)
    if ![file owned $acn/bin/icemcfd] {
	file stat $acn/bin/icemcfd foo
	set line [exec egrep "^\[^:\]+:\[^:\]*:$foo(uid):" /etc/passwd]
	set user [lindex [split $line :] 0]
	if {$user == ""} { set user "uid $foo(uid)" }
	errormsg "Only the owner of the ICEM CFD software ($user) can perform\
		this operation."
	return
    }
    
    update_grab icemcfd.map /tmp/icemcfd.map
    set fp [open /tmp/icemcfd.map r]
    set modules [read $fp]
    close $fp
    exec rm /tmp/icemcfd.map
    
    set mods ""
    foreach mod $modules {
	set n [lindex $mod 0]
	if [file exists $acn/[lindex $mod 1]] {
	    file stat $acn/[lindex $mod 1] stvar
	    set my_time($n) $stvar(mtime)
	} else {
	    set my_time($n) 0
	}
	set new_time($n) [lindex $mod 3]
	set name($n) [lindex $mod 2]
	lappend mods $n
    }
    
    toplevel .update
    set i 1
    label .update.name0 -text Module
    label .update.stat0 -text Status
    label .update.copy0 -text Copy
    blt_table .update .update.name0 0,0
    blt_table .update .update.stat0 0,1
    blt_table .update .update.copy0 0,2
    foreach n $mods {
	if !$my_time($n) {
	    set stat "not installed"
	    set do_update($n) 0
	} elseif {$my_time($n) < $new_time($n)} {
	    set stat "out of date"
	    set do_update($n) 1
	} else {
	    set stat "up to date"
	    set do_update($n) 0
	}
	label .update.name$i -text $name($n)
	label .update.stat$i -text $stat
	checkbutton .update.copy$i -variable do_update($n)
	
	blt_table .update .update.name$i $i,0
	blt_table .update .update.stat$i $i,1
	blt_table .update .update.copy$i $i,2
	
	incr i
    }
    frame .update.buts
    button .update.buts.update -text Update -command "set update_done 1"
    button .update.buts.cancel -text Cancel -command "set update_done 0"
    pack .update.buts.update .update.buts.update -side left
    blt_table .update .update.buts $i,0 -columnspan 3
    
    tkwait variable update_done
    destroy .update
    if !$update_done { infomsg "Cancelled update."; return }
    
    set here [pwd]
    cd $acn
    foreach n $mods {
	if $do_update($n) {
	    infomsg "Getting module $n for $arch ..."
	    set out [update_grab $n.$arch.tar.gz /tmp/$n.$arch.tar.gz]
	    if ![string compare $out ""] {
		errormsg "Couldn't get module $n for $arch"
		continue
	    }
	    infomsg $out
	    set out [exec $acn/bin/gzip -d < /tmp/$n.$arch.tar.gz | tar xvf -]
	    infomsg $out
	    exec rm -f /tmp/$n.$arch.tar.gz
	}
    }
    cd $here
}

proc update_grab {fromfile tofile} {
    set site ftp.dnai.com
    set dir users/i/icemcfd/updates/carborundum
    
    spawn ftp $site
    expect "Name*:"
    exp_send "anonymous\r"
    expect "Password:"
    exp_send "icemuser@\r"
    expect "ftp>"
    exp_send "binary\r"
    expect "ftp>"
    exp_send "cd $dir\r"
    exp_send "get $fromfile $tofile\r"
    set out [expect "ftp>"]
    exp_send "quit\r"
    close
    wait
    return $out
}
