function main()
{
    $opts = getopt("cath");
    if (isset($opts['h']) || !isset($opts['a']) && !isset($opts['t'])) {
        usage();
    }
    if (isset($opts['a']) && isset($opts['t'])) {
        fatal("can't use -a and -t together");
    }
    $mode = isset($opts['t']) ? 'tx' : 'address';
    if (isset($opts['c'])) {
        switch ($mode) {
            case "tx":
                echo <<<'EOD'

DROP TABLE t_shortlinks;
CREATE TABLE t_shortlinks (
    shortcut bytea NOT NULL PRIMARY KEY,
    hash bytea NOT NULL REFERENCES transactions
);

ALTER TABLE public.t_shortlinks OWNER TO blockupdate;
GRANT SELECT ON TABLE t_shortlinks TO "www-data";

EOD;
                break;
            case "address":
                echo <<<'EOD'

DROP TABLE a_shortlinks;
CREATE TABLE a_shortlinks (
    shortcut bytea NOT NULL PRIMARY KEY,
    hash160 bytea NOT NULL REFERENCES keys
);

ALTER TABLE public.a_shortlinks OWNER TO blockupdate;
GRANT SELECT ON TABLE a_shortlinks TO "www-data";

EOD;
                break;
        }
    }
    $fh = fopen("php://stdin", "r");
    while ($line = trim(fgets($fh))) {
        $arr = explode(" ", $line);
        if ($mode == "tx") {
            $shortcut_hex = decodeBase58($arr[0]);
            $tx_hex = $arr[1];
            echo "INSERT INTO t_shortlinks(shortcut, hash) VALUES (decode('{$shortcut_hex}', 'hex'), decode('{$tx_hex}', 'hex'));\n";
        } elseif ($mode == "address") {
            $shortcut_hex = decodeBase58($arr[0]);
            $hash160_hex = addressToHash160($arr[1]);
            echo "INSERT INTO a_shortlinks(shortcut, hash160) VALUES (decode('{$shortcut_hex}', 'hex'), decode('{$hash160_hex}', 'hex'));\n";
        }
    }
}
function checkAddress($addr, $addressversion = ADDRESSVERSION)
{
    $addr = decodeBase58($addr);
    if (strlen($addr) != 50) {
        return false;
    }
    $version = substr($addr, 0, 2);
    if (hexdec($version) != hexdec($addressversion)) {
        return false;
    }
    $check = substr($addr, 0, strlen($addr) - 8);
    $check = pack("H*", $check);
    $check = strtoupper(hash("sha256", hash("sha256", $check, true)));
    $check = substr($check, 0, 8);
    return $check == substr($addr, strlen($addr) - 8);
}
function WIFtoPriv($WIF)
{
    $uWif1 = decodeBase58($WIF);
    $uWif2 = substr($uWif1, 2, strlen($uWif1) - 12);
    return $uWif2;
}