#!/bin/sh
######################################################
#
# Test POP support in inc
# Only tests checking of local maildrop, does not
# test checking of POP server.
#
######################################################

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

#
# Some extra stuff we need for POP support
#

TESTUSER=testuser
TESTPASS=testuserpass
arith_eval 64001 + `id -u` % 1000
testport=$arith_val

HOME="${MH_TEST_DIR}"; export HOME
netrc="${HOME}/.netrc"
echo "default login ${TESTUSER} password ${TESTPASS}" > "$netrc"
chmod 600 "$netrc"

expected=$MH_TEST_DIR/$$.expected
expected_err=$MH_TEST_DIR/$$.expected_err
actual=$MH_TEST_DIR/$$.actual
actual_err=$MH_TEST_DIR/$$.actual_err
testmessage=$MH_TEST_DIR/testmessage

cat > $testmessage <<EOM
Received: From somewhere
From: No Such User <nosuch@example.com>
To: Some Other User <someother@example.com>
Subject: Hello
Date: Sun, 17 Dec 2006 12:13:14 -0500

Hey man, how's it going?
.
Hope you're doing better.
EOM

pid=`"${MH_OBJ_DIR}/test/fakepop" "$testmessage" "$testport" \
			"$TESTUSER" "$TESTPASS"`

run_test "inc -user ${TESTUSER} -host 127.0.0.1 -port $testport -width 80" \
	"Incorporating new mail into inbox...

  11+ 12/17 No Such User       Hello<<Hey man, how's it going? . Hope you're do"
check $testmessage `mhpath +inbox 11` 'keep first'

# check -pack
pid=`"${MH_OBJ_DIR}/test/fakepop" "$testmessage" "$testport" \
			"$TESTUSER" "$TESTPASS"`

touch "$MH_TEST_DIR/inc.mbox"
run_test "inc -user ${TESTUSER} -host 127.0.0.1 -port $testport -width 80 \
          -pack $MH_TEST_DIR/inc.mbox" \
	"Incorporating new mail into (null)...

   1  12/17 No Such User       Hello<<Hey man, how's it going? . Hope you're do"
run_test "inc -file $MH_TEST_DIR/inc.mbox -truncate -width 80" \
	"Incorporating new mail into inbox...

  11+ 12/17 No Such User       Hello<<Hey man, how's it going? . Hope you're do"

check $testmessage `mhpath +inbox 11` 'keep first'
rm -f "$MH_TEST_DIR/inc.mbox" "$MH_TEST_DIR/.inc.map" "$netrc"

# check credentials: file
# Redirect stdin so that inc doesn't wait on the user if it can't
# read the netrc file, even though that shouldn't happen.
TESTUSER=differenuser
TESTPASS=differentpass

netrc="${HOME}/.mhnetrc"
echo "default login ${TESTUSER} password ${TESTPASS}" > "$netrc"
chmod 600 "$netrc"
echo "credentials: file:${netrc}" >>$MH

pid=`"${MH_OBJ_DIR}/test/fakepop" "$testmessage" "$testport" \
			"$TESTUSER" "$TESTPASS"`

touch "$MH_TEST_DIR/inc.mbox"
run_test "inc -user ${TESTUSER} -host 127.0.0.1 -port $testport -width 65 \
          -pack $MH_TEST_DIR/inc.mbox" \
	"Incorporating new mail into (null)...

   1  12/17 No Such User       Hello<<Hey man, how's it going? ." </dev/null
run_test "inc -file $MH_TEST_DIR/inc.mbox -truncate -width 65" \
	"Incorporating new mail into inbox...

  11+ 12/17 No Such User       Hello<<Hey man, how's it going? ." </dev/null

check $testmessage `mhpath +inbox 11`
rm -f "$MH_TEST_DIR/inc.mbox" "$MH_TEST_DIR/.inc.map" "$netrc"

exit ${failed:-0}
