#! /usr/bin/perl
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# Copyright (C) 2010  Raphael Bossek <bossekr@debian.org>
#

use strict;
use lib "/usr/share/bugzilla3";

# Work-arround to be able to use Bugzilla functionality where
# shutdownhtml parameter is set.
$0="checksetup_nondebian.pl";
require Bugzilla;
require Bugzilla::Config;
require Getopt::Long;
require Bugzilla::Install::Localconfig;
use Data::Dumper;

my $opt_stdin = 0;
my $opt_infile = 0;
my $opt_localconfig = 0;
my $opt_dryrun = 0;
my $opt_value = 0;
Getopt::Long::GetOptions('stdin' => \$opt_stdin, 'infile' => \$opt_infile, 'localconfig' => \$opt_localconfig, 'dry-run' => \$opt_dryrun, 'value' => \$opt_value);

my $p;
if ($opt_localconfig) {
	$p = Bugzilla->localconfig;
}
else {
	$p = Bugzilla::Config::read_param_file();
}
if (exists $p->{$ARGV[0]}) {
	if (not exists $ARGV[1]) {
		if ($opt_stdin) {
			chomp ($ARGV[1] = <STDIN>);
		} else {
			print $p->{$ARGV[0]} . "\n";
		}
	}
	if (exists $ARGV[1]) {
		if ($opt_infile and not $opt_stdin) {
			open (f, $ARGV[1]) or exit (2);
			chomp ($ARGV[1] = <f>);
			close (f);
		}
		if ($opt_value) {
			$p->{$ARGV[0]} = int ($ARGV[1]);
		}
		else {
			$p->{$ARGV[0]} = $ARGV[1];
		}
		if ($opt_dryrun) {
			print Dumper ($p) . "\n";
		}
		else {
			if ($opt_localconfig) {
				# To get this working Bugzilla/Install/Localconfig.pm is patched.
				Bugzilla::Install::Localconfig::update_localconfig ({output => 0, debian_localconfig => $p});
			}
			else {
				Bugzilla::Config::write_params ($p);
			}
		}
	}
} else {
	print Dumper ($p) . "\n";
	exit (1);
}

exit (0);
