/* gcc a.c -o a -O0 -g `pkg-config libical --cflags --libs` && ./a */

#include <libical/ical.h>
#include <stdio.h>

int test(const char *str)
{
	icalcomponent *comp;
	icalproperty *p;
	icalattach *attach = NULL;
	const char *attach_url = NULL;

	comp = icalparser_parse_string (str);
	if (!comp) {
		printf ("parse failed\n");
		return 1;
	}
	
	for (p = icalcomponent_get_first_property (comp, ICAL_ATTACH_PROPERTY);
	     p;
	     p = icalcomponent_get_next_property (comp, ICAL_ATTACH_PROPERTY)) {
		attach = icalproperty_get_attach (p);
		if (!icalattach_get_is_url (attach)) {
			attach = NULL;
			printf ("not an url attachment\n");
		} else {
			attach_url = icalattach_get_url (attach);
			printf ("%x ", attach_url); fflush (stdout);
			printf ("url:'%s'\n", attach_url);
		}
	}

	icalcomponent_free (comp);

	return 0;
}

int main (int argc, char **argv)
{
	const char
		*good =
		"BEGIN:VEVENT\n"
		"UID:20110722T130009Z-2198-1000-1-2368@azur\n"
		"DTSTAMP:20110722T130009Z\n"
		"SUMMARY:From Evolution with attachment\n"
		"DTSTART;TZID=/freeassociation.sourceforge.net/Tzfile/Europe/Paris:\n"
		" 20110722T150000\n"
		"DTEND;TZID=/freeassociation.sourceforge.net/Tzfile/Europe/Paris:\n"
		" 20110722T153000\n"
		"CREATED:20110722T130029Z\n"
		"LAST-MODIFIED:20110722T130029Z\n"
		"SEQUENCE:2\n"
		"CLASS:PUBLIC\n"
		"TRANSP:OPAQUE\n"
		"ATTACH;X-EVOLUTION-CALDAV-ATTACHMENT-NAME=protocol-tests.properties;\n"
		" X-S1CS-ATTACH-ID=/xxxx/xxxxxxxx/xxxxxxxx/dropbox/20110722T130009Z-2198-100\n"
		" 0-1-2368@azur/attach-1311338374154-54;X-S1CS-FILESIZE=418:https:\n"
		" //xxxxx.xxxx.xxx.xxx/xxx/xxxx/xxxxxxxx/calendar/dropbox/20110722T130009Z-2\n"
		" 198-1000-1-2368@azur/attach-1311338374154-54\n"
		"END:VEVENT\n",

		*bad = 
		"BEGIN:VEVENT\n"
		"UID:A28028F2-4AA8-4C83-8551-6780E6ACB17E\n"
		"DTSTAMP:20110722T125509Z\n"
		"SUMMARY:from Apple iCal with attachment\n"
		"DTSTART;TZID=Europe/Paris:20110722T180000\n"
		"DTEND;TZID=Europe/Paris:20110722T184500\n"
		"CREATED:20110722T125449Z\n"
		"SEQUENCE:3\n"
		"TRANSP:OPAQUE\n"
		"ATTACH;VALUE=URI;\n"
		" X-S1CS-ATTACH-ID=/home/xxxxxxxx@xxxx.xxx.xxx/dropbox/A28028F2-4AA8-4C83-85\n"
		" 51-6780E6ACB17E.dropbox/protocol-tests.properties;\n"
		" X-S1CS-FILENAME=protocol-tests.properties:https:\n"
		" //xxxxx.xxxx.xxx.xxx/xxx/home/xxxxxxxx@xxxx.xxx.xxx/dropbox/A28028F2-4AA8-\n"
		" 4C83-8551-6780E6ACB17E.dropbox/protocol-tests.properties\n"
		"BEGIN:VALARM\n"
		"ATTACH;VALUE=URI:Basso\n"
		"ACTION:AUDIO\n"
		"TRIGGER;VALUE=DURATION:-PT15M\n"
		"X-WR-ALARMUID:28BFC73F-395F-4703-B5ED-58C01D49F25E\n"
		"END:VALARM\n"
		"END:VEVENT\n";

	int res;

	res = test (good);
	if (res == 0)
		res = test (bad);

	printf ("Done with code %d.\n", res);

	return res;
}

