#!/bin/sh
######################################################
#
# Test mhical
#
######################################################

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

#### Make sure that html-to-text conversion is what we expect.
require_locale en_US.UTF-8 en_US.UTF8 en_US.utf-8 en_US.utf8

#### Disable colorized output.
TERM=dumb; export TERM

expected="$MH_TEST_DIR/test-mhical$$.expected"
expected_err="$MH_TEST_DIR/test-mhical$$.expected_err"
actual="$MH_TEST_DIR/test-mhical$$.actual"
actual_err="$MH_TEST_DIR/test-mhical$$.actual_err"


# check -help
start_test "-help"
cat >"$expected" <<EOF
Usage: mhical [switches]
  switches are:
  -reply accept|decline|tentative
  -cancel
  -form formatfile
  -(forma)t string
  -infile
  -outfile
  -[no]contenttype
  -unfold
  -debug
  -version
  -help
EOF

#### Skip nmh intro text.
run_prog mhical -help 2>&1 | sed '/^$/,$d' >"$actual"
check "$expected" "$actual"


# check -version
start_test "-version"
case `mhical -version` in
  mhical\ --*) ;;
  *) printf '%s: mhical -version generated unexpected output\n' "$0" >&2
     failed=`expr ${failed:-0} + 1`;;
esac


# check display with timezone that only has standard time
start_test "display with timezone that only has standard time"
cat >"$expected" <<'EOF'
Summary: Santa Watch
Description: See Santa here first!
At: Wed, 24 Dec 2014 12:00 +0000
To: Fri, 25 Dec 2015 11:59
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
VERSION:2.0
PRODID:test-mhical

BEGIN:VTIMEZONE
TZID:MHT-12
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:+1200
TZOFFSETTO:+1200
END:STANDARD
END:VTIMEZONE

BEGIN:VEVENT
DTSTAMP:20141224T140426Z
DTSTART;TZID=MHT-12:20141225T000000
DTEND;TZID=MHT-12:20151225T235959
SUMMARY:Santa Watch
DESCRIPTION: See Santa here first!
END:VEVENT

END:VCALENDAR
EOF

TZ=UTC mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check display with 24 hour time format and -outfile
start_test "display with 24 hour time format and -outfile"
cat >"$expected" <<'EOF'
Summary: 4 pm meeting
At: Mon, 05 Jan 2015 16:00
To: Mon, 05 Jan 2015 16:30
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
VERSION:2.0
PRODID:test-mhical

BEGIN:VEVENT
DTSTAMP:20150101T162400Z
DTSTART:20150105T160000
DTEND:20150105T163000
SUMMARY:4 pm meeting
END:VEVENT

END:VCALENDAR
EOF

mhical -outfile "$MH_TEST_DIR/test1.txt" <"$MH_TEST_DIR/test1.ics"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check display with 12 hour time format and -infile
start_test "display with 12 hour time format and -infile"
cat >"$expected" <<'EOF'
Summary: 4 pm meeting
At: Mon, 05 Jan 2015  4:00 PM
To: Mon, 05 Jan 2015  4:30 PM
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
VERSION:2.0
PRODID:test-mhical

BEGIN:VEVENT
DTSTAMP:20150101T162800Z
DTSTART:20150105T160000
DTEND:20150105T163000
SUMMARY:4 pm meeting
END:VEVENT

END:VCALENDAR
EOF

mhical -form mhical.12hour -infile "$MH_TEST_DIR/test1.ics" \
       >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check display with DST
start_test "display with DST"
cat >"$expected" <<'EOF'
Method: REQUEST
Organizer: Requester
Summary: Big Meeting
Location: The Office
At: Mon, 05 Jan 2015 08:00 -0500
To: Mon, 05 Jan 2015 09:00
Attendees: Requestee
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:Microsoft Exchange Server 2010
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=FALSE;CN=Requestee
 :MAILTO:requestee@example.com
