#!/usr/local/bin/wish -f
global mf mfp

set mf(mail-command) "Mail"
set mf(mail-system) /usr/spool/mail/[exec whoami]
set mf(mail-mbox) $env(HOME)/mbox
set mf(mail-deliver) "/usr/lib/sendmail -bm -t"

set mfp(user) [exec whoami]
set mf(mail-flush) 1000
set mfp(fid) ""

wm withdraw .
regsub -all {//} $mf(mail-mbox) {/} mf(mail-mbox)

source ../mailcmd.tk
# source ../mailcmd2.tk

proc mf_mailerror { txt } {
    puts stderr "\007\007$txt"
}

proc askcont { txt } {
    global mf mfp tk_version

    set ans ""
    puts stdout $txt
    while {!($ans == "y" || $ans == "yes")} {
	puts stdout "(y/n)? " nonewline
	flush stdout
	gets stdin ans
	if {$ans == "n" || $ans == "no"} {
	    puts stdout "\nTest aborted by user."
	    if {$mfp(fid) != ""} {mf_killcmdpipe $mfp(fid)}
	    if {$tk_version<3.3} {destroy .}
	    exit
	}
    }
}

if {![file exists mtest.box]} {
    mf_mailerror "Test aborted: can't find mtest.box file"
    if {$tk_version<3.3} {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 $mf(mail-system)]]} {
    puts stderr "Test aborted: It does not appear that the filename you "
    puts stderr "      gave me for your system mailbox is valid."
    if {$tk_version<3.3} {destroy .}
    exit
}

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

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

# send new mail to user
set txt "Subject: tkmail test\nTo: $mfp(user)\n\nSimple mesg body\n"
set cmd {exec $mf(mail-deliver) << \$txt}
if {[catch "eval $cmd" res]} {
    puts stderr "Got following error trying to mail $mfp(user) message"
    puts stderr $res
    if {$tk_version<3.3} {destroy .}
    exit
}

# start Mail pipeline
set mfp(bcmd) "xxx"
set mfp(fid) [mf_getcmdpipe "mtest.box"]
if {$mfp(fid) == ""} {
    if {$tk_version<3.3} {destroy .}
    exit
}
puts stdout "Sentinel is : $mfp(sentinel)\nStarting tests...\n\n"
if {$mfp(sentinel) == ""} {
    mf_mailerror "Empty sentinel string."
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
}

# Actual tests
puts stdout "### Test of 'h' command. Should be message list ###"
mf_mailcmd res h $mfp(fid)
puts stdout $res
askcont "Is the previous text a list of six message headers?"

puts stdout "### Test of '1' command. Should be text of message 1 ###"
mf_mailcmd res 1 $mfp(fid)
puts stdout $res
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"
mf_mailcmd res "folder mtest2.box" $mfp(fid)
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
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
} else {
    puts stdout "Passed."
}

puts stdout "### Testing 'folder <goodfile>' ####"
mf_mailcmd res "folder mtest.box" $mfp(fid)
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
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
} else {
    puts stdout "Passed."
}

puts stdout "### Testing 's 1 mtest3.box' ####"
mf_mailcmd res "s 1 mtest3.box" $mfp(fid)
if {![regexp {[^ ]+ \[.+\] [^ ]} $res]} {
    puts stdout "### ERROR: incorrect output from save operation"
    puts stdout "           got: $res"
    puts stdout "	    need: \"mtest3.box\" [New file] 13/298"
    puts stdout "           This is incompatible with TkMail"
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
}
mf_mailcmd trash "folder mtest3.box" $mfp(fid)
mf_mailcmd res "1" $mfp(fid)
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
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
} else {
    puts stdout "Passed."
}


while {![file exists $mf(mail-system)] || [file size $mf(mail-system)] == 0} {
     set txt "\nThe mail I sent to you hasn't seemed to arrive yet."
     set txt "\nThe file $mf(mail-system) is empty or non-existant."
     set txt "$txt\nShould I check again now?"
     askcont $txt
}

puts stdout "### Testing incorporating new mail ###"
mf_mailcmd trash "folder $mf(mail-system)" $mfp(fid)
mf_mailcmd gnum "size \$" $mfp(fid)
if {![regexp {([0-9][0-9]*)[ ]*:[ ]*[0-9]} $gnum trash newm] || 
	$newm != 1} {
    puts stdout "### ERROR: the following output not compatible with TkMail\n"
    puts stdout "	    It's possible you got new mail from outside\n"
    puts stdout "	    Should have contained:    1: 12/329\n"
    puts stdout "           I got the following:\n"
    puts stdout $gnum
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
}

set mtime [file mtime $mf(mail-mbox)]
mf_mailcmd res "s ^-\$ $mf(mail-mbox)" $mfp(fid)

if {![regexp {[^ ]+ \[.+\] [^ ]} $res]} {
    puts stdout "### ERROR: the incorporation failed on copy to $mf(mail-mbox)\n"
    puts stdout "           I got the following error result:\n"
    puts stdout $res
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
}

if {$mtime == [file mtime $mf(mail-mbox)]} {
    puts stdout "### ERROR: the incorporation failed\n"
    puts stdout "           Your mbox does not appear to have been appended to"
    puts stdout $res
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
}

mf_mailcmd res "d ^-\$" $mfp(fid)
if {$res != "" && [string first "eleting:" $res] == -1} {
    puts stdout "I got the following message from Mail will removing the message"
    puts stdout "from your system folder:\n"
    puts stdout $res\n
    askcont "Should I continue?"
}
puts stdout "Passed."
mf_mailcmd trash "folder $mf(mail-mbox)" $mfp(fid)


puts stdout "### Testing '$' to display new message ###"
mf_mailcmd res "$" $mfp(fid)
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
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
} else {
    puts stdout "Passed."
}

mf_mailcmd res "f $" $mfp(fid)
puts stdout ""
puts stdout $res
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 $' ###"
mf_mailcmd res "d $" $mfp(fid)
if {$res != "" && [string first "eleting:" $res] == -1} {
    puts stdout "I got the following message from Mail will removing the message"
    puts stdout "from your mbox folder:\n"
    puts stdout $res\n
    askcont "Should I continue?"
}
puts stdout "Passed."



puts stdout "### Testing 'folder $mf(mail-mbox)' to really delete message ###"
mf_mailcmd res "folder $mf(mail-mbox)" $mfp(fid)
mf_mailcmd res "$" $mfp(fid)
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"
    mf_killcmdpipe $mfp(fid)
    if {$tk_version<3.3} {destroy .}
    exit
} else {
    puts stdout "Passed."
}

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

mf_closecmdpipe $mfp(fid)
if {$tk_version<3.3} {destroy .}
exit

# Local Variables: ***
# mode:tcl ***
# End: ***

