proc editFile {box tfile ext op} {

# $Header: /home/cvsroot/tcldb/ucodb/Tlib/editFile,v 1.3 1999/03/27 01:19:19 de Exp $
# tcl procs saved on Sun Sep 03 16:19:14 PDT 1995

	global env

	set win .[lindex [split $box .] 1]
	set wlist [winfo children $win]
	$win configure -cursor watch
	foreach child $wlist {
		$child configure -cursor watch
	}

	set unique [getclock]
        set uname [id user]

	if {[info exists env(HOME)]} {
	set hdir $env(HOME)
	} else {
        set hdir /u/$uname
	}

        if {![file isdirectory $hdir]} {
                puts stderr "Cannot find user $uname home directory $hdir."
                puts stderr "using /tmp"
                set hdir /tmp
        }

	if {$ext == ""} {
		set fdir $hdir
		set ext $unique
	} else {
		set fdir $hdir/.$ext
	}

	if {![file isdirectory $fdir]} {
		if {[file exists $fdir]} {
			set fdir /tmp
			puts stderr "using /tmp"
		} else {
			system "mkdir $fdir"
		}
	}

	set fe [lindex [split $tfile .] [expr [llength [split $tfile .]] - 1]]	

	if {$fe != "$ext"} {
		set tfile $tfile.$ext
	}

#	give priority to file in current dir
	set tf $tfile
	if {![info exists $tfile]} {
	set tf $fdir/$tfile
	}
	
	set err [catch {set tfp [open $tf $op]}]

	if {$err} {
		puts stderr "Cannot open file $tf for operation $op"
		return
	}

	if {$err} {
		set of "/tmp/$tfile.$unique"	
		set tfp [open $tf w]
	}

	case $op in {
	{w} {
	set lines [split [$box get 1.0 end] \n]

	foreach line $lines {
	
		puts $tfp "[stringFix2 $line out]"

	}
		puts stderr "Output is in file $tf"
	}
        {r} {
                $box delete 1.0 end
                for_file line $fdir/$tfile {
                        $box insert end "$line \n"
                }
        }
	}

	close $tfp

	$win configure -cursor {}
	foreach child $wlist {
		$child configure -cursor {}
	}

}