DESCRIPTION;LANGUAGE=en-US:\n\n
SUMMARY;LANGUAGE=en-US:Big Meeting
DTSTART;TZID=Eastern Standard Time:20150105T080000
DTEND;TZID=Eastern Standard Time:20150105T090000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20141231T235959Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:The Office
X-MICROSOFT-CDO-APPT-SEQUENCE:0
X-MICROSOFT-CDO-OWNERAPPTID:-0123456789
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY
X-MICROSOFT-CDO-ALLDAYEVENT:FALSE
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-CDO-INSTTYPE:0
X-MICROSOFT-DISALLOW-COUNTER:FALSE
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

TZ=EST mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check timezone boundary at transition to daylight saving time
start_test "timezone boundary at transition to daylight saving time"
# The default mhical display format doesn't show the timezone for the
# To: time, but it is different than that of the At: time.
cat >"$expected" <<'EOF'
Summary: EST to EDT
At: Sun, 09 Mar 2014 01:59 -0500
To: Sun, 09 Mar 2014 03:30
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
VERSION:2.0
PRODID:test-mhical
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20150101T000000Z
DTSTART;TZID=Eastern Standard Time:20140309T015959
DTEND;TZID=Eastern Standard Time:20140309T023000
Summary: EST to EDT
END:VEVENT
END:VCALENDAR
EOF

TZ=EST5EDT mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check -format, and that timezone is correct in end time
start_test "-format, and that timezone is correct in end time"
cat >"$expected" <<'EOF'
Sun, 09 Mar 2014 03:30:00 -0400
EOF

TZ=EST5EDT mhical -format '%(pretty{dtend})' \
    -infile "$MH_TEST_DIR/test1.ics" -outfile "$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check timezone boundary at transition from daylight saving time
start_test "timezone boundary at transition from daylight saving time"
cat >"$expected" <<'EOF'
Summary: EDT to EST
At: Sun, 02 Nov 2014 01:59 -0400
To: Sun, 02 Nov 2014 02:00
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
VERSION:2.0
PRODID:test-mhical
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20150101T000000Z
DTSTART;TZID=Eastern Standard Time:20141102T015959
DTEND;TZID=Eastern Standard Time:20141102T020000
Summary: EDT to EST
END:VEVENT
END:VCALENDAR
EOF

TZ=EST5EDT mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check -format, and that timezone is correct in end time
start_test "-format, and that timezone is correct in end time"
cat >"$expected" <<'EOF'
Sun, 02 Nov 2014 02:00:00 -0500
EOF

TZ=EST5EDT mhical -format '%(pretty{dtend})' \
    -infile "$MH_TEST_DIR/test1.ics" -outfile "$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


printf 'Local-Mailbox: Requestee2 <requestee2@example.com>\n' >> "$MH"

# check accept of request
start_test "accept of request"
cat >"$expected" <<'EOF'
BEGIN:VCALENDAR
METHOD:REPLY
PRODID:nmh mhical v0.1
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;PARTSTAT=ACCEPTED;CN=Requestee2:MAILTO:requestee2@example.com
SUMMARY;LANGUAGE=en-US:Accepted: test request
DTSTART;TZID=Eastern Standard Time:20150105T090000
DTEND;TZID=Eastern Standard Time:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
END:VEVENT
END:VCALENDAR
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:test-mhical
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee1
 :MAILTO:requestee1@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee2
 :MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:test request
DTSTART;TZID=Eastern Standard Time:20150105T090000
DTEND;TZID=Eastern Standard Time:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20150101T171600Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

mhical -reply accept <"$MH_TEST_DIR/test1.ics" | egrep -v '^DTSTAMP:' \
       >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"

# check accept of multiple vevent requests in single vcalendar
start_test "accept of multiple vevent requests in single vcalendar"
cat >"$expected" <<'EOF'
BEGIN:VCALENDAR
METHOD:REPLY
PRODID:nmh mhical v0.1
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;PARTSTAT=ACCEPTED;CN=Requestee2:MAILTO:requestee2@example.com
SUMMARY;LANGUAGE=en-US:Accepted: test request
DTSTART;TZID=Eastern Standard Time:20150105T090000
DTEND;TZID=Eastern Standard Time:20150105T093000
UID:0123456790
CLASS:PUBLIC
PRIORITY:5
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
END:VEVENT

BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;PARTSTAT=ACCEPTED;CN=Requestee2:MAILTO:requestee2@example.com
SUMMARY;LANGUAGE=en-US:Accepted: test request
DTSTART;TZID=Eastern Standard Time:20150105T130000
DTEND;TZID=Eastern Standard Time:20150105T134500
UID:0123456791
CLASS:PUBLIC
PRIORITY:5
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
END:VEVENT
END:VCALENDAR
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:test-mhical
VERSION:2.0

BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16010101T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE

BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee1
 :MAILTO:requestee1@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee2
 :MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:test request
DTSTART;TZID=Eastern Standard Time:20150105T090000
DTEND;TZID=Eastern Standard Time:20150105T093000
UID:0123456790
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20150101T171600Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT

BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee2
 :MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:test request
DTSTART;TZID=Eastern Standard Time:20150105T130000
DTEND;TZID=Eastern Standard Time:20150105T134500
UID:0123456791
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20150101T171600Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT

END:VCALENDAR
EOF

mhical -reply accept <"$MH_TEST_DIR/test1.ics" | egrep -v '^DTSTAMP:' \
       >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check decline of request
start_test "decline of request"
cat >"$expected" <<'EOF'
BEGIN:VCALENDAR
METHOD:REPLY
PRODID:nmh mhical v0.1
VERSION:2.0
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;PARTSTAT=DECLINED;CN=Requestee2:MAILTO:requestee2@example.com
SUMMARY;LANGUAGE=en-US:Declined: test request
DTSTART:20150105T090000
DTEND:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
END:VEVENT
END:VCALENDAR
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:test-mhical
VERSION:2.0
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee1
 :MAILTO:requestee1@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee2
 :MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:test request
DTSTART:20150105T090000
DTEND:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20150101T171600Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

mhical -reply decline <"$MH_TEST_DIR/test1.ics" | egrep -v '^DTSTAMP:' \
       >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check response of tentative to request, and -nocontenttype
start_test "response of tentative to request, and -nocontenttype"
cat >"$expected" <<'EOF'
BEGIN:VCALENDAR
METHOD:REPLY
PRODID:nmh mhical v0.1
VERSION:2.0
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;PARTSTAT=TENTATIVE;CN=Requestee2:MAILTO:requestee2@example.com
SUMMARY;LANGUAGE=en-US:Tentative: test request
DTSTART:20150105T090000
DTEND:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
END:VEVENT
END:VCALENDAR
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:test-mhical
VERSION:2.0
BEGIN:VEVENT
ORGANIZER;CN=Requester:MAILTO:requester@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee1
 :MAILTO:requestee1@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee2
 :MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:test request
DTSTART:20150105T090000
DTEND:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20150101T171600Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

mhical -reply tentative -contenttype -nocontenttype \
       -infile "$MH_TEST_DIR/test1.ics" | egrep -v '^DTSTAMP:' \
       >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# check cancel request, and -contenttype
start_test "cancel request, and -contenttype"
cat >"$expected" <<'EOF'
Content-Type: text/calendar; method="CANCEL"; charset="UTF-8"

BEGIN:VCALENDAR
METHOD:CANCEL
PRODID:nmh mhical v0.1
VERSION:2.0
BEGIN:VEVENT
ORGANIZER;CN=Requestee2:MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee1
 :MAILTO:requestee1@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:Cancelled:test request
DTSTART:20150105T090000
DTEND:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
TRANSP:OPAQUE
STATUS:CANCELLED
SEQUENCE:1
LOCATION;LANGUAGE=en-US:
END:VEVENT
END:VCALENDAR
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:test-mhical
VERSION:2.0
BEGIN:VEVENT
ORGANIZER;CN=Requestee2:MAILTO:requestee2@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee1
 :MAILTO:requestee1@example.com
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Requestee3
 :MAILTO:requestee3@example.com
