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

require_once __DIR__ . '/../autoload_register.php';

$help = <<<help
Sslurp Root CA Bundle Updater

Usage:
 {$argv[0]} [--stdout]
 {$argv[0]} -o[output_file]

Options
 -o      \tPath/filename to the file to (over)write the updated root CA bundle. Defaults to ca-bundle.pem
 --stdout\tDo not write file, send output to stdout instead.

Sslurp home page: https://github.com/EvanDotPro/Sslurp
help;

$opts = getopt('o::h', array('stdout'));

if (isset($opts['h'])) {
    echo $help . "\n\n";
    exit(0);
}
if (isset($opts['stdout']) || !isset($opts['o'])) {
    $opts['stdout'] = true;
} elseif (isset($opts['o']) && is_array($opts['o'])) {
    echo "WARNING: Multiple output paths given; ignoring all but the first value provided: `{$opts['o'][0]}`\n\n";
    $opts['o'] = $opts['o'][0];
} elseif (empty($opts['o'])) {
    $opts['o'] = 'ca-bundle.pem';
}

try {
    $caRootPemBundle = new Sslurp\CaRootPemBundle(isset($opts['stdout']) ? 'php://temp' : $opts['o']);
    $caBundle        = $caRootPemBundle->getUpdatedCaRootBundle();
} catch (Exception $e) {}

if (empty($caBundle)) {
    echo "Sorry, there was an error building the latest root CA bundle.\n";
    if (!empty($e)) {
        echo $e->getMessage() . "\n";
    }
    exit(1);
}

if (isset($opts['stdout'])) {
    echo $caBundle;
} else {
    echo "Updated root CA bundle written to {$opts['o']}\n";
}
