#!/usr/local/bin/wish


set exmh_path /usr/bin
set exmh_bin exmh
wm withdraw .

# if exmh isn't running , start it up
if {[lsearch [winfo interps] $exmh_bin ]==-1} {
    exec $exmh_path/$exmh_bin  > /dev/null &
    while {[lsearch [winfo interps] $exmh_bin ]==-1} {
	after 200       
    }
}

if { 0 == [string length $argv] } {
    send exmh Msg_Compose
} else {
    set path [ send exmh set mhProfile(path)]
    if {[catch {open $argv r} in] == 0} {
	set nsdata [read $in]
	close $in
	set out [open $argv w]
	foreach line [split $nsdata \n] {
	    if { [regexp -- "^--------" $line] } {
		if {[catch {open $path/components r} in] == 0} {
		    set old [read $in]
		    close $in
		    foreach header [split $old \n] {
			if { 0 == [regexp "^(To|Subject):" $header] } {
			    puts $out $header
			}
		    }
		}
	    } else {
		puts $out $line
	    }
	}
	close $out
    }
    send exmh MhExec comp -nowhatnowproc -form $argv
    file delete $argv
    set draftFolder [send exmh set mhProfile(draft-folder)]
    set draftID [send exmh Mh_Cur $draftFolder]
    send exmh Msg_CompUse $draftFolder $draftID
}

exit 0
