#!/bin/sh
######################################################
#
# Test sortm
#
######################################################

set -e

if test -z "$MH_OBJ_DIR"; then
    srcdir=`dirname $0`/../..
    MH_OBJ_DIR=`cd $srcdir && pwd`; export MH_OBJ_DIR
fi

. "$MH_OBJ_DIR/test/common.sh"

setup_test

check_exit '-eq 1' sortm -

expected=$MH_TEST_DIR/$$.expected
actual=$MH_TEST_DIR/$$.actual

cd $MH_TEST_DIR

# check -help
cat >"$expected" <<EOF
Usage: sortm [+folder] [msgs] [switches]
  switches are:
  -datefield field
  -textfield field
  -notextfield
  -limit days
  -nolimit
  -[no]verbose
  -[no]all
  -[no]check
  -version
  -help
EOF

#### Skip nmh intro text.
run_prog sortm -help 2>&1 | sed '/^$/,$d' >"$actual"
check "$expected" "$actual"

# check -version
case `sortm -version` in
  sortm\ --*) ;;
  *       ) echo "$0: sortm -v generated unexpected output" 1>&2
            failed=`expr ${failed:-0} + 1`;;
esac

cat >$MH_TEST_DIR/Mail/inbox/11 <<EOF || exit 1
From: Test11 <test11@example.com>
To: Some User <user@example.com>
Date: Fri, 29 Sep 2006 00:00:01
Alt-Date: Thu,  1 Sep 2005 00:00:00
Subject: Re: common subject

This is message number 11
EOF

cat >$MH_TEST_DIR/Mail/inbox/12 <<EOF || exit 1
From: Test12 <test12@example.com>
To: Some User <user@example.com>
Date: Thu, 28 Sep 2006 23:59:59
Alt-Date: Sat, 30 Sep 2006 00:00:00
Subject: common subject

This is message number 12
EOF

# check with no options
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test1              Testing message 1<<This is message number 1 >>
   3  09/29 Test2              Testing message 2<<This is message number 2 >>
   4  09/29 Test3              Testing message 3<<This is message number 3 >>
   5  09/29 Test4              Testing message 4<<This is message number 4 >>
   6  09/29 Test5              Testing message 5<<This is message number 5 >>
   7  09/29 Test6              Testing message 6<<This is message number 6 >>
   8  09/29 Test7              Testing message 7<<This is message number 7 >>
   9  09/29 Test8              Testing message 8<<This is message number 8 >>
  10  09/29 Test9              Testing message 9<<This is message number 9 >>
  11  09/29 Test10             Testing message 10<<This is message number 10 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
EOF

run_prog sortm
scan -width 80 >"$actual"
check "$expected" "$actual"

# check +folder
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2+ 09/29 Test1              Testing message 1<<This is message number 1 >>
   3  09/29 Test2              Testing message 2<<This is message number 2 >>
   4  09/29 Test3              Testing message 3<<This is message number 3 >>
   5  09/29 Test4              Testing message 4<<This is message number 4 >>
   6  09/29 Test5              Testing message 5<<This is message number 5 >>
   7  09/29 Test6              Testing message 6<<This is message number 6 >>
   8  09/29 Test7              Testing message 7<<This is message number 7 >>
   9  09/29 Test8              Testing message 8<<This is message number 8 >>
  10  09/29 Test9              Testing message 9<<This is message number 9 >>
  11  09/29 Test10             Testing message 10<<This is message number 10 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
EOF

refile first +inbox; folder -pack >/dev/null
run_prog sortm +inbox
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -verbose
cat >"$expected" <<EOF
sorting by datefield date
renaming message chain from 12 to 1
message 11 becomes message 12
message 10 becomes message 11
message 9 becomes message 10
message 8 becomes message 9
message 7 becomes message 8
message 6 becomes message 7
message 5 becomes message 6
message 4 becomes message 5
message 3 becomes message 4
message 2 becomes message 3
message 1 becomes message 2
   1  09/28 Test12             common subject<<This is message number 12 >>
   2+ 09/29 Test1              Testing message 1<<This is message number 1 >>
   3  09/29 Test2              Testing message 2<<This is message number 2 >>
   4  09/29 Test3              Testing message 3<<This is message number 3 >>
   5  09/29 Test4              Testing message 4<<This is message number 4 >>
   6  09/29 Test5              Testing message 5<<This is message number 5 >>
   7  09/29 Test6              Testing message 6<<This is message number 6 >>
   8  09/29 Test7              Testing message 7<<This is message number 7 >>
   9  09/29 Test8              Testing message 8<<This is message number 8 >>
  10  09/29 Test9              Testing message 9<<This is message number 9 >>
  11  09/29 Test10             Testing message 10<<This is message number 10 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
