#!/bin/sh
######################################################
#
# Test mark
#
######################################################

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' mark -

# Test sequence creation.
run_test 'mark 1 3 5 7 +inbox -sequence odd' ''
run_test 'mark 2 4 6 8 10 +inbox -sequence even' ''
run_test 'mark +inbox -seq odd -seq even -list' \
         'odd: 1 3 5 7
even: 2 4 6 8 10'

# Test add.
run_test 'mark 9 -sequence odd -add -nozero' ''
run_test 'mark -seq odd -list' 'odd: 1 3 5 7 9'

# Test delete.
run_test 'mark 9 -sequence odd -delete' ''
run_test 'mark -seq odd -list' 'odd: 1 3 5 7'

# Test that -nozero is default
run_test 'mark 9 -sequence odd -add' ''
run_test 'mark -seq odd -list' 'odd: 1 3 5 7 9'

# Test -zero
run_test 'mark 10 -s even -add -zero' ''
run_test 'mark -s even -list' 'even: 10'

# Test add prior to last message;
run_test 'mark 8 -s even -add' ''
run_test 'mark -s even -list' 'even: 8 10'

# Test list.
run_test 'mark -s odd -list' 'odd: 1 3 5 7 9'

# Try to mark message that doesn't exist.
run_test 'mark 12' "mark: message 12 doesn't exist"

# Try to mark nonexistent folder.
run_test 'mark +nonexistent' \
         "mark: unable to change directory to `mhpath +`/nonexistent: \
No such file or directory"

# Test message ranges
# Set current message for following tests.
folder +inbox 5 >/dev/null
run_test 'mark -s foo -zero cur' ''
run_test 'mark -s foo -list' 'foo: 5'
run_test 'mark -s foo -zero 5:-3' ''
run_test 'mark -s foo -list' 'foo: 3-5'
run_test 'mark -s foo -zero 5:2' ''
run_test 'mark -s foo -list' 'foo: 5-6'
run_test 'mark -s foo -zero 5=1' ''
run_test 'mark -s foo -list' 'foo: 5'
run_test 'mark -s foo -zero 5=-2' ''
run_test 'mark -s foo -list' 'foo: 4'
run_test 'mark -s foo -zero 5=7' 'mark: no such message'

run_test 'mark -s onesix -zero 1 2 3 4 5 6'
run_test 'mark -s onesix -list' 'onesix: 1-6'
run_test 'mark -s foo -zero onesix:2'
run_test 'mark -s foo -list' 'foo: 1-2'
run_test 'mark -s foo -zero onesix=3'
run_test 'mark -s foo -list' 'foo: 3'
run_test 'mark -s foo -zero onesix:-3'
run_test 'mark -s foo -list' 'foo: 4-6'
run_test 'mark -s foo -zero onesix=-3'
run_test 'mark -s foo -list' 'foo: 4'
run_test 'mark -s foo -zero onesix:8'
run_test 'mark -s foo -list' 'foo: 1-6'
run_test 'mark -s foo -zero onesix=11' 'mark: no onesix=11 message'

run_test 'mark -s foo -delete all' ''
run_test 'mark -s onesix -delete all' ''


# Test private sequence creation.
# Set current message for following tests.
folder +inbox 1 >/dev/null
run_test 'mark 1 -sequence privateseq -add -nopublic' ''
run_test 'mark -list' 'cur: 1
odd: 1 3 5 7 9
even: 8 10
privateseq (private): 1'

# Test add to private sequence.
run_test 'mark 2 -sequence privateseq -add -nopublic' ''
run_test 'mark -list' 'cur: 1
odd: 1 3 5 7 9
even: 8 10
privateseq (private): 1-2'

# Test private sequence list.
run_test 'mark -sequence privateseq -list' 'privateseq (private): 1-2'

# Test private sequence list with -public, which is apparently ignored.
run_test 'mark -sequence privateseq -list -public' 'privateseq (private): 1-2'

# Test mark on empty folder.
folder -create +other >/dev/null
run_test 'mark +other -sequence unseen all' 'mark: no messages in other'

# Test removal of indication of cur message.
mark +inbox -sequence cur -delete all
run_test 'pick -nolist cur' 'pick: no cur message'

# Check large number of sequences.
for i in 1 2 3 4 5; do
  for j in 0 1 2 3 4 5 6 7 8 9; do
    run_prog mark $i -seq seq$i${j}0 -seq seq$i${j}1 -seq seq$i${j}2 \
                     -seq seq$i${j}3 -seq seq$i${j}4 -seq seq$i${j}5 \
                     -seq seq$i${j}6 -seq seq$i${j}7 -seq seq$i${j}8 \
                     -seq seq$i${j}9
    run_test "pick seq$i${j}0 seq$i${j}1 seq$i${j}2 seq$i${j}3 seq$i${j}4 \
                   seq$i${j}5 seq$i${j}6 seq$i${j}7 seq$i${j}8 seq$i${j}9" $i
  done
done

exit $failed
