define("SHUTTLE_NOTIFY_THRESHOLD", 320);

require_once("DaemonWrapper.php");

$daemon = new DaemonWrapper("shuttle");
$daemon->start($argv);


require_once dirname(__FILE__) . "/../../../mobi-config/mobi_lib_constants.php";
require_once LIB_ROOT . "ShuttleSchedule.php";
require_once LIB_ROOT . "NextBusReader.php";
require_once LIB_ROOT . "db.php";
require_once "apns_lib.php";

ShuttleSchedule::init();
NextBusReader::init();

$all_routes = ShuttleSchedule::get_route_list();

while ($daemon->sleep(10)) {
  db::ping(); // keep the database connection running

  $time = time();
  $too_old = $time - 7200; // arbitrary 2 hour timeout threshold for now

  // only keep track of shuttles that are running
  $routes = array_filter($all_routes, array('ShuttleSchedule', 'is_running'));

  if (count($routes)) {
    foreach ($routes as $route) {
  public static function init() {
    self::reset_query();
    if (!self::$routeCache) {
      ShuttleSchedule::init();
      // this is part of a hack to correct 
      // saferide route names that nextbus gets wrong
      $combined_routes = Array(
        'saferidebostone' => 'saferidebostonall',
	'saferidebostonw' => 'saferidebostonall',
	'saferidecambeast' => 'saferidecamball',
	'saferidecambwest' => 'saferidecamball',
	);

      // query nextbus to see what routes are available
      self::set_command('routeList');
      $xml = self::query();
      if ($xml) {
	self::$unmodifiedRouteList = Array();

	foreach ($xml->getElementsByTagName('route') as $route) {
	  $routeName = $route->getAttribute('tag');
	  self::$unmodifiedRouteList[] = $routeName;

	  // if nextbus' route list is not consistent with the published schedule
	  // use the published route list

	  if (!ShuttleSchedule::is_running_today($routeName)
	      && array_key_exists($routeName, $combined_routes)
              && ShuttleSchedule::is_running_today($combined_routes[$routeName])) {
	    $routeName = $combined_routes[$routeName];
	  }

	  if (!self::$routeCache[$routeName] = self::read_route_cache($routeName)) {
	    self::$routeCache[$routeName] = Array();
	  }
	}

      } else {
	// query failed; get {routeName}s from cached filenames
	foreach (scandir(CACHE_DIR) as $filename) {
	  if (strpos($filename, self::$routeCachePrefix) == 0) {
	    $routeName = substr($filename, 0, strlen(self::$routeCachePrefix));
	    self::$routeCache[$routeName] = self::read_route_cache($routeName);
	  }
	}
      }

    }
  }