EOF

refile first +inbox; folder -pack >/dev/null
run_prog sortm -verbose >"$actual"
scan -width 80 >>"$actual"
check "$expected" "$actual"

# check -noverbose
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2+ 09/29 Test1              Testing message 1<<This is message number 1 >>
   3  09/29 Test2              Testing message 2<<This is message number 2 >>
   4  09/29 Test3              Testing message 3<<This is message number 3 >>
   5  09/29 Test4              Testing message 4<<This is message number 4 >>
   6  09/29 Test5              Testing message 5<<This is message number 5 >>
   7  09/29 Test6              Testing message 6<<This is message number 6 >>
   8  09/29 Test7              Testing message 7<<This is message number 7 >>
   9  09/29 Test8              Testing message 8<<This is message number 8 >>
  10  09/29 Test9              Testing message 9<<This is message number 9 >>
  11  09/29 Test10             Testing message 10<<This is message number 10 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
EOF

refile first +inbox; folder -pack >/dev/null
run_prog sortm -verbose -noverbose >"$actual"
scan -width 80 >>"$actual"
check "$expected" "$actual"

# check msgs
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test2              Testing message 2<<This is message number 2 >>
   3  09/29 Test3              Testing message 3<<This is message number 3 >>
   4  09/29 Test4              Testing message 4<<This is message number 4 >>
   5  09/29 Test5              Testing message 5<<This is message number 5 >>
   6  09/29 Test6              Testing message 6<<This is message number 6 >>
   7  09/29 Test7              Testing message 7<<This is message number 7 >>
   8  09/29 Test8              Testing message 8<<This is message number 8 >>
   9  09/29 Test9              Testing message 9<<This is message number 9 >>
  10  09/29 Test10             Testing message 10<<This is message number 10 >>
  11  09/29 Test11             Re: common subject<<This is message number 11 >>
  12+ 09/29 Test1              Testing message 1<<This is message number 1 >>
EOF

refile first +inbox; folder -pack >/dev/null
run_prog sortm first last
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -datefield
cat >"$expected" <<EOF
   1  09/29 Test11             Re: common subject<<This is message number 11 >>
   2  09/29 Test2              Testing message 2<<This is message number 2 >>
   3  09/29 Test3              Testing message 3<<This is message number 3 >>
   4  09/29 Test4              Testing message 4<<This is message number 4 >>
   5  09/29 Test5              Testing message 5<<This is message number 5 >>
   6  09/29 Test6              Testing message 6<<This is message number 6 >>
   7  09/29 Test7              Testing message 7<<This is message number 7 >>
   8  09/29 Test8              Testing message 8<<This is message number 8 >>
   9  09/29 Test9              Testing message 9<<This is message number 9 >>
  10  09/29 Test10             Testing message 10<<This is message number 10 >>
  11  09/28 Test12             common subject<<This is message number 12 >>
  12+ 09/29 Test1              Testing message 1<<This is message number 1 >>
EOF

run_prog sortm -datefield Alt-Date 1 11
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -textfield subject (datefield major, subjects grouped)
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test11             Re: common subject<<This is message number 11 >>
   3  09/29 Test2              Testing message 2<<This is message number 2 >>
   4  09/29 Test3              Testing message 3<<This is message number 3 >>
   5  09/29 Test4              Testing message 4<<This is message number 4 >>
   6  09/29 Test5              Testing message 5<<This is message number 5 >>
   7  09/29 Test6              Testing message 6<<This is message number 6 >>
   8  09/29 Test7              Testing message 7<<This is message number 7 >>
   9  09/29 Test8              Testing message 8<<This is message number 8 >>
  10  09/29 Test9              Testing message 9<<This is message number 9 >>
  11  09/29 Test10             Testing message 10<<This is message number 10 >>
  12+ 09/29 Test1              Testing message 1<<This is message number 1 >>
EOF

run_prog sortm -textfield subject
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -nolimit
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test11             Re: common subject<<This is message number 11 >>
   3  09/29 Test2              Testing message 2<<This is message number 2 >>
   4  09/29 Test3              Testing message 3<<This is message number 3 >>
   5  09/29 Test4              Testing message 4<<This is message number 4 >>
   6  09/29 Test5              Testing message 5<<This is message number 5 >>
   7  09/29 Test6              Testing message 6<<This is message number 6 >>
   8  09/29 Test7              Testing message 7<<This is message number 7 >>
   9  09/29 Test8              Testing message 8<<This is message number 8 >>
  10  09/29 Test9              Testing message 9<<This is message number 9 >>
  11  09/29 Test10             Testing message 10<<This is message number 10 >>
  12+ 09/29 Test1              Testing message 1<<This is message number 1 >>
EOF

