#!/usr/bin/perl

$openscconf = "/etc/opensc/opensc.conf";
$openscconfdnie = $openscconf.".dnie";
$previousopenscconf = $openscconf.".dniebak";

system("cp", $openscconf, $previousopenscconf);
system("cp", $openscconf, $openscconfdnie);

open(OPENSC,$openscconf);
open(BUFFER,">".$openscconfdnie);

@filecontent=<OPENSC>;
$i=0;

while($i <= $#filecontent){

	$content=$filecontent[$i];

	if ($content=~/card_drivers\ \=\ dnie\;/){
		$i++;
		$content=$filecontent[$i];
        }

       	if ($content=~/card_driver\ dnie\ \{/){
		while($filecontent[$i] !~ '\}')
		{
			$i++;
		}
		$i++;        
		$content=$filecontent[$i];
		
	}
 

	print BUFFER $content;
	$i++;
}

close(OPENSC);
close(BUFFER);

system("mv", $openscconfdnie, $openscconf);
system("rm", $previousopenscconf);
print "File opensc.conf updated succesfully!\n";

