#!/usr/bin/python
#
# trimpciids
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import sys
import os
import string

vendors = []
devices = []

f = open(sys.argv[1])
if f:
	pcitable = f.readlines()
	f.close()
	for line in pcitable:
	    if not line.startswith("alias pci:"):
		continue
	    vend = "0x%s" % (line[15:19],)
	    dev = "0x%s" % (line[24:28],)
	    vend = vend.upper()
	    dev = dev.upper()
	    if vend not in vendors:
		vendors.append(vend)
	    if (vend, dev) not in devices:
		devices.append( (vend, dev) )

for file in sys.argv[2:]:
   if not os.path.exists(file):
       sys.stderr.write("WARNING: non-existent file %s for trimpciids\n" %(file,))
       continue
   f = open(file)
   if f:
	pcitable = f.readlines()
	f.close()
	for line in pcitable:
	    if not line.startswith("alias pcivideo:"):
		continue
	    vend = "0x%s" % (line[20:24],)
	    dev = "0x%s" % (line[29:33],)
	    vend = vend.upper()
	    dev = dev.upper()
	    if vend not in vendors:
		vendors.append(vend)
	    if (vend, dev) not in devices:
		devices.append( (vend, dev) )

pciids = sys.stdin.readlines()
current_vend = 0
for line in pciids:
    if line.startswith("#") or line == "\n":
	continue
    if line.startswith("\t\t"):
	continue
    if not line.startswith("\t"):
	current_vend = "0x%s" % line.split()[0]
	current_vend = current_vend.upper()
	if current_vend in vendors:
	    print line,
	continue
    dev = "0x%s" % line.split()[0]
    dev = dev.upper()
    if (current_vend, dev) in devices:
	print line,