run_prog sortm -textfield subject -limit 0 -nolimit
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -textfield subject -limit 0 (subject sort)
cat >"$expected" <<EOF
   1  09/29 Test11             Re: common subject<<This is message number 11 >>
   2  09/28 Test12             common subject<<This is message number 12 >>
   3  09/29 Test1              Testing message 1<<This is message number 1 >>
   4  09/29 Test10             Testing message 10<<This is message number 10 >>
   5  09/29 Test2              Testing message 2<<This is message number 2 >>
   6  09/29 Test3              Testing message 3<<This is message number 3 >>
   7  09/29 Test4              Testing message 4<<This is message number 4 >>
   8  09/29 Test5              Testing message 5<<This is message number 5 >>
   9  09/29 Test6              Testing message 6<<This is message number 6 >>
  10  09/29 Test7              Testing message 7<<This is message number 7 >>
  11  09/29 Test8              Testing message 8<<This is message number 8 >>
  12+ 09/29 Test9              Testing message 9<<This is message number 9 >>
EOF

refile 2-7 +inbox; refile 1 +inbox; folder -pack last >/dev/null
run_prog sortm -textfield subject -limit 0
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -textfield -limit 0 (text sort)
cat >"$expected" <<EOF
   1  09/29 Test10             Testing message 10<<This is message number 10 >>
   2  09/29 Test11             Re: common subject<<This is message number 11 >>
   3  09/28 Test12             common subject<<This is message number 12 >>
   4  09/29 Test1              Testing message 1<<This is message number 1 >>
   5  09/29 Test2              Testing message 2<<This is message number 2 >>
   6  09/29 Test3              Testing message 3<<This is message number 3 >>
   7  09/29 Test4              Testing message 4<<This is message number 4 >>
   8  09/29 Test5              Testing message 5<<This is message number 5 >>
   9  09/29 Test6              Testing message 6<<This is message number 6 >>
  10  09/29 Test7              Testing message 7<<This is message number 7 >>
  11  09/29 Test8              Testing message 8<<This is message number 8 >>
  12+ 09/29 Test9              Testing message 9<<This is message number 9 >>
EOF

refile 2-7 +inbox; refile 1 +inbox; folder -pack last >/dev/null
run_prog sortm -textfield from -limit 0
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -textfield with finite -limit that doesn't cover message 13
cat >$MH_TEST_DIR/Mail/inbox/13 <<EOF || exit 1
From: Test13 <test13@example.com>
To: Some User <user@example.com>
Date: Tue, 31 Oct 2006 23:59:59
Subject: Re: common subject

This is message number 13
EOF

cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test11             Re: common subject<<This is message number 11 >>
   3  09/29 Test5              Testing message 5<<This is message number 5 >>
   4  09/29 Test6              Testing message 6<<This is message number 6 >>
   5  09/29 Test7              Testing message 7<<This is message number 7 >>
   6  09/29 Test8              Testing message 8<<This is message number 8 >>
   7  09/29 Test9              Testing message 9<<This is message number 9 >>
   8  09/29 Test1              Testing message 1<<This is message number 1 >>
   9  09/29 Test2              Testing message 2<<This is message number 2 >>
  10  09/29 Test3              Testing message 3<<This is message number 3 >>
  11  09/29 Test4              Testing message 4<<This is message number 4 >>
  12+ 09/29 Test10             Testing message 10<<This is message number 10 >>
  13  10/31 Test13             Re: common subject<<This is message number 13 >>
EOF

refile 2-7 +inbox; refile 1 +inbox; folder -pack last >/dev/null
run_prog sortm -textfield subject -limit 10
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -textfield with finite -limit that does cover message 13
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test11             Re: common subject<<This is message number 11 >>
   3  10/31 Test13             Re: common subject<<This is message number 13 >>
   4  09/29 Test1              Testing message 1<<This is message number 1 >>
   5  09/29 Test2              Testing message 2<<This is message number 2 >>
   6  09/29 Test3              Testing message 3<<This is message number 3 >>
   7  09/29 Test4              Testing message 4<<This is message number 4 >>
   8  09/29 Test10             Testing message 10<<This is message number 10 >>
   9  09/29 Test5              Testing message 5<<This is message number 5 >>
  10  09/29 Test6              Testing message 6<<This is message number 6 >>
  11  09/29 Test7              Testing message 7<<This is message number 7 >>
  12  09/29 Test8              Testing message 8<<This is message number 8 >>
  13+ 09/29 Test9              Testing message 9<<This is message number 9 >>
EOF

