#!/usr/bin/perl 


my $VERSION;
$VERSION="2.1";



use strict;
use warnings;
use Getopt::Long;
use fontlinge::Filebasics;

use bytes;

my $help;
my @parameter;
my $username="";
my $webgui;
my $bash;
my $format;
my $status;

$|=1;

GetOptions (	'help'		=>	\$help,
		'webgui:s'	=>	\$webgui,
		'bash'		=>	\$bash,
		'user=s'	=>	\$username,
		'status'	=>	\$status
) || exit -1 ;


if ( $help  ) {
	&usage;
	exit;
}

my $cmd_count = 0;
if (defined $webgui) { $cmd_count +=1; }
if (defined $bash  ) { $cmd_count +=1; }
if (defined $status) { $cmd_count +=1; }

if ($cmd_count > 1) {
	print STDERR "ERROR: You cannot use --webgui, --bash and --status together\n";
	exit 1;
}

my %config;

%config=&fontlinge_Get_Config($username, $status);

if ( (! defined @ARGV) || (scalar(@ARGV) == 0) ) { @parameter=keys %config; } 
else { @parameter=@ARGV; }

if ($status) {
	foreach my $i (@parameter) {
		if(! defined $config{$i}) {
			exit 2;
		}
	}
	exit 0;
}


my $output="-";
if ( defined $webgui ) {
	$format="\$parameter['\000NAME\000']=stripslashes\('\000VALUE\000'\);\n";	# $parameter['var']='value';<CR>
	if ( $webgui ne "" ) {
		$output=$webgui;
		if ($output !~ /^-$/) {
			$output=fontlinge_Expand_Filename($output);
			$output=~s/\/$//;
		}
	} else {
		if( ! $config{'webgui_conf'} ) {
			print "Parameter 'webgui_conf' ist missing in ~/.fontlinge.\n";
			exit -1;
		}
		$output=$config{'webgui_conf'};
	}
} elsif ( defined $bash ) {
	$format="\000NAME\000=\000VALUE\000\n";		# parameter=value<CR>
	# (quoting is done by quotemeta, see below. This seems to be the best working solution.)
} else {						# not webgui or bash output, plain "normal"
	$format="\000NAME\000\t\000VALUE\000\n";	# var<TAB>value<CR>
}
if (! open ( OUTPUT, ">$output" ) ) {
	print "Cannot open '$output' for writing.\n";
	exit -1;
}
	
if ( defined $webgui ) {
	print OUTPUT "<?php\n# This file was autogenerated.\n";
	print OUTPUT "# Do not change except via fontlinge_getconfig --webgui\n";
}

foreach my $i (@parameter) {
	if(defined $config{$i}) {
		my $tmp = $format;
		my $tmp2 = $config{$i};
		if ( defined $webgui ) {
			$tmp2=~s/\\/\\\\/g;
			$tmp2= quotemeta $tmp2;
		} elsif ( defined $bash ) {
			$tmp2= quotemeta $tmp2;
		}
		$tmp=~s/\000NAME\000/$i/gs;
		$tmp=~s/\000VALUE\000/$tmp2/gs;
		print OUTPUT $tmp;
	}
}
if ( defined $webgui ) { print OUTPUT "?>\n"; }

close( OUTPUT );

sub usage {
print <<'END_OF_HELP';

fontlinge_getconfig
    [--user=username]
    [--webgui[=/path/to/webgui/config.inc.php] | --bash | --status ]
    [config1] [config2] ...

Reads the ~/.fontlinge config file and prints the value of [config].
If no config option is given, all defined values are printed.

Undefined values are not returned.
Empty values are returned as empty.

The return values have the format
name\tvalue\n

--user=username  Read other users' config

--webgui[=file]  Write the configfile for the webgui (PHP format).
                 If 'webgui_conf' is set in ~/.fontlinge, the path/file
                 may be ommited.
                 Usually the filename is 'config.inc.php'.
                 Use '-' as filename to check the output.

--bash           Print output in a bash compatible format.
                 Can be read into bash variables using the command
                 eval `fontlinge_getconfig --bash`

--status         Check if the given config option(s) are set in ~/.fontlinge.
                 Returns an exitcode of 0 if all of them are set, otherwise 2.
                 No output in this mode.

END_OF_HELP
}

# vim:ts=8
