#!/bin/sh
# ---------------------------------------------------------------------------
# TEST-REM
#
# This file runs an acceptance test for Remind.  To use it, type:
#      sh test-rem  OR make test
# in the build directory.
#
# This file is part of REMIND.
# Copyright (C) 1992-2025 Dianne Skoll
# SPDX-License-Identifier: GPL-2.0-only
# ---------------------------------------------------------------------------

DIR=`dirname $0`
cd $DIR
if test $? != 0 ; then
    echo ""
    echo "Unable to cd $DIR" >&2
    echo ""
    exit 1
fi

if test `id -u` = 0 ; then
    echo ""
    echo "*** Please do not run the test suite as root; it will fail."
    echo ""
    exit 1
fi

REMIND="../src/remind --flush -q"
OUT="../tests/test.out"
CMP="../tests/test.cmp"
# Set a known timezone so moon phases show up in predictable places
TZ=UTC
export TZ

# Colorize output iff stdout is a tty
stty <&1 > /dev/null 2>&1
if test $? = 0; then
    RED="[31m"
    GRN="[32m"
    NRM="[0m"
else
    RED=""
    GRN=""
    NRM=""
fi

# See if our version of sleep supports fractional sleeps
sleep 0.2 > /dev/null 2>&1
if test $? = 0 ; then
    DELAY=0.2
else
    DELAY=1
fi

# If we're already in a utf-8 locale, do
# nothing; otherwise, set LC_ALL
OK=0
if echo $LC_ALL | grep -i utf-8 > /dev/null 2>&1 ; then
    OK=1
fi

if test -z "$LC_ALL" ; then
    if echo $LANG | grep -i utf-8 > /dev/null 2>&1 ; then
        export LC_ALL="$LANG"
        OK=1
    fi
fi

if test "$OK" = 0 ; then
    export LC_ALL=C.UTF-8
    export LANG=C.UTF-8
fi

chmod 000 include_dir/04cantread.rem
TEST_GETENV="foo bar baz" ; export TEST_GETENV
echo "Test 1" > $OUT
echo "" >> $OUT
echo "Running main tests"
$REMIND -e -dxte ../tests/test.rem 16 feb 1991 12:13 2>&1 | grep -v -a 'TimetIs64bit' >> $OUT 2>&1
echo "" >> $OUT
echo "Test 2" >> $OUT
echo "" >> $OUT
$REMIND -p -l ../tests/test2.rem 1 aug 2007 >> $OUT 2>&1
echo "" >> $OUT
echo "Test 3" >> $OUT
echo "" >> $OUT
$REMIND -s ../tests/test2.rem 1 aug 2007 >> $OUT 2>&1
echo "" >> $OUT
echo "Test 4" >> $OUT
echo "" >> $OUT
$REMIND -sa ../tests/test2.rem 1 aug 2007 >> $OUT 2>&1
echo "Test 5" >> $OUT
echo "" >> $OUT
$REMIND -p -l -b0 ../tests/test3.rem 1 aug 2007 >> $OUT 2>&1
echo "Test 6" >> $OUT
echo "" >> $OUT
$REMIND -p -l -b1 ../tests/test3.rem 1 aug 2007 >> $OUT 2>&1
echo "Test 7" >> $OUT
echo "" >> $OUT
$REMIND -p -l -b2 ../tests/test3.rem 1 aug 2007 >> $OUT 2>&1

echo "Test 8" >> $OUT
echo "" >> $OUT
$REMIND -df -p -l -b2 ../tests/include_dir 1 aug 2007 >> $OUT 2>&1

echo "Test 9" >> $OUT
echo "" >> $OUT
$REMIND -df -p -l -b2 ../tests/nonexistent_include_dir 1 aug 2007 >> $OUT 2>&1
$REMIND -df -p -l -b2 ../tests/include_dir_no_rems 1 aug 2007 >> $OUT 2>&1
$REMIND -df -p -l -b2 ../tests/include_test.rem 1 aug 2007 >> $OUT 2>&1

