#!/usr/bin/php
<?php
if (count($argv) != 3) {
    echo "usage: $argv[0] pagename revision\n";
    exit;
}
if (!is_numeric($argv[2])) {
    echo "usage: revision must be an integer\n";
    exit;
}

$wsdl = getenv('PHPWIKI_WSDL_URL');
if ($wsdl === false) {
    $wsdl = "http://phpwiki.fr/PhpWiki.wsdl";
}

try {
    $client = new SoapClient($wsdl);
} catch (SoapFault $fault) {
    die($fault->faultstring);
}

$phpwiki = getenv("HOME")."/.phpwiki";
if (!file_exists($phpwiki)) {
    $login = readline("Login: ");
    $password = readline("Password: ");
    $credentials = base64_encode($login.':'.$password);
    if ($fp = fopen($phpwiki, 'w')) {
        fprintf($fp, "%s:%s", $login, $password);
        fclose($fp);
        chmod($phpwiki, 0600);
    }
} else {
    $credentials = base64_encode(file_get_contents($phpwiki));
}

try {
    echo $client->getPageRevision($argv[1], $argv[2], $credentials);
    echo "\n";
} catch (SoapFault $e) {
    echo 'Error: ' .  $e->getMessage() . "\n";
}
