#!/usr/bin/perl
# program: parseconfig
# language: perl
# license: GPL (GNU Public License)
# Original Author: John 'Warthog9' Hawley
# Description:
#	This program really is quite simple, it's a perl script that will happily parse
#	a configuration file for me to deal with SVN configuration options
#
#                  \|/  ( Nothing else to see here, move along!)
#                 (o o) /
# -----------o00o--(_)--o00o-----------
#

`echo "--------------\ntrying this" >> /tmp/svnserve.pl.log`;

$debug = 0; # debug = 0 is off

$config{'daemon'} = ''; # -d
$config{'listen-port'} = '';
$config{'listen-host'} = '';
$config{'foreground'} = '';
$config{'help'} = ''; # -h
$config{'version'} = '';
$config{'inetd'} = ''; # -i
$config{'root'} = ''; # -r
$config{'read-only'} = ''; # -R
$config{'tunnel'} = ''; # -t
$config{'tunnel-user'} = '';
$config{'threads'} = ''; # -T
$config{'listen-once'} = ''; # -X

$shortcuts{'d'} = 'daemon';
$shortcuts{'h'} = 'help';
$shortcuts{'i'} = 'inetd';
$shortcuts{'r'} = 'root';
$shortcuts{'R'} = 'read-only';
$shortcuts{'t'} = 'tunnel';
$shortcuts{'T'} = 'threads';
$shortcuts{'X'} = 'listen-once';

`echo "Setup the prevariables" >> /tmp/svnserve.pl.log`;

$PATH = `echo \$PATH`;
$PATH =~ s/\s//gi;
$PWD = `pwd`;
$PWD =~ s/\s//gi;
@paths = split(/:/, $PATH);
$break = "";
$found = 0;
while( $temp = shift(@paths) ){
	if( -e ($temp . "/svnserve") ){
		if( $found > 1 ){
			$whereSVN = $temp;
			print "FOUND AT $temp\n" if $debug > 0;
			last;
		}
		$found++;
	}
	#if(
	#	$temp ne $PWD
	#	&&
	#	-e ($temp . "/svnserve")
	#){
	#	$whereSVN = $temp;
	#	print "FOUND AT $temp\n" if $debug > 0;
	#	last;
	#}
}
print "$PATH\n$PWD\n" if $debug > 0;
print `which svnserve` if $debug > 0;

`echo "Aquired the svnserve $PATH" >> /tmp/svnserve.pl.log`;

#$whereSVN = `which svnserve`;	# this breaks badly
$whereSVN =~ s/\s//gi;

print "Found svnserve at $whereSVN\n" if $debug > 0;

`echo "Found svnserve at $whereSVN" >> /tmp/svnserve.pl.log`;

@configFileUser;
@cliArgs;
while( $temp = shift(@ARGV) ){
	if(
		( $temp eq '-c' )
		||
		( $temp eq '-config' )
		||
		( $temp eq '--config' )
	){
		$temp = shift(@ARGV);
		push(@configFileUser, $temp);
	}elsif(
		( $temp eq '--help' )
		||
		( $temp eq '-h' )
		||
		( $temp eq '-help' )
	){
		help();
	}else{
		push(@cliArgs, $temp);
	}
}

@configFile = (
		'/etc/svnserve.conf',
		'/usr/etc/svnserve.conf',
		'/usr/local/etc/svnserve.conf',
		);

push(@configFileUser, @configFile);
@configFile = @configFileUser;

while(
	( $temp = shift(@configFile) )
	&&
	!defined($found)
){
	print $temp if $debug > 0;
	if( -e $temp){
		$found = $temp;
		print " - FOUND" if $debug > 0;
	}
	print "\n" if $debug > 0;
}

if( !defined($found) ){
	print "*** Warning *** Unable to locate the config file, please try again\n";
	exit();
}

print "File temp - $temp / $found\n" if $debug > 0;

open(FH,$temp) || die "*** Warning *** Couldn't open $temp - ERROR: $!\n";

`echo "Obviously opened the file" >> /tmp/svnserve.pl.log`;

#going to read out the config file at this point and get those values put into the config hash
while(<FH>){
	print if $debug > 0;
	$temp = $_;
	print "cycle temp = $temp" if $debug > 0;
	$temp =~ /^(\w*)\s*\=\s*(\S*)/;
	#$temp =~ /^(\w*)\s*=\s*(*)\;/;
	print "$1, $2\n" if $debug > 0;
	$key = $1;
	$value = $2;
	if( exists $shortcuts{ $key } ){
		$key = $shortcuts{ $key };
	}
	print "key - $key\n" if $debug > 0;
	$config{$key} = $value;
}

print "parsing CLI\n" if $debug > 0;

`echo "parsing CLI" >> /tmp/svnserve.pl.log`;

while( $temp = shift(@cliArgs) ){
	print "$temp\n" if $debug > 0;
	$temp =~ s/-//g;
        $temp =~ /^(\w*)\s*\=\s*(\S*)/;
	if( $1 ne '' ){
	        $key = $1;
        	$value = $2;
	}else{
		$key = $temp;
		$value = 'true';
	}
        if( exists $shortcuts{ $key } ){
                $key = $shortcuts{ $key };
        }
        $config{$key} = $value;
}
print "********\n" if $debug > 0;

`echo "**********" >> /tmp/svnserve.pl.log`;

#
# Ok time to overlay the CLI arguments over the top of this
#

# check for mutually exclusive options, error if so
#	daemon / -d
#	listen-port
#	listen-host
#	foreground
#	help /  -h
#	version
#	inetd / -i
#	root / -r
#	read-only / -R
#	tunnel / -t
#	tunnel-user
#	threads / -T
#	listen-once / -X

# ok so I decided not to mess with this, pass the options down to the client and be done with it

$cliargs = '';
foreach $k (keys (%config)){
	print "$k => ". $config{$k} ."\n" if $debug > 0;
	if( $config{$k} ne ''){
		if($config{$k} =~ /true/i){
			$cliargs .= " --$k";
		}else{
			$cliargs .= " --$k=". $config{$k};
		}
	}
}

print "CLI Arguments: |$cliargs|\n" if $debug > 0;

`echo "CLI Arguments: |$cliargs|" >> /tmp/svnserve.pl.log`;
`echo "executing:\n$whereSVN/svnserve $cliargs" >> /tmp/svnserve.pl.log`;
$whoami = `whoami`;
`echo "whoami $whoami" >> /tmp/svnserve.pl.log`;

#print `$whereSVN/svnserve $cliargs`;

print "$cliargs";

sub help{
	print "Help testin: |$whereSVN|\n";
	print "Usage:\n";
	print "--- svnserve (actual program) help ---\n";
	print `$whereSVN/svnserve --help`;
	exit();
}
