#!/usr/bin/perl

require("translations");

sub parseLine {
    my($line) = @_;

    $line =~ /"(.*)"/;
    $vendor = $1;
    $device = $2;
    $class =~ s/  *$//;
    $device =~ s/  *$//;
    $id = $vendor . "|" . $device;
    return $id;
}

open(F, "pcitable");
while (<F>) {
    chop;
    if (/^# List of known device classes/) { break; }
    if (/^#/) { next; }
    s/  *$//;
    if (!length($_)) { next };
    if ( /([^\t]*)\t([^\t]*)\t([^\t]*)\t"([^"]*)"/) {
      $vendor = $1;
      $device = $2;
      $driver = $3;
      $class =~ s/  *$//;
      $device =~ s/  *$//;
      $id = $vendor . "|" . $device;

      $drivers{$id} = $driver;
    }
}
close(F);

rename("pcitable","pcitable.old");
open(F, "<pci.ids");

open(DRIVERS, ">pcitable");

print DRIVERS "# This file is automatically generated from isys/pci. Edit\n";
print DRIVERS "# it by hand to change a driver mapping. Other changes will\n";
print DRIVERS "# be lost at the next merge - you have been warned.";
print DRIVERS "\n";
print DRIVERS "# The format is (\"%d\\t%d\\t%s\\t\"%s\"\\n\", classid, devid, moduleName, cardDescription)";
print DRIVERS "\n\n";

$class = "";


while (<F>) {
    chop;
    s/   */ /g;
    s/^  *//g;
    s/  *$//g;
    if (/^# List of known device classes/) { last; }
    if (/^#.*/) { next };
    if (!length($_)) { next };

    if (/^\t/ && ! /^\t\t/) {
	if ($class eq "") {
	    die "unexpected device\n";
	}
	s/\t([0-9A-Fa-f]+) +//;
	$devid = $1;

	$name = $class . "|" . $_;
	$device = "0x" . $classid . "|0x" . $devid;
	if ($drivers{$device}) {
	    printf(DRIVERS "0x%s\t0x%s\t%s\t\"%s\"\n", $classid, $devid, 
		   $drivers{$device},$name);
	} else {
	    printf(DRIVERS "0x%s\t0x%s\t%s\t\"%s\"\n", $classid, $devid,
	    	  "\"unknown\"",$name);
	}
    } elsif ( ! /^\t\t/)  {
	s/([0-9A-Fa-f]+) +//;
	$classid = $1;
	if ($classtr{$_}) {
	   $class = $classtr{$_};
	} else {
	   $class = $_;
	}
    }
}

close(F);

