#!/usr/bin/perl

# KMACPS Driver Installation Routine
# Version 1.3
# March 14th 2006
# Markus Timpner


use strict;

print "\nInstallation procedure for Konica Minolta drivers is starting.\n";


my $cupsconffile;
my $inifile="kmacpsdriverini.xml";
my $FilterOrg;
my $DataDir;
my $ServerBin;
my $PPDDir;
my $FilterDir;
my $DataDirSearch='DataDir\s+';
my $ServerBinSearch='ServerBin\s+';
if (@ARGV >=1){
	$cupsconffile=$ARGV[0];
	quotemeta($cupsconffile);
}
else{
	$cupsconffile="/etc/cups/cupsd.conf";
}

if (-e $cupsconffile && -r $cupsconffile){
	open (CUPSCONF, $cupsconffile) or die "Can't open file: $cupsconffile\n";

	while (<CUPSCONF>)	{
		if (m/$DataDirSearch/i)		{
			chomp $_;
			my($tmp1 , $tmp2) = split /\s+/,$_,2;
			$DataDir = $tmp2;
			$PPDDir="$DataDir\/model";
			
		}
		if(m/$ServerBinSearch/i){
			chomp $_;
			my($tmp1 , $tmp2) = split /\s+/,$_,2;
			$ServerBin = $tmp2;
			$FilterDir="$ServerBin\/filter";
			
		}
	}

	close(CUPSCONF);
}
else{	
	if(-e $cupsconffile){
		file_noread($cupsconffile);
	}
	else{
		file_noexist($cupsconffile);
	}
}
if (!(-e $PPDDir)){
	file_noexist($PPDDir);
}
elsif( !(-w $PPDDir)){
	file_nowrite($PPDDir);
}
elsif(!(-e $FilterDir)){
	file_noexist($FilterDir);
}
elsif(!(-w $FilterDir)){
	file_nowrite($FilterDir);
}
else{
	print "Installation starting\n";
	installPPD($PPDDir,getPPDFiles($inifile));
	installFilter($FilterDir,getFilterFiles($inifile));
	print "\nInstallation procedure finished!\n"; 
	
	print "\nTo install printers you can use the CUPS webinterface for example.\n";
	print "Please restart the CUPS server now\n\n";
	exit;
}

sub file_noread{
	my @filename = @_;
	print "You do not have sufficient priviledges to read $filename[0]\n";
	print "Maybe you are not root!\n";
	print "Exiting\n";
	exit;
}
sub file_nowrite{
	my @filename = @_;
	print "You do not have sufficient priviledges to write $filename[0]\n";
	print "Maybe you are not root!\n";
	print "Exiting\n";
	exit;
}
sub file_noexist{
	my @filename = @_;
	print "No such file: $filename[0]\n";
	print "usage:\t install [CUPS configuration file]\n";
	print "Exiting\n";
	exit;	
}
sub getPPDFiles{
	my $inifile=@_[0];
	my @ppdfiles=();
	open(INIFILE,$inifile);
	while(<INIFILE>){
		if(m/<ppdfile>/){
			s/<ppdfile>//;
			s/<\/ppdfile>//;
			s/\s//;
			s/\n//;
			push (@ppdfiles,$_);
		}
	}
	return @ppdfiles;		
}

sub getFilterFiles{
	my $inifile=@_[0];
	my @filterfiles=();
	open(INIFILE,$inifile);
	while(<INIFILE>){
		if(m/<filterfile>/){
			s/<filterfile>//;
			s/<\/filterfile>//;
			s/\s//;
			s/\n//;
			push (@filterfiles,$_);
		}
	}
	return @filterfiles;		
}

sub installPPD{
		my $PPDDir=shift @_;
		my @ppds=@_;
		my $ppd="";
		print "Installing PPD files to $PPDDir\n";
		foreach $ppd (@ppds){
			print "\n\tInstalling $ppd! "; 	
			if(-e "$PPDDir/$ppd"){
				print "Newer PPD version already installed!";			
			}
			else{
			`cp $ppd $PPDDir`;
			}
		}
}
sub installFilter{
		my $FilterDir=shift @_;
		my @filters=@_;
		my $filter="";
		my $VersionCheck=0;
		my $newversion=0;
		my $oldversion=0; 
		print "\n\nInstalling Filter files to $FilterDir\n";
		foreach $filter (@filters){
			if(-e "$FilterDir/$filter"){
						
						$newversion=getFilterVersion("$filter");
						$oldversion=getFilterVersion("$FilterDir/$filter");
						if($newversion==0 || $oldversion==0){
									print "ERROR:Filter cannot be installed\n";
									print "File version cannot be obtained!\n";
						}
						if($newversion lt $oldversion){
							print "There is already an installed filter(Version $oldversion) which is newer than the filter selected for installation (Version $newversion)\n";
							$VersionCheck=1;				
						}
						if($newversion eq $oldversion){
							print "There is already an installed filter(Version $oldversion) which is the same version than the filter selected for installation (Version $newversion)\n";	
							$VersionCheck=1;
						}
						
			}
			if($VersionCheck==0){
				copyFilter($filter,$FilterDir);
				#print "Installing $filter \n"; 	
				#`cp $filter $FilterDir`;	
			}
			else{
				
				print "Do you like me to overwrite the old filter version?\n";
				my $overwrite_decision="n";
				read (STDIN,$overwrite_decision,1);
				if($overwrite_decision eq ('y' or 'Y')){
					print "Overwriting old filter version!\n";
					copyFilter($filter,$FilterDir);
				}
				else{
					print "I will NOT overwrite the old filter version and therefore NOT install the new version.\n";
					print "Please be aware that the driver might not work correctly!\n";
				}
				#print "If you are sure to overwrite the installed filter, please remove it manually and start again!\n";
			}
			
		}
}

sub getFilterVersion{ 
	my $filter=@_[0];
	my $version="";
	open(FILTER,$filter) or die "Cannot open $filter: $!";
	while(<FILTER>){
		if(m/Version/){
			s/#//;
			s/\t//;
			s/\n//;
			my($versionmarker,$versionnumber)=split/:/,$_,2;
			$version=$versionnumber;
			last;
		}
					
	}
	close(FILTER);
	return $version;
				
}
sub copyFilter{
	my $filter=@_[0];
	my $FilterDir=@_[1];
	print "Installing $filter \n"; 	
	`cp $filter $FilterDir`;	
}

sub getPPDVersion{ 
	my $ppd=@_[0];
	my $version="";
	open(PPD,$ppd) or die "Cannot open $ppd: $!";
	while(<PPD>){
		if(m/Version/){
			s/\*//;
			s/"//;
			s/\w//;
			s/\n//;
			my($versionmarker,$versionnumber)=split/:/,$_,2;
			$version=$versionnumber;
			last;
		}
					
	}
	close(PPD);
	return $version;
				
}