chmod 644 include_dir/04cantread.rem

# Feb 29 bug
echo "Feb 29 Bug Test" >> $OUT
echo 'REM Sun 29 Feb MSG [$T]' | $REMIND -dt - 1 feb 2021 >> $OUT 2>&1

# Day Weekday Year out-of-year bug
echo "Mon 31 Dec Bug Test" >> $OUT
echo 'REM Mon 31 2021 MSG [$T]' | $REMIND -dt - 31 dec 2021 >> $OUT 2>&1

echo "Color Test" >> $OUT
$REMIND -ccl ../tests/colors.rem 1 aug 2007 >> $OUT 2>&1

echo "ANSI Color Test" >> $OUT
$REMIND ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@0 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@2 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@0,0 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@1,0 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@2,0 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@0,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@1,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@2,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@0,,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@1,,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@2,,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@0,0,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@1,0,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@2,0,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@0,1,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@1,1,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND -@2,1,1 ../tests/ansicolors.rem 1 Jan 2022 >> $OUT 2>&1

echo '$AddBlankLines test' >> $OUT
$REMIND ../tests/blanks.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND '-i$AddBlankLines=1' ../tests/blanks.rem 1 Jan 2022 >> $OUT 2>&1
$REMIND '-i$AddBlankLines=0' ../tests/blanks.rem 1 Jan 2022 >> $OUT 2>&1

echo "MON WKDAY DAY across year test" >> $OUT
echo 'REM Mon 29 Dec MSG x' | $REMIND -dt - 1 Jan 2000 >> $OUT 2>&1

echo "Sort Test" >> $OUT
(echo "REM AT 12:00 MSG Untimed"; echo "REM MSG Timed") | $REMIND -q -gaaa - 1 Jan 2000 >> $OUT 2>&1
(echo "REM AT 12:00 MSG Untimed"; echo "REM MSG Timed") | $REMIND -q -gaaad - 1 Jan 2000 >> $OUT 2>&1

echo "Purge Test" >> $OUT
$REMIND -j999 ../tests/purge_dir/f1.rem 3 Feb 2012 >> $OUT 2>&1
echo "F1" >> $OUT
cat ../tests/purge_dir/f1.rem.purged >> $OUT
echo "F2" >> $OUT
cat ../tests/purge_dir/f2.rem.purged >> $OUT
echo "F3" >> $OUT
cat ../tests/purge_dir/f3.rem.purged >> $OUT

