<?php

  require_once 'iCalcreator.class.php';
  require_once 'schedule-functions.php';

  # if the user has customized output, use specified, otherwise show all
  $lectures = $_GET['custom'] ? $_GET['lectures'] : array('MoWe', 'TuTh');
  $labs = $_GET['custom'] ? $_GET['labs'] : array('Mo','Tu','We','Th');

  // create a new calendar instance
  $cal = new vcalendar(array('unique_id' => 'cs.umbc.edu', 'filename' => 'calendar.ics'));

  // required of some calendar software
  $cal->setProperty('method', 'PUBLISH');
  $cal->setProperty("x-wr-calname", "CMSC 202");
  $cal->setProperty("X-WR-CALDESC", "Schedule of events for CMSC 202");
  $cal->setProperty("X-WR-TIMEZONE", "America/New_York");

  // slurp the schedule file, nuke C++ style comments & decode
  $events = json_decode(preg_replace('/^\s*\/\/.*$/m', '', file_get_contents('schedule.json')), true);

  // print_r($events);
  foreach($events as $event) {
    foreach($event['dates'] as $date) {
      $dayOfWeek = date("w", strtotime($date));
      if(shouldDisplayEvent($event, $dayOfWeek, $lectures, $labs)) {
        $e =& $cal->newComponent('vevent');
        $e->setProperty('dtstart', str_replace('-', '', $date), array('VALUE' => 'DATE'));
        $e->setProperty('LOCATION', 'UMBC');
        $e->setProperty('summary', $event['title']);
        if($event['url']) {
          $e->setProperty('description', '<a href="' . $event['url'] . '">' . $event['url'] . '</a>');
        }
      }
    }
  }

  $cal->returnCalendar();

?>
