#!/bin/sh
export LC_ALL=C.UTF-8
export LANG=C.UTF-8

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

# 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

# We alias "remind" here so that we don't inadvertently add code that
# runs the system-installed verion of Remind rather than
# ../src/remind.  This trick was suggested by Jochen Sprickerhof
alias remind="echo You should be using ../src/remind explicitly in test-rem >&2; exit 1"

REMIND="../src/remind -h -q --flush"
OUT=../tests/tz.out
CMP=../tests/tz.cmp

echo "Running timezone tests"
cp /dev/null $OUT

TZ=Europe/Amsterdam $REMIND - 2025-09-03@14:00 <<'EOF' >> $OUT 2>&1
SET $AddBlankLines=0
BANNER %
REM Fri AT 23:30 TZ America/Toronto +1000 SATISFY [$Td == 13] MSG Fri 13th @23:30 Eastern is %a %2 here
DEBUG +x
SET a $T
SET b $Tt
SET c trigtz()
SET d trigtime()
SET e trigtimetz()
SET f trigeventstart()
SET g trigeventstarttz()
DEBUG -x
EOF

TZ=Europe/Amsterdam $REMIND - 2026-02-14@14:00 <<'EOF' >> $OUT 2>&1
SET $AddBlankLines=0
BANNER %
REM Fri AT 23:30 TZ America/Toronto +1000 SATISFY [$Td == 13] MSG Fri 13th @23:30 Eastern is %a %2 here
DEBUG +x
SET a $T
SET b $Tt
SET c trigtz()
SET d trigtime()
SET e trigtimetz()
SET f trigeventstart()
SET g trigeventstarttz()
DEBUG -x
EOF

TZ=Europe/Amsterdam $REMIND - 2026-02-15@14:00 <<'EOF' >> $OUT 2>&1
SET $AddBlankLines=0
BANNER %
REM Fri AT 23:30 TZ America/Toronto +1000 SATISFY [$Td == 13] MSG Fri 13th @23:30 Eastern is %a %2 here
SET a $T
SET b $Tt
SET c trigtz()
SET d trigtime()
SET e trigtimetz()
SET f trigeventstart()
SET g trigeventstarttz()
EOF

TZ=America/Toronto $REMIND -p12 - 2025-01-01 <<'EOF' 2>&1 | grep Locally >> $OUT 2>&1
REM Second Thursday AT 15:00 TZ Europe/Amsterdam MSG Locally: %3
EOF

TZ=America/Toronto $REMIND -g - 2026-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0
SET new   '2026-01-18@14:53'
SET first '2026-01-25@23:48'
SET full  '2026-01-03@05:04'
SET last  '2026-01-10@10:49'

REM [new]   +100 MSG New Moon      %*l %3.

SET a trigtz()
SET b trigtime()
SET c trigtimetz()
SET d trigeventstart()
SET e trigeventstarttz()

REM MSG trigtz = [a], trigtime = [b], trigtimetz = [c], trigeventstart = [d], trigeventstarttz = [e]

REM [first] +100 MSG First Quarter %*l %3.
REM [full]  +100 MSG Full Moon     %*l %3.
REM [last]  +100 MSG Last Quarter  %*l %3.

SET new   tzconvert(new,   "America/Toronto", "Australia/Sydney")
SET first tzconvert(first, "America/Toronto", "Australia/Sydney")
SET full  tzconvert(full,  "America/Toronto", "Australia/Sydney")
SET last  tzconvert(last,  "America/Toronto", "Australia/Sydney")

REM [new]   +100 TZ Australia/Sydney MSG New Moon      %*l %3. (*)

SET a trigtz()
SET b trigtime()
SET c trigtimetz()
SET d trigeventstart()
SET e trigeventstarttz()

REM MSG trigtz = [a], trigtime = [b], trigtimetz = [c], trigeventstart = [d], trigeventstarttz = [e]

