#!/usr/bin/perl

#
# Chris Vanhoof <vanhoof@redhat.com>
# automagically load vnc module, and config options to x config (el3/el4)
#

use strict;

my $config;
my @xconfig_file_array = (
	'/etc/X11/xorg.conf',
	'/etc/X11/XF86Config'
);

foreach my $file (@xconfig_file_array){
	if (-e $file){
		$config = $file;
		last;
	}
}

open(FILE, $config);
my @lines = <FILE>;
close(FILE);


my $vnc_required = 1;
my $password_file_required = 1;

foreach my $line (@lines){
	$vnc_required = 0 if $line =~ m/Load\s+"vnc"/;
	$password_file_required = 0 if $line =~ m!Option\s+"passwordFile"\s+"/root/\.vnc/passwd"!;
}

if (($vnc_required == 0) && ($password_file_required == 0)){
	print "Nothing to do"."\n";
	exit;
}

`/bin/cp $config $config.bak`;

open(FILE, ">$config");
select(FILE);
foreach my $line (@lines){
	print $line;
        if (($vnc_required) && ($line =~ m/Section \"Module\"/)){
        	print "\t".'Load  "vnc"'."\n";
        }
        elsif (($password_file_required) && ($line =~ m/DefaultDepth\s+\d+/)){
		print "\t".'Option  "passwordFile"  "/root/.vnc/passwd"'."\n";
        }
}
close(FILE);
