#!/usr/bin/perl
my $filename=$ARGV[0];
my $full_path=`pwd`;
chomp $full_path;
my $str;
my %KEY;
my $current_path;
my $out;
open (H, "< $filename");

if ($full_path =~ /\.wine\/drive_(.)\/(.+$)/)
{
       $full_path="$1:/$2";
}
else { $full_path = "z:$full_path"; }
$full_path=~s/\//\\\\/g;

print $full_path;


while ($str=<H>) {
	print $str;
	if ($str=~/set\s+(\S+?)\s*\=\s*(\S+)/i) { $KEY{$1}=$2; print "$1=$2\n"; }
	elsif ($str=~/reg\s+ADD\s+(\S+)\s+\/v\s(\S+)\s\/t\s(\S+)\s\/d\s(\S+)/)
	{
		my $path=$1;
		my $name=$2;
		my $type=$3;
		my $value=$4;
		$path=~s/\%(.+)\%/$KEY{$1}/;
		$path=~s/HKLM/HKEY_LOCAL_MACHINE/;
		if ($path ne $current_path)
		{
			$out.="[$path]\n";
		}
		if ($type eq "REG_BINARY")
		{
			$out.="\"$name\"=hex\:".substr($value,0,2)."\,".substr($value,2,2)."\n";
		}	
		elsif ($type eq "REG_SZ")
		{
			$value=~s/\%\~dp0\\?/$full_path\\\\/;
			$out.="\"$name\"=$value\n";
		}
		$current_path=$path;
	} 
}

print $out;
close (H);

open (W, ">installreg.reg");
print W $out;
close W;

$out=`wine regedit installreg.reg`;
print $out;