REM [first] +100 TZ Australia/Sydney MSG First Quarter %*l %3. (*)
REM [full]  +100 TZ Australia/Sydney MSG Full Moon     %*l %3. (*)
REM [last]  +100 TZ Australia/Sydney MSG Last Quarter  %*l %3. (*)

REM MSG new=[new] first=[first] full=[full] last=[last]

EOF

TZ=America/Los_Angeles $REMIND - 2026-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0

REM AT 13:33 MSG Whatsup? %*l %3.
REM AT 13:33 TZ "" MSG Whatsup? %*l %3.
EOF

TZ=America/Los_Angeles $REMIND - 2026-01-01 <<'EOF' >> $OUT 2>&1
BANNER %
SET $AddBlankLines 0

REM AT 13:33 TZ America/Los_Angeles TZ UTC MSG Whatsup? %*l %3.
REM AT 13:33 TZ "" TZ America/Los_Angeles MSG Whatsup? %*l %3.
REM TZ UTC MSG Borked
EOF

TZ=America/Toronto $REMIND -dx - 2025-09-01@01:00 <<'EOF' >> $OUT 2>&1
SET a tzconvert('2025-09-01@14:44', "", "Europe/Berlin")
SET a tzconvert('2025-09-01@14:44', "America/Toronto", "Europe/Berlin")
SET a tzconvert('2025-09-01@14:44', "Europe/Berlin", "")
SET a tzconvert('2025-09-01@14:44', "Europe/Berlin")
SET a tzconvert('2025-09-01@14:44', "", "")
SET a tzconvert('2025-09-01@14:44', "")

SET b evaltrig("Wednesday at 23:00 TZ Australia/Sydney")
SET b evaltrig("Wednesday at 23:00 TZ Europe/Amsterdam")
SET b evaltrig("Wednesday at 23:00 TZ America/Los_Angeles")
set b evaltrig("Wednesday TZ Europe/Amsterdam")

debug +t
set c trig("Mon at 00:00 TZ Australia/Sydney",\
           "Tue at 00:00 TZ America/Toronto",\
           "Wed at 00:00 TZ Europe/Amsterdam",\
           "Thu at 00:00 TZ America/Los_Angeles",\
           "Fri at 00:00 TZ Australia/Sydney",\
           "Sat at 00:00 TZ Australia/Sydney",\
           "Sun at 00:00 TZ Australia/Sydney", \
           "at 14:42")
debug -t

EOF

TZ=America/Toronto $REMIND -s - 2025-09-01@00:00 <<'EOF' >> $OUT 2>&1
REM 2025-09-24 AT 09:00 TZ America/Denver MSG Dr. Smith
EOF

echo "" >> $OUT
# Bug reported by Jin Chen
TZ=Australia/Sydney $REMIND -s - 2025-10-01 <<'EOF' >> $OUT 2>&1
REM Wednesday AT 14:00 TZ America/Toronto MSG 2PM Eastern
EOF

echo "" >> $OUT
# And the inverse...
TZ=America/Toronto $REMIND -s - 2025-10-01 <<'EOF' >> $OUT 2>&1
REM Wednesday AT 14:00 TZ Australia/Sydney MSG 2PM Sydney
EOF

echo "" >> $OUT
# Every day and a SATISFY reminder
TZ=America/Toronto $REMIND -s - 2025-10-01 <<'EOF' >> $OUT 2>&1
REM AT 04:44 TZ America/Los_Angeles MSG 04:44 every day in L.A.
REM 2 AT 05:00 TZ America/Los_Angeles MSG 05:00 on 2nd in L.A.
REM Wed AT 06:00 TZ America/Los_Angeles MSG 06:00 on Wed in L.A.
REM Wed AT 23:00 TZ America/Los_Angeles MSG 23:00 on Wed in L.A.
REM AT 09:00 TZ America/Los_Angeles SATISFY [!($Td % 3)] MSG 09:00 every third day in L.A.
REM AT 22:00 TZ America/Los_Angeles SATISFY [!($Td % 3)] MSG 22:00 every third day in L.A.
EOF