refile 2-7 +inbox; refile 1 +inbox; folder -pack last >/dev/null
run_prog sortm -textfield subject -limit 60
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -notextfield
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2  09/29 Test10             Testing message 10<<This is message number 10 >>
   3  09/29 Test5              Testing message 5<<This is message number 5 >>
   4  09/29 Test6              Testing message 6<<This is message number 6 >>
   5  09/29 Test7              Testing message 7<<This is message number 7 >>
   6  09/29 Test8              Testing message 8<<This is message number 8 >>
   7  09/29 Test9              Testing message 9<<This is message number 9 >>
   8  09/29 Test1              Testing message 1<<This is message number 1 >>
   9  09/29 Test2              Testing message 2<<This is message number 2 >>
  10  09/29 Test3              Testing message 3<<This is message number 3 >>
  11  09/29 Test4              Testing message 4<<This is message number 4 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
  13+ 10/31 Test13             Re: common subject<<This is message number 13 >>
EOF

refile 2-7 +inbox; refile 1 +inbox; folder -pack last >/dev/null
run_prog sortm -textfield subject -limit 60 -notextfield
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -noall
run_test 'sortm -noall' 'sortm: must specify messages to sort with -noall'

# check -all
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2+ 09/29 Test10             Testing message 10<<This is message number 10 >>
   3  09/29 Test5              Testing message 5<<This is message number 5 >>
   4  09/29 Test6              Testing message 6<<This is message number 6 >>
   5  09/29 Test7              Testing message 7<<This is message number 7 >>
   6  09/29 Test8              Testing message 8<<This is message number 8 >>
   7  09/29 Test9              Testing message 9<<This is message number 9 >>
   8  09/29 Test1              Testing message 1<<This is message number 1 >>
   9  09/29 Test2              Testing message 2<<This is message number 2 >>
  10  09/29 Test3              Testing message 3<<This is message number 3 >>
  11  09/29 Test4              Testing message 4<<This is message number 4 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
  13  10/31 Test13             Re: common subject<<This is message number 13 >>
EOF

refile first +inbox; folder -pack >/dev/null
run_prog sortm -noall -all
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -check
cat >"$expected" <<EOF
sortm: can't parse date field in message 14, will use file modification time
sortm: errors found, no messages sorted
EOF

sed -e 's/Sep//' $MH_TEST_DIR/Mail/inbox/7 > $MH_TEST_DIR/Mail/inbox/14
set +e
run_prog sortm -check >"$actual" 2>&1
set -e
check "$expected" "$actual"

cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2+ 09/29 Test10             Testing message 10<<This is message number 10 >>
   3  09/29 Test5              Testing message 5<<This is message number 5 >>
   4  09/29 Test6              Testing message 6<<This is message number 6 >>
   5  09/29 Test7              Testing message 7<<This is message number 7 >>
   6  09/29 Test8              Testing message 8<<This is message number 8 >>
   7  09/29 Test9              Testing message 9<<This is message number 9 >>
   8  09/29 Test1              Testing message 1<<This is message number 1 >>
   9  09/29 Test2              Testing message 2<<This is message number 2 >>
  10  09/29 Test3              Testing message 3<<This is message number 3 >>
  11  09/29 Test4              Testing message 4<<This is message number 4 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
  13  10/31 Test13             Re: common subject<<This is message number 13 >>
  14  01/00 Test9              Testing message 9<<This is message number 9 >>
EOF
scan -width 80 >"$actual"
check "$expected" "$actual"

# check -nocheck
cat >"$expected" <<EOF
   1  09/28 Test12             common subject<<This is message number 12 >>
   2+ 09/29 Test10             Testing message 10<<This is message number 10 >>
   3  09/29 Test5              Testing message 5<<This is message number 5 >>
   4  09/29 Test6              Testing message 6<<This is message number 6 >>
   5  09/29 Test7              Testing message 7<<This is message number 7 >>
   6  09/29 Test8              Testing message 8<<This is message number 8 >>
   7  09/29 Test9              Testing message 9<<This is message number 9 >>
   8  09/29 Test1              Testing message 1<<This is message number 1 >>
   9  09/29 Test2              Testing message 2<<This is message number 2 >>
  10  09/29 Test3              Testing message 3<<This is message number 3 >>
  11  09/29 Test4              Testing message 4<<This is message number 4 >>
  12  09/29 Test11             Re: common subject<<This is message number 11 >>
  13  10/31 Test13             Re: common subject<<This is message number 13 >>
  14  01/00 Test9              Testing message 9<<This is message number 9 >>
EOF

sed -e 's/Sep//' $MH_TEST_DIR/Mail/inbox/7 > $MH_TEST_DIR/Mail/inbox/14
run_test 'sortm -check -nocheck' \
         "sortm: can't parse date field in message 14, will use file \
modification time"
scan -width 80 >"$actual"
check "$expected" "$actual"


exit ${failed:-0}