SUMMARY;LANGUAGE=en-US:test request
DTSTART:20150105T090000
DTEND:20150105T093000
UID:0123456789
CLASS:PUBLIC
PRIORITY:5
DTSTAMP:20150101T171600Z
TRANSP:OPAQUE
STATUS:CONFIRMED
SEQUENCE:0
LOCATION;LANGUAGE=en-US:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

mhical -cancel -contenttype <"$MH_TEST_DIR/test1.ics" | egrep -v '^DTSTAMP:' \
       >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# Check TZID name wrapped with quotes, this used to cause a segfault.
start_test "TZID name wrapped with quotes, this used to cause a segfault."
cat >"$expected" <<'EOF'
Method: REQUEST
Summary: Quoted timezone ID
At: Wed, 01 Jan 2014 00:00
To: Wed, 01 Jan 2014 01:00
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
PRODID:Zimbra-Calendar-Provider
VERSION:2.0
METHOD:REQUEST
SUMMARY:Quoted timezone ID
BEGIN:VTIMEZONE
TZID:Etc/GMT
BEGIN:STANDARD
DTSTART:19710101T000000
TZOFFSETTO:-0000
TZOFFSETFROM:-0000
TZNAME:GMT
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART:20140101T000000
DTEND;TZID="Etc/GMT":20140101T010000
SEQUENCE:0
END:VEVENT
END:VCALENDAR
EOF

TZ=UTC mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"


# Check start datetime without a time, and missing end datetime.
start_test "start datetime without a time, and missing end datetime."
cat >"$expected" <<'EOF'
Method: PUBLISH
Summary: Test datetime without time
At: Wed, 30 Dec 2015 00:00
To: Wed, 30 Dec 2015 23:59
EOF

cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
BEGIN:VCALENDAR
VERSION:2.0
PRODID:icalendar-ruby
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
DTSTAMP:20151208T204350Z
UID:0123456789
DTSTART;VALUE=DATE:20151230
CLASS:PRIVATE
SUMMARY:Test datetime without time
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

TZ=UTC mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
check "$expected" "$MH_TEST_DIR/test1.txt"
rm -f "$MH_TEST_DIR/test1.ics"


# check missing final newline
start_test "missing final newline"

cat >"$expected" <<'EOF'
Summary: test
Description: this file does not end with a newline
At: Sun, 14 May 2017 13:00 +0000
To: Sun, 14 May 2017 14:00
EOF

printf %s \
"BEGIN:VCALENDAR
VERSION:2.0
PRODID:RevenueWell
BEGIN:VEVENT
DTSTAMP:20170514T122300Z
DTSTART:20170514T130000Z
DTEND:20170514T140000Z
SUMMARY:test
DESCRIPTION:this file does not end with a newline
END:VEVENT
END:VCALENDAR" | TZ=UTC mhical >"$actual"
check "$expected" "$actual"


# check invalid line folding
start_test "invalid line folding"

cat >"$expected" <<'EOF'
Summary: test
Description: this file does not end with a newline
At: Sun, 14 May 2017 13:00 +0000
To: Sun, 14 May 2017 14:00
EOF

cat >"$expected_err" <<'EOF'
mhical: syntax error, unexpected ICAL_COMMA, expecting ICAL_COLON after " this line is not folded"
EOF

set +e
printf %s \
"BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//TDP v3.0//EN
BEGIN:VEVENT
DTSTAMP:20170514T122300Z
DTSTART:20170514T130000Z
DTEND:20170514T140000Z
SUMMARY:test
DESCRIPTION:this file does not end with a newline
and this line is not folded,
END:VEVENT
END:VCALENDAR" | TZ=UTC mhical >"$actual" 2>"$actual_err"
set -e
check "$expected" "$actual"
check "$expected_err" "$actual_err"


# check null input
start_test "null input"
check_exit '-eq 0' mhical </dev/null


finish_test
exit $failed
