<?php
/*
* Copyright (c) 2009-2010 ATMAIL. All rights reserved
* See http://atmail.com/license.php for license agreement
*/

/*
* CalDAV test
*
*/
include ('../../library/Atmail/CalDav/caldav-client.php');
include ('../../library/Atmail/CalDav/caldav-support.php');

if($argc != 3)
	die("This utility will purge ALL data from a target user account.\nUsage: caldav_purge.php CALDAV_STORE_URL USERNAME\n");

$password = readline('Password:');

print("=== CalDAV Client Purge ===\n");

$total_items = 0;

// new caldav client
$cal = new CalDAVClient( $argv[1], $argv[2], $password, "calendar" );
$cal->SetUserAgent("Atmail 6.0");

print("Deleting all events...\n");
$cal->DoDELETERequest("*", "*");//$event_obj['etag']);

// grab all the events we can
$events = $cal->GetEvents(null,null);

print(count($events) . " events.\n");

$delete_start = microtime();
// delete all events currently for the user
foreach ( $events AS $k => $event ) {
	$event_obj = ical_load_entry($event);
	$cal->DoDELETERequest($event_obj['VCALENDAR']['VEVENT']['UID'] . ".ics", "*");//$event_obj['etag']);
	$total_items++;
}

$delete_end = microtime();
print("OK.\n");

print("Deleting all todos...\n");

// grab all the events we can
$events = $cal->GetTodos('','', true, true);

print(count($events) . " todos.\n");

$delete_todos_start = microtime();
// delete all events currently for the user
foreach ( $events AS $k => $event ) {
	$event_obj = ical_load_entry($event);
	$cal->DoDELETERequest($event_obj['VCALENDAR']['VTODO']['UID'] . ".ics", "*");//$event_obj['etag']);
	$total_items++;
}
$delete_todos_end = microtime();
print("OK.\n");


print("--------------------------------------\n");
list($delete_start_ms, $delete_start_sec) = explode(" ",$delete_start);
list($delete_end_ms, $delete_end_sec) = explode(" ",$delete_end);
$delete_start = $delete_start_sec + $delete_start_ms;
$delete_end = $delete_end_sec + $delete_end_ms;
printf("Delete Runtime = %.02f seconds\tDelete/Sec = %.02f\n", ($delete_end - $delete_start), $total_items / ($delete_end - $delete_start));