echo "" >> $OUT
# And the inverse
TZ=America/Los_Angeles $REMIND -s - 2025-10-01 <<'EOF' >> $OUT 2>&1
REM AT 04:44 TZ America/Toronto MSG 04:44 every day in T.O.
REM 2 AT 05:00 TZ America/Toronto MSG 05:00 on 2nd in T.O.
REM Wed AT 06:00 TZ America/Toronto MSG 06:00 on Wed in T.O.
REM Wed AT 01:00 TZ America/Toronto MSG 01:00 on Wed in T.O.
REM AT 09:00 TZ America/Toronto SATISFY [!($Td % 3)] MSG 09:00 every third day in T.O.
REM AT 02:00 TZ America/Toronto SATISFY [!($Td % 3)] MSG 02:00 every third day in T.O.
EOF

# Semi-exhaustive checking...
OFFSETS="-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12"
cat <<'EOF' >> $OUT

NOTE NOTE NOTE: The notation UTC+n denotes a time zone n hours
WEST of UTC, and UTC-n denotes a time zone n hours EAST of
UTC... the opposite of what you might think.  The zone
UTC+1 is two hours EARLIER than UTC-1.

EOF

for src in $OFFSETS; do
    srczone="UTC$src"
    for dst in $OFFSETS ; do
        dstzone="UTC$dst"
        (echo 'BANNER %'; echo "REM AT 00:00 TZ !$dstzone MSG 00:00 at $dstzone in [getenv(\"TZ\")] is %3, %r %m; diff=[(\$Tt-00:00)/60]") | TZ=$srczone $REMIND '-i$addblanklines=0' - 2025-10-09@12:00 >> $OUT 2>&1
    done
done
for src in $OFFSETS; do
    srczone="UTC$src"
    for dst in $OFFSETS ; do
        dstzone="UTC$dst"
        (echo 'BANNER %'; echo "REM AT 12:00 TZ !$dstzone MSG 12:00 at $dstzone in [getenv(\"TZ\")] is %3, %r %m; diff=[(\$Tt-12:00)/60]") | TZ=$srczone $REMIND '-i$addblanklines=0' - 2025-10-09@12:00 >> $OUT 2>&1
    done
done
for src in $OFFSETS; do
    srczone="UTC$src"
    for dst in $OFFSETS ; do
        dstzone="UTC$dst"
        (echo 'BANNER %'; echo "REM 11 ++10 AT 00:00 TZ !$dstzone MSG 00:00 on 11th at $dstzone in [getenv(\"TZ\")] is %3, %r %m; diff=[(trigdatetime()-'2025-10-11@00:00')/60]") | TZ=$srczone $REMIND '-i$addblanklines=0' - 2025-10-09@12:00 >> $OUT 2>&1
    done
done
for src in $OFFSETS; do
    srczone="UTC$src"
    for dst in $OFFSETS ; do
        dstzone="UTC$dst"
        (echo 'BANNER %'; echo "REM 11 ++10 AT 12:00 TZ !$dstzone MSG 12:00 on 11th at $dstzone in [getenv(\"TZ\")] is %3, %r %m; diff=[(trigdatetime()-'2025-10-11@12:00')/60]") | TZ=$srczone $REMIND '-i$addblanklines=0' - 2025-10-09@12:00 >> $OUT 2>&1
    done
done

cmp -s $OUT $CMP
if [ "$?" = "0" ] ; then
    echo "remind:    Time zone tests ${GRN}PASSED${NRM}"
    exit 0
else
    echo "remind:    Time zone tests ${RED}FAILED${NRM}"
    echo "Examine the file tz.out to see where it differs from the"
    echo "reference file tz.cmp.  Here are the first 200 lines of"
    echo "diff -u tz.out tz.cmp"
    echo ""
    diff -u $OUT $CMP | head -n 200
    echo ""
    exit 1
fi
