#!/bin/sh
######################################################
#
# Test ali
#
######################################################

. "$MH_TEST_COMMON"

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


runandcheck "ali -help" <<!
Usage: ali [switches] aliases ...
  switches are:
  -file aliasfile
  -[no]list
  -[no]normalize
  -[no]user
  -Version
  -help
!


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


# check with no arguments and no AliasFile profile component
runandcheck 'ali' <<!
!

# check with nonexistent alias file
runandcheck 'ali -file nonexistent || :' <<!
ali: aliasing error in nonexistent: unable to read 'nonexistent'
!

cat >"${MH_TEST_DIR}/Mail/aliases" <<EOF
me: me@example.com
rush: geddy, alex, neil
geddy: geddy@example.com
alex: alex@example.com
neil: neil@example.com
EOF

# check -file
runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases" <<!
me: me@example.com
rush: geddy@example.com, alex@example.com, neil@example.com
geddy: geddy@example.com
alex: alex@example.com
neil: neil@example.com
!

# check for a specific alias
runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases rush" <<!
geddy@example.com, alex@example.com, neil@example.com
!

# check for a specific alias that doesn't exist
runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases nonexistent" <<!
nonexistent
!

# check -list
runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases -list" <<!
me: me@example.com
rush: geddy@example.com
      alex@example.com
      neil@example.com
geddy: geddy@example.com
alex: alex@example.com
neil: neil@example.com
!

# check -nolist, which is the default
runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases -list -nolist" <<!
me: me@example.com
rush: geddy@example.com, alex@example.com, neil@example.com
geddy: geddy@example.com
alex: alex@example.com
neil: neil@example.com
!

# check -user
runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases -user geddy@example.com" <<!
rush, geddy
!

# check -nouser
runandcheck \
  "ali -file ${MH_TEST_DIR}/Mail/aliases -user -nouser geddy@example.com" <<!
geddy@example.com
!

# check expansion of first address of blind list [Bug #15604]
cat >"${MH_TEST_DIR}/Mail/aliases" <<EOF
rush: Rush: geddy, alex, neil
geddy: geddy@example.com
alex: alex@example.com
neil: neil@example.com
EOF

runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases rush" <<!
Rush: geddy@example.com, alex@example.com, neil@example.com
!

# check that aliases followed by ; are not expanded [Bug #15604]
cat >"${MH_TEST_DIR}/Mail/aliases" <<EOF
rush: Rush: geddy, alex, neil;
geddy: geddy@example.com
alex: alex@example.com
neil: neil@example.com
EOF

runandcheck "ali -file ${MH_TEST_DIR}/Mail/aliases rush" <<!
Rush: geddy@example.com, alex@example.com, neil;
!


exit $failed
