#!/usr/bin/env php
<?php

if (is_dir($vendor = __DIR__ . '/../vendor')) {
  require($vendor . '/autoload.php');
} elseif (is_dir($vendor = __DIR__ . '/../../../../vendor')) {
  require($vendor . '/autoload.php');
} else {
  die(
      'You must set up the project dependencies, run the following commands:' . PHP_EOL .
      'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
      'php composer.phar install' . PHP_EOL
  );
}

$domain = 'http://user:pass@www.pref.okinawa.jp:8080/path/to/page.html?query=string#fragment';

if ($argc == 2) {
  $domain = $argv[1];
}

try {
  $manager = new \Pdp\PublicSuffixListManager();
  $parser = new Pdp\Parser($manager->getList());
  $url = $parser->parseUrl($domain);
  $suffixValid = ($parser->isSuffixValid((string)$url->getHost())) ? 'IS' : 'IS NOT';

  print_r($url->toArray());
  echo sprintf('Host: %s', $url) . PHP_EOL;
  echo sprintf(
           "'%s' %s a valid public suffix.",
           $url->getHost()->getPublicSuffix(),
           $suffixValid
       ) . PHP_EOL;
} catch (\Exception $e) {
  die($e->getMessage() . PHP_EOL);
}