rm -f ../tests/purge_dir/*.rem.purged >> $OUT 2>&1

echo "Hush Purge Test" >> $OUT
$REMIND -h -j999 ../tests/purge_dir/f1.rem 3 Feb 2012 >> $OUT 2>&1
echo "F1" >> $OUT
cat ../tests/purge_dir/f1.rem.purged >> $OUT
echo "F2" >> $OUT
cat ../tests/purge_dir/f2.rem.purged >> $OUT
echo "F3" >> $OUT
cat ../tests/purge_dir/f3.rem.purged >> $OUT

rm -f ../tests/purge_dir/*.rem.purged >> $OUT 2>&1

$REMIND ../tests/runtest.rem >> $OUT 2>&1

$REMIND -p ../tests/shade.rem 1 August 2009 | ../src/rem2ps -e -l -c3 >> $OUT 2>&1
$REMIND -pp ../tests/shade.rem 1 August 2009 | ../src/rem2ps -e -l -c3 >> $OUT 2>&1

TZ=America/Toronto $REMIND ../tests/sunmoon.rem 1 Jan 2011 >> $OUT 2>&1

# Test -a vs -aa
$REMIND -q -a - 1 Jan 2012 9:00 <<'EOF' >> $OUT 2>&1
REM 1 Jan 2012 AT 8:00 MSG 8am: Should not show up
REM 1 Jan 2012 AT 9:00 MSG 9am: Should not show up
REM 1 Jan 2012 AT 10:00 MSG 10am: Should not show up
MSG [$DontTrigAts]
EOF

$REMIND -q -a -a - 1 Jan 2012 9:00 <<'EOF' >> $OUT 2>&1
REM 1 Jan 2012 AT 8:00 MSG 8am: Should not show up
REM 1 Jan 2012 AT 9:00 MSG 9am: Should show up
REM 1 Jan 2012 AT 10:00 MSG 10am: Should show up
MSG [$DontTrigAts]
EOF

# OMITFUNC should indicate nonconst_expr
$REMIND -pp - 1 jan 2012 9:00 <<'EOF' >> $OUT 2>&1
REM Mon OMITFUNC foo MSG bar
EOF

# Test default color
$REMIND -pppq - 1 Jan 2012 9:00 <<'EOF' >> $OUT 2>&1
REM 2 MSG Normal
SET $DefaultColor "255 0 0"
REM 3 \
    MSG %"Red%" on the calendar!
SET $DefaultColor "-1 -1 -1"
REM 4 MSG Normal
# Should give an error - split on two lines to test line number reporting
SET $DefaultColor \
    "256 0 0"
EOF

# Test default color with weekly calendar
$REMIND -pq+ - 1 Jan 2012 9:00 <<'EOF' >> $OUT 2>&1
REM 2 MSG Normal
SET $DefaultColor "255 0 0"
REM 3 MSG %"Red%" on the calendar!
SET $DefaultColor "-1 -1 -1"
REM 4 MSG Normal
# Should give an error
SET $DefaultColor "256 0 0"
EOF

# Test stdout
$REMIND - 1 jan 2012 <<'EOF' >> $OUT 2>&1
BANNER %
MSG STDOUT is a: [stdout()]%
EOF

$REMIND - 1 jan 2012 <<'EOF' 2>&1 | cat >> $OUT
BANNER %
MSG STDOUT is a: [stdout()]%
EOF

# Test -@ option
$REMIND -w,0,0 -@0,,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@0
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@0,0,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@0,0
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@0,1,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@0,1
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@1,,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@1
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@1,0,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@1,0
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@1,1,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@1,1
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@2,,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@2
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@2,0,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@2,0
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF
$REMIND -w,0,0 -@2,1,1 -c - 1 Jan 2020 <<'EOF' >> $OUT 2>&1
SET $SuppressLRM 1
rem 1 SPECIAL COLOR 0   0   0 BLACK
rem 2 SPECIAL COLOR 0   0  65 BLUE
rem 3 SPECIAL COLOR 0  65   0 GREEN
rem 4 SPECIAL COLOR 0  65  65 CYAN
rem 5 msg -@2,1
rem 6 SPECIAL SHADE 255 255 0
rem 7 SPECIAL SHADE 255 0 255
rem 8 SPECIAL SHADE 0 255 255
rem 15 SPECIAL COLOR 65  0   0 RED
rem 16 SPECIAL COLOR 65  0  65 MAGENTA
rem 17 SPECIAL COLOR 65 65   0 YELLOW
rem 18 SPECIAL COLOR 65 65  65 WHITE
rem 8 SPECIAL COLOR 0   0   0 BLACK
rem 9 SPECIAL COLOR 0   0  200 BRIGHT BLUE
rem 10 SPECIAL COLOR 0  200   0 BRIGHT GREEN
rem 11 SPECIAL COLOR 0  200  200 BRIGHT CYAN
rem 22 SPECIAL COLOR 200  0   0 BRIGHT RED
rem 23 SPECIAL COLOR 200  0  200 BRIGHT MAGENTA
rem 24 SPECIAL COLOR 200 200   0 BRIGHT YELLOW
rem 25 SPECIAL COLOR 200 200  200 BRIGHT WHITE
EOF

$REMIND -w128 -c ../tests/utf-8.rem 1 Nov 2019 >> $OUT
$REMIND -c ../tests/test-addomit.rem 1 Sep 2021 >> $OUT

$REMIND -cu ../tests/utf-8.rem 1 Nov 2019 >> $OUT
$REMIND -cu '-i$SuppressLRM=1' ../tests/utf-8.rem 1 Nov 2019 >> $OUT

TZ=America/Toronto $REMIND -dxe ../tests/tz.rem >> $OUT 2>&1
TZ=Europe/Berlin $REMIND -dxe ../tests/tz.rem >> $OUT 2>&1

$REMIND ../tests/soleq.rem 1 April 2044 >> $OUT 2>&1

# Test that banner is printed on every iteration
echo "MSG Should be three banners." | $REMIND - 2022-10-20 '*3' >> $OUT 2>&1

# Test the -tn option
echo "REM May 23 +10 MSG Orange %b" | $REMIND - 2023-05-21 >> $OUT 2>&1
echo "REM May 23 +10 MSG Quux %b" | $REMIND -t1 - 2023-05-21 >> $OUT 2>&1
echo "REM May 23 +10 MSG Cabbage %b" | $REMIND -t2 - 2023-05-21 >> $OUT 2>&1
echo "REM May 23 MSG Banana %b" | $REMIND - 2023-05-21 >> $OUT 2>&1
echo "REM May 23 MSG Carrot %b" | $REMIND -t1 - 2023-05-21 >> $OUT 2>&1
echo "REM May 23 MSG Apple %b" | $REMIND -t2 - 2023-05-21 >> $OUT 2>&1

# Test the -tz option
echo "REM May 22 +10 MSG Foo %b" | $REMIND - 2023-05-21 >> $OUT 2>&1
echo "REM May 22 +10 MSG Bar %b" | $REMIND -tz - 2023-05-21 >> $OUT 2>&1

# World-writable file
rm -rf include_dir/ww
touch include_dir/ww
chmod 0666 include_dir/ww
$REMIND include_dir/ww >> $OUT 2>&1
rm -rf include_dir/ww

# World-writable directory
mkdir -p include_dir/ww
touch include_dir/ww/0.rem
chmod 0777 include_dir/ww
$REMIND include_dir/ww >> $OUT 2>&1
rm -rf include_dir/ww

# This segfaulted in 04.02.03
$REMIND -h '-imsgprefix(x)="foo"' /dev/null >> $OUT 2>&1

# Test --version long option
$REMIND --version >> $OUT 2>&1

# Test queueing.  Because eventstart depends on the actual system
# date, we use the --test flag to fake the date and time.
# We can't use $REMIND here because it includes the -q flag.
echo JSONQUEUE | ../src/remind --flush --test -z0 ../tests/queue1.rem >> $OUT 2>&1
echo QUEUE     | ../src/remind --flush --test -zj ../tests/queue1.rem >> $OUT 2>&1

# Test for leap year bug that was fixed
$REMIND -dte - 28 Feb 2024 <<'EOF' >> $OUT 2>&1
BANNER %
REM 29 MSG One
REM 29 Feb MSG two
REM 29 2024 MSG three
REM 29 Feb 2024 MSG four
REM Thursday 29 MSG One
REM Thursday 29 Feb MSG two
REM Thursday 29 2024 MSG three
REM Thursday 29 Feb 2024 MSG four
REM Wednesday 29 MSG One
REM Wednesday 29 Feb MSG two
REM Wednesday 29 2024 MSG three
REM Wednesday 29 Feb 2024 MSG four
REM Friday 29 MSG One
REM Friday 29 Feb MSG two
REM Friday 29 2024 MSG three
REM Friday 29 Feb 2024 MSG four
EOF

$REMIND -dte - 1 Mar 2024 <<'EOF' >> $OUT 2>&1
BANNER %
REM 29 MSG One
REM 29 Feb MSG two
REM 29 2024 MSG three
REM 29 Feb 2024 MSG four
REM Thursday 29 MSG One
REM Thursday 29 Feb MSG two
REM Thursday 29 2024 MSG three
REM Thursday 29 Feb 2024 MSG four
REM Wednesday 29 MSG One
REM Wednesday 29 Feb MSG two
REM Wednesday 29 2024 MSG three
REM Wednesday 29 Feb 2024 MSG four
REM Friday 29 MSG One
REM Friday 29 Feb MSG two
REM Friday 29 2024 MSG three
REM Friday 29 Feb 2024 MSG four
EOF

$REMIND -dte - 28 Feb 2025 <<'EOF' >> $OUT 2>&1
BANNER %
REM 29 MSG One
REM 29 Feb MSG two
REM 29 2025 MSG three
REM 29 Feb 2025 MSG four
REM Thursday 29 MSG One
REM Thursday 29 Feb MSG two
REM Thursday 29 2025 MSG three
REM Thursday 29 Feb 2025 MSG four
REM Wednesday 29 MSG One
REM Wednesday 29 Feb MSG two
REM Wednesday 29 2025 MSG three
REM Wednesday 29 Feb 2025 MSG four
REM Friday 29 MSG One
REM Friday 29 Feb MSG two
REM Friday 29 2025 MSG three
REM Friday 29 Feb 2025 MSG four
EOF

$REMIND -dte - 1 Mar 2025 <<'EOF' >> $OUT 2>&1
BANNER %
REM 29 MSG One
REM 29 Feb MSG two
REM 29 2025 MSG three
REM 29 Feb 2025 MSG four
REM Thursday 29 MSG One
REM Thursday 29 Feb MSG two
REM Thursday 29 2025 MSG three
REM Thursday 29 Feb 2025 MSG four
REM Wednesday 29 MSG One
REM Wednesday 29 Feb MSG two
REM Wednesday 29 2025 MSG three
REM Wednesday 29 Feb 2025 MSG four
REM Friday 29 MSG One
REM Friday 29 Feb MSG two
REM Friday 29 2025 MSG three
REM Friday 29 Feb 2025 MSG four
EOF


(echo 'BANNER %'; echo 'REM 29 MSG No bug') | $REMIND -dt - 29 Feb 2024 >> $OUT 2>&1

$REMIND -ifoo - <<'EOF' >> $OUT 2>&1
BANNER %
DUMP
EOF

$REMIND '-i$AddBlankLines' - <<'EOF' >> $OUT 2>&1
BANNER %
DUMP $AddBlankLines
EOF

$REMIND ../tests/expr.rem >> $OUT 2>&1

$REMIND -  <<'EOF' >> $OUT 2>&1
PUSH
POP
PUSH
PUSH
POP
POP
PUSH
PUSH
POP
PUSH
POP
PUSH
POP
EOF

$REMIND ../tests/if1.rem 2020-03-03 >> $OUT 2>&1

# Test ONCE with a timestamp file
rm -f ../tests/once.timestamp
$REMIND ../tests/test-once.rem >> $OUT 2>&1
$REMIND ../tests/test-once.rem >> $OUT 2>&1
$REMIND ../tests/test-once.rem >> $OUT 2>&1
tail -n+2 ../tests/once.timestamp >> $OUT 2>&1
rm -f ../tests/once.timestamp

$REMIND - < ../tests/test-once.rem >> $OUT 2>&1
$REMIND - < ../tests/test-once.rem >> $OUT 2>&1
$REMIND - < ../tests/test-once.rem >> $OUT 2>&1
tail -n+2 ../tests/once.timestamp >> $OUT 2>&1
rm -f ../tests/once.timestamp

# Newlines in calendar output
(echo 'SET $SuppressLRM 1'; echo 'REM 16 MSG foo%_bar%_baz wookie quux apple   %_    %_    %_  blech'; echo "REM 16 MSG ANOTHER") | $REMIND -c -w80 - 1 sep 1990 >> $OUT 2>&1

# Dedupe feature
$REMIND -c ../tests/dedupe.rem 1 November 2023 >> $OUT 2>&1
$REMIND -q ../tests/dedupe.rem 8 November 2023 >> $OUT 2>&1

# Remove references to SysInclude, which is build-specific
grep -F -v -a '$SysInclude' < $OUT > $OUT.1 && mv -f $OUT.1 $OUT

# If "man" accepts the --warnings flag, test all the man pages.
RUNMAN=0
man man 2>&1 | grep -e --warnings > /dev/null 2>&1
if test "$?" = 0 ; then
    echo "Checking syntax of man pages"
    RUNMAN=1
fi
for i in ../man/*.1 ; do
    echo "Checking $i..." >> $OUT
    if test "$RUNMAN" = 1 ; then
        man --warnings=w $i 2>>$OUT 1>/dev/null
    fi
done

# If we have "spellintian", run it against man pages
cat <<'EOF' >> $OUT

If spellintian is installed, its output (if any) follows...

EOF

spellintian /dev/null < /dev/null > /dev/null 2>&1
if test $? = 0 ; then
    echo "Linting man pages"
    # we grep out some false-positives from rem2ps
    spellintian --picky ../man/*.1 2>&1 | grep -v -F 'MinX MinX (duplicate word)' | grep -v -F 'MinY MinY (duplicate word)' | grep -v -F 'show show (duplicate word)' >> $OUT 2>&1
fi

# If we have "hunspell", run it against man pages
cat <<'EOF' >> $OUT

If hunspell is installed, its output (if any) follows...

EOF

HUNSPELL="hunspell -d en_US -l -p manpage-personal-dict"

$HUNSPELL /dev/null < /dev/null > /dev/null 2>&1
if test $? = 0; then
    echo "Spell-checking man pages, WHATSNEW and README.md"
    cat ../man/*.1 | $HUNSPELL -n >> $OUT 2>&1
    cat ../docs/WHATSNEW | $HUNSPELL >> $OUT 2>&1
    cat ../README.md | $HUNSPELL >> $OUT 2>&1
    # If we have pod2man, check rem2pdf and rem2html man pages too
    pod2man < ../rem2html/rem2html.in > /dev/null 2>&1
    if test $? = 0 ; then
        pod2man < ../rem2html/rem2html.in | $HUNSPELL -n >> $OUT 2>&1
        pod2man < ../rem2pdf/bin/rem2pdf.in | $HUNSPELL -n >> $OUT 2>&1
    fi
fi

echo "" >> $OUT
# Test --print-tokens long option
$REMIND --print-tokens < /dev/null >> $OUT 2>&1

# Torture test #2
$REMIND ../tests/torture2.rem >> $OUT 2>&1

# Expression error-reporting
$REMIND -de - 1 Feb 2024 <<'EOF' >> $OUT 2>&1
set a 8 * "]]]" & 6
msg [8 * "]]]" & 6] is weird
set a 9 *
set a 9 * ]
EOF

# Translation template generateion
$REMIND -h - 1 Feb 2024 <<'EOF' >> $OUT 2>&1
TRANSLATE GENERATE
EOF

# Make sure stupidly-long translations of "am" and "pm" can't cause a
# segmentation fault
$REMIND -c - 1 Feb 2024 <<'EOF' >> $OUT 2>&1
TRANS "am" "alsdkjalksdj alksjd alksdj alksjd laksjd laksjd laksjd laksjd laksjd laksjd laksjd laksjd lkasjd laksjd laksjd lkajs dlkajs dlkasj dlkasjd lkajsd lkajs dlkasjd lkasj dlkajsd lkasjd lkasjd laksjd laksjd laksjd alskdj alskdj alksdj alksdj alskdj alksdj aslkdj"
TRANS "pm" "oiwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwjwwwwwwwwwwwwwwwjwpqoejkpqwojepqowjepqojwepqowjepqowjepqowjepqowjepqowjepqowjepqojwepqowjepqowjepqowjepqowjepqowjeqpweoj"

REM WED AT 11:00 MSG wookie
REM WED AT 13:00 MSG blah
EOF

# The INFO keyword
$REMIND -pp - 1 Feb 2024 <<'EOF' >> $OUT 2>&1
REM Wed INFO "Location: here" INFO "Summary: Nope" MSG Meeting [triginfo("location")] %<summary> %<nonexist> [triginfo("cabbage")]
EOF

# Invalid info strings
$REMIND - 1 Feb 2024 <<'EOF' >> $OUT 2>&1
REM Thu INFO "Invalid" MSG wookie
REM Fri INFO ": foo" MSG blat
REM Sun INFO "foo bar baz : blork" MSG uua

# Duplicate info string
REM Sat INFO "Location: here" INFO "location: there" MSG blort
EOF

# Test parsing of quoted strings and the "escape" function
$REMIND - 1 Feb 2024 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0
TRANSLATE "foo" "test: \\\" \a\b\f\\n\r\t\v\x3\x1b haha"
REM msg foo translation = %(foo)
FLUSH
set a "vartest: \\\" \a\b\f\\n\r\t\v\x3\x1b haha"
REM msg a = [a]
FLUSH
dump a
set b escape(a)
REM msg b = [b]
set b escape(a,1)
REM msg b = [b]
FLUSH
dump b
FLUSH
set a "\x"
dump a
FLUSH
set a "\xPOO"
dump a
FLUSH
set a "\x0"
set a "\x00"
set a "\x0P"
set a "\x00P"
EOF

# Test diagnostics when using a timed substitution without an AT clause
$REMIND - 1 Feb 2024 1:00 <<EOF >> $OUT 2>&1
REM MSG %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 hahaha
EOF

# Test translate table dumping
$REMIND - 1 Feb 2024 <<EOF >> $OUT 2>&1
TRANSLATE "\x03" "BREAK"
TRANSLATE DUMP
EOF

$REMIND -ppp - 1 Feb 2024 <<EOF >> $OUT 2>&1
TRANSLATE "\x03" "BREAK"
EOF

# SCANFROM should be preserved even if it is today
$REMIND -ppp - 1 Feb 2024 <<EOF >> $OUT 2>&1
REM SCANFROM 2024-02-01 MSG Preserve SCANFROM
EOF
# Languages
for i in ../include/lang/??.rem ; do
    $REMIND -r -q "-ii=\"$i\"" ../tests/tstlang.rem 1 Feb 2024 13:34 >> $OUT 2>&1
done

# Fix for $DefaultColor bug with remind -s
$REMIND -s - 1 Feb 2024 >> $OUT 2>&1 <<'EOF'
SET $DefaultColor "255 0 0"
REM Wed MSG Wookie
EOF

# Test year-folding
TZ=America/Toronto $REMIND -dx ../tests/yearfold.rem >> $OUT 2>&1

# Test unused-variable debugging
$REMIND -du - <<'EOF' >> $OUT 2>&1
set a 1
set b a*2
set c "What"
if c
   set d "Told ya!"
endif

set x 44
set x 45
set y 1000
unset y
EOF

# Test RETURN statement
$REMIND ../tests/ret1.rem 4 June 2000 >> $OUT 2>&1
$REMIND ../tests/ret1.rem 5 June 2000 >> $OUT 2>&1
$REMIND ../tests/ret1.rem 7 June 2000 >> $OUT 2>&1
$REMIND -s ../tests/ret1.rem 1 June 2000 >> $OUT 2>&1

# Make sure all the include files are ok
echo "Checking syntax of *.rem files in ../include"
find ../include -type f -name '*.rem' | while read x; do $REMIND -du -n $x 1 Jan 2024 2>>$OUT 1>/dev/null; done

# Test todos
echo "" >> $OUT 2>&1
echo "Testing TODOS in agenda mode" >> $OUT 2>&1
$REMIND ../tests/todos.rem 2025-08-13 >> $OUT 2>&1
echo "" >> $OUT 2>&1
echo "Testing TODOS in calendar mode" >> $OUT 2>&1
$REMIND -s ../tests/todos.rem 2025-08-13 >> $OUT 2>&1
echo "" >> $OUT 2>&1
echo "Testing TODOS in calendar mode with completed todos hidden" >> $OUT 2>&1
$REMIND -s --hide-completed-todos ../tests/todos.rem 2025-08-13 >> $OUT 2>&1
echo "Testing TODOS and JSON mode" >> $OUT 2>&1
$REMIND --json ../tests/todos.rem 2025-08-13 >> $OUT 2>&1

echo "Testing proper redirection of RUN stdout in JSON mode... here's stdout" >> $OUT 2>&1
$REMIND --json ../tests/json-redirect.rem 1 Jan 2025 >> $OUT 2>/dev/null

echo "... and here is stderr" >> $OUT 2>&1
$REMIND --json ../tests/json-redirect.rem 1 Jan 2025 > /dev/null 2>> $OUT

# Test %: substitution sequence in all the languages
for i in ../include/lang/??.rem ; do
    $REMIND "-ii=\"$i\"" -p - 2025-08-13 <<'EOF' 2>&1 | grep -a 2025/ >> $OUT
DO [i]
REM TODO 2025-08-13 MSG %(LANGID) Task1%:
REM TODO 2025-08-13 COMPLETE-THROUGH 2025-08-12 MSG %(LANGID) Task2%:
REM TODO 2025-08-13 COMPLETE-THROUGH 2025-08-13 MSG %(LANGID) Task3%:
EOF
done

$REMIND -q ../tests/safety.rem 2025-08-13 >> $OUT 2>&1

# Test --max-expr-complexity
$REMIND -dh --max-expr-complexity=1000000 - 2025-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0
FSET fib(n) iif(n < 3, 1, fib(n-1) + fib(n-2))

REM MSG fib(10) = [fib(10)]
REM MSG fib(15) = [fib(15)]
REM MSG fib(20) = [fib(20)]
REM MSG fib(21) = [fib(21)]
REM MSG fib(22) = [fib(22)]
REM MSG fib(23) = [fib(23)]
REM MSG fib(24) = [fib(24)]
REM MSG fib(25) = [fib(25)]
REM MSG fib(30) = [fib(30)]

EOF

# Test hex constants
$REMIND -dh - 2025-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0
set a 0xfe + 0xef
rem msg a = [a]; hex(a) = [hex(a)]
rem msg hex(-1) = [hex(-1)]
set a 0x7fff
rem msg a = [a]; hex(a) = [hex(a)]
EOF

# Test proper truncation by "dumpvars"
$REMIND -h - 2025-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0
set a "¢" * 41
set b "D" * 41
set c mbchar(0x1F642) * 41
DUMP
set a "¢" * 40
set b "D" * 40
set c mbchar(0x1F642) * 40
DUMP
EOF

# Test $WarningLevel
$REMIND -de -h - 2025-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0
REM Monday ADDOMIT MSG woohoo!
SET $WarningLevel "06.01.02"
REM Tuesday ADDOMIT MSG Cabbage.
SET $WarningLevel "cabbage"
SET $WarningLevel today()
SET $WarningLevel "3.1.4"
EOF

# Make sure shell() and RUN redirect stdin to /dev/null
(echo 'set a shell("cat")'; sleep $DELAY; echo 'rem msg devnull test [a]') | $REMIND - 2025-10-01 >> $OUT 2>&1
(echo 'REM RUN cat'; sleep $DELAY; echo 'rem msg devnull test b') | $REMIND - 2025-10-01 >> $OUT 2>&1

# For this one, we need to force an error so we make sure the error is
# reported in -stdin- and not cat|
(echo 'INCLUDECMD cat'; sleep $DELAY; echo 'set a 1/0') | $REMIND - 2025-10-01 >> $OUT 2>&1
cmp -s $OUT $CMP
if [ "$?" = "0" ]; then
   echo "Remind:  Acceptance tests ${GRN}PASSED${NRM}"
   exit 0
else
   echo "Remind:  Acceptance tests ${RED}FAILED${NRM}"
   echo ""
   echo "Examine the file test.out to see where it differs from the"
   echo "reference file test.cmp.  Here are the first 200 lines of"
   echo "diff -u test.out test.cmp"
   echo ""
   diff -u $OUT $CMP | head -n 200
   echo ""
   exit 1
fi

