#!/usr/bin/php
<?php

$LIC = '';
if (isset($argv[1]))
{
	# treat 1st arg as a file name
	if (($hnd = @fopen($argv[1],'r')) !== false)
	{
		fprintf (STDERR, "Reading license from file '%s'...\n",$argv[1]);
		$LIC = trim(fgets($hnd));
	}
	# if not: treat as verbatim string
	else
	{
		$LIC = $argv[1];
		fprintf (STDERR, "Inserting '%s' as license string...\n",$LIC);
	}
}
# no arg: read one line from stdin
else
{
	fprintf(STDERR, "Reading one line from STDIN...\n");
	$LIC = trim(fgets(STDIN));
}
if (! $LIC)
{
	fprintf(STDERR, "No license string could be determined.\n");
	exit(1);
}

$CONFFILE = '/var/www/owncloud/config/config.php';
include_once($CONFFILE);
$CONFIG['license-key'] = $LIC;
# copy $CONFFILE to ${CONFFILE}.bak ?
if (($hnd = fopen($CONFFILE,'w')) !== false)
{
	fprintf($hnd,"<?php\n");
	fprintf($hnd,"\$CONFIG = ");
	fprintf($hnd,var_export($CONFIG,true));
	fprintf($hnd,";\n");
	fclose($hnd);
}

