#!/usr/bin/perl

require("translations");

print "Make ABSOLUTELY SURE no entries are lost. Look at pcitable.old, and merge the differences.";


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([^\t]*)\t([^\t]*)\t"([^"]*)"/) {
      $vendor = $1;
      $device = $2;
      $subvendor = $3;
      $subdevice = $4;
      $driver = $5;
      $class =~ s/  *$//;
      $device =~ s/  *$//;
      $id = $vendor . "|" . $device . "|" . $subvendor . "|" . $subdevice;
      $drivers{$id} = $driver;
    } elsif ( /([^\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 "# Edit by hand to change a driver mapping. Changes to descriptions\n";
print DRIVERS "# will be lost at the next merge - you have been warned.\n";
print DRIVERS "# If you run makeids, please make sure no entries are lost.";
print DRIVERS "\n";
print DRIVERS "# The format is (\"%d\\t%d\\t%s\\t\"%s\"\\n\", vendid, devid, moduleName, cardDescription)\n";
print DRIVERS "# or (\"%d\\t%d\\t%d\\t%d\\t%s\\t\"%s\"\\n\", vendid, devid, subvendid, subdevid, 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/ ) {
        if ($class eq "") {
	    die "unexpected subdevice\n";
	}
	if ($devid eq "") {
	    die "unexpected subdevice\n";
	}
	s/\t\t([0-9A-Fa-f]+) ([0-9A-Fa-f]+) +//;
	$subvend = $1;
	$subdev = $2;
	
	$name = $class . "|" . $_;
	$device = "0x" . $classid . "|0x" . $devid . "|0x" . $subvend . "|0x" . $subdev;
	if ($drivers{$device}) {
	    printf(DRIVERS "0x%s\t0x%s\t0x%s\t0x%s\t%s\t\"%s\"\n", 
	    	   $classid, $devid, $subvend, $subdev,
		   $drivers{$device},$name);
	}
    } else  {
	s/([0-9A-Fa-f]+) +//;
	$classid = $1;
	if ($classtr{$_}) {
	   $class = $classtr{$_};
	} else {
	   $class = $_;
	}
    }
}

close(F);

