/**
 * Freebox DNS Auto updater
 * @author <*****@*****.**>
 */
require_once __DIR__ . '/vendor/autoload.php';
$Args = new GetOpt();
$Args->addShortOpt('v', 'Enable verbose mode');
$Args->addLongOpt('verbose', 'Enable verbose mode');
$Args->addLongOpt('checkdns', 'Check current DNS status');
$Args->addLongOpt('udpatedns', 'Update DNS servers');
$Args->parse();
$isVerbose = $Args->hasOption('v') || $Args->hasOption('verbose');
/// Launch app
$App = new freebox\DNS_changer($isVerbose);
// Check status
if ($Args->hasOption('checkdns')) {
    if (!$App->checkDNS()) {
        if ($isVerbose) {
            IO::stdout('DNS seems to be corrupted. Forced update...', 0, true, IO::COLOR_MAGENTA);
        }
        $App->updateDNS();
    }
}
// Force update
if ($Args->hasOption('updatedns')) {
    $App->updateDNS();
}
// No args given
if (!$Args->hasOption('updatedns') && !$Args->hasOption('checkdns')) {
    IO::stdout('Please use the --checkdns or --updatedns flag');
}
 /**
  * Update DNS
  * Get new DNS servers from openDNS and Google
  */
 public function updateDNS()
 {
     if ($this->isVerbose) {
         IO::stdout('Updating DNS Servers...', 0, true, IO::COLOR_MAGENTA);
     }
     /// Find new DNS servers
     $opennic_DNS_servers = opennic\DNS_server::get_nearest_servers();
     $google_DNS_servers = google\DNS_server::get_nearest_servers();
     $new_DNS_servers = array_merge($opennic_DNS_servers, $google_DNS_servers);
     /// Update Configuration
     $Config = new FreeboxAPI\services\config\DHCP($this->app);
     $Config->set_attribute_configuration(['dns' => $new_DNS_servers]);
     $newConfig = $Config->get_current_configuration();
     if ($this->isVerbose) {
         $a = $newConfig->dns;
         IO::stdout('New DNS Servers :', 0, true, IO::COLOR_MAGENTA);
         foreach ($a as $ServerDNS) {
             IO::stdout("- " . ($ServerDNS ?: 'Undefined'), 1);
         }
     }
 }