#!/usr/local/bin/wish -f
global mfp
set user [exec whoami]
set mcmd "Mail"
set systemf /usr/spool/mail/[exec whoami]
set mbox $env(HOME)/mbox

wm withdraw .
regsub -all {//} $mbox {/} mbox

proc mailcmd { cmd } {
    global mfp

    puts $mfp(fid) $cmd
    puts $mfp(fid) $mfp(bcmd)
    flush $mfp(fid)

    set res ""
    while {[gets $mfp(fid) line] != -1 &&
            [string first $mfp(sentinel) $line] != 0} {
        set res "${res}${line}\n"
	flush $mfp(fid)
    }

    return $res
}

proc askcont { txt } {

    puts stdout $txt
    puts stdout "(y/n)? " nonewline
    flush stdout
    gets stdin ans
    if {$ans != "y"} {
	puts stdout "\nTest aborted by user."
	destroy . ; exit
    }
}

if {![file exists mtest.box]} {
    puts stderr "Test aborted: can't find mtest.box file"
    destroy . ; exit
}

set txt {
######################## TkMail Test #######################
Before running this test, you should backup your ~/mbox file
and make sure you have no new mail. However, if new mail
arrives during the test, it will not be lost, just appended
to the end of your ~/mbox. Also, at least one message must exist
in your ~/mbox file. A test message will be written there and 
deleted (you will be prompted before the deletion). Do you wish to continue?}
askcont $txt

# remove generated testing files if they already exist
if {[file exists mtest3.box]} {
    exec rm mtest3.box
}
if {[file exists mtest2.box]} {
    exec rm mtest2.box
}

# check that system inbox given might be valid
if {![file exists [file dirname $systemf]]} {
    puts stderr "Test aborted: It does not appear that the filename you "
    puts stderr "      gave me for your system mailbox is valid."
    destroy . ; exit
}

# check that system inbox is empty
if {[file exists $systemf] && [file size $systemf] > 0} {
    puts stderr "Test aborted: There is mail in your system inbox. "
    puts stderr "      Please deal with it before running this test."
    destroy . ; exit
}

# check that ~/mbox has at least one message
if {![file exists $mbox] || [file size $mbox] == 0} {
    puts stderr "Test aborted: your $mbox file is empty. I needs to have"
    puts stderr "              have at least one message."
    destroy . ; exit
}

# send new mail to user
set cmd {exec echo \"Simple mesg body\" | $mcmd -s \"tkmail test\" $user}
if {[catch "eval $cmd" res]} {
    puts stderr "Got following error trying to mail $user message"
    puts stderr $res
    destroy . ; exit
}

# start Mail pipeline
set mfp(bcmd) "xxx"
if {[catch {open "|$mcmd -N -f mtest.box" r+} mfp(fid)]} {
    puts stdout "Error starting pipe to $mcmd"
    destroy . ; exit
}
puts $mfp(fid) $mfp(bcmd)
flush $mfp(fid)
while {[gets $mfp(fid) mfp(sentinel)] != -1 &&
	[string first $mfp(bcmd) $mfp(sentinel)] == -1} {}
puts stdout "Sentinel is : $mfp(sentinel)\nStarting tests...\n\n"

puts $mfp(fid) "set append"
flush $mfp(fid)

# Actual tests
puts stdout "### Test of 'h' command. Should be message list ###"
puts stdout [mailcmd h]
askcont "Is the previous text a list of six message headers?"

puts stdout "### Test of '1' command. Should be text of message 1 ###"
puts stdout [mailcmd 1]
askcont "Is the previous text that of the message number one?"

puts stdout "### Testing 'folder <nonmailfile>' ####"
exec cat > mtest2.box << "A bunch of junk\nThis is not a mail file"
set res [mailcmd "folder mtest2.box"]
if {[string first " 0 messages" $res] == -1} {
    puts stdout "### ERROR: the following output not compatible with TkMail"
    puts stdout "           Should have been: \"mtest2.box\": 0 messages"
    puts stdout "           I got the following:\n"
    puts stdout $res
    destroy . ; exit
} else {
    puts stdout "Passed."
}

puts stdout "### Testing 'folder <goodfile>' ####"
set res [mailcmd "folder mtest.box"]
if {[string first " 6 messages" $res] == -1} {
    puts stdout "### ERROR: the following output not compatible with TkMail"
    puts stdout "           Should have been: \"mtest.box\": 6 messages"
    puts stdout "           I got the following:\n"
    puts stdout $res
    destroy . ; exit
} else {
    puts stdout "Passed."
}

puts stdout "### Testing 's 1 mtest3.box' ####"
set res [mailcmd "s 1 mtest3.box"]
if {$res == ""} {
    puts stdout "### ERROR: got no output from save operation"
    puts stdout "           This is incompatible with TkMail"
    destroy . ; exit
}
mailcmd "folder mtest3.box"
set res [mailcmd "1"]
if {[string first "Body of 1" $res] == -1} {
    puts stdout "### ERROR: the following output not compatible with TkMail"
    puts stdout "           Should have been the text of message 1"
    puts stdout "           I got the following:\n"
    puts stdout $res
    destroy . ; exit
} else {
    puts stdout "Passed."
}


while {![file exists $systemf] || [file size $systemf] == 0} {
     set txt "\nThe mail I sent to you hasn't seemed to arrive yet."
     set txt "$txt\nShould I check again now?"
     askcont $txt
}

puts stdout "### Testing incorporating new mail ###"
set res [mailcmd "folder %\nmbox ^-$\nfolder &"]
if {![regexp {Saved[ ]*([0-9][0-9]*)[ ]*mess} $res trash newm]} {
if {![regexp {message[ ]*([0-9][0-9]*)[ ]*new} $res trash newm]} {
    puts stdout "### ERROR: the following output not compatible with TkMail"
    puts stdout "	    Should have contained: Saved 1 message in mbox"
    puts stdout "		               or: * message 1 new"
    puts stdout "           I got the following:\n"
    puts stdout $res
    destroy . ; exit
}
}

if {$newm != 1} {
    puts stdout "### ERROR: the following output not compatible with TkMail"
    puts stdout "           or you received new outside mail during test."
    puts stdout "	    Should have been: Saved 1 message in mbox"
    puts stdout "           I got the following:\n"
    puts stdout $res
    destroy . ; exit
} else {
    puts stdout "Passed."
}


puts stdout "### Testing '$' to display new message ###"
set res [mailcmd "$"]
if {[string first "Simple mesg body" $res] == -1} {
    puts stdout "### ERROR: the following output not compatible with TkMail"
    puts stdout "           You might have given me the wrong ~/mbox filename."
    puts stdout "           Should have been message text with a body "
    puts stdout "	    containing the line: Simple mesg body"
    puts stdout "           I got the following:\n"
    puts stdout $res
    destroy . ; exit
} else {
    puts stdout "Passed."
}


puts stdout ""
puts stdout [mailcmd "f $"]
set txt "I am now going to delete the message displayed above from your"
set txt "$txt\nmbox file. Should I continue?"
askcont $txt


puts stdout "\n### Testing 'd $' ###"
set res [mailcmd "d $"]
if {$res != "" && [string first "eleting:" $res] == -1} {
    puts stdout "### ERROR: I expected to get no output from a delete"
    puts stdout "           operation, but I got:"
    puts stdout $res
    destroy . ; exit
} else {
    puts stdout "Passed."
}


puts stdout "### Testing 'folder &' to really delete message ###"
mailcmd "folder &"
set res [mailcmd "$"]
if {[string first "Simple mesg body" $res] != -1} {
    puts stdout "### ERROR: it appears that the test message did not get"
    puts stdout "           deleted. The message shown below contains the"
    puts stdout "           string 'Simple mesg body' which makes me think"
    puts stdout "	    that my test message was not deleted"
    puts stdout "	    It is possible there might have been more than"
    puts stdout "	    one test messsage at the end of your mbox"
    puts stdout "	    because of previous failed tests."
    puts stdout "/n$res"
    destroy . ; exit
} else {
    puts stdout "Passed."
}

puts stdout "\nCONGRATULATIONS! It appears TkMail will work on your system"

destroy . ; exit
