#!/bin/awk -f
#
# convert_isapnp_ids
# Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
# 
# This scripts converts PnP ID tables from new form:
#
# static struct pnp_card_device_id variable[] __devinitdata = {
#	{ .id = "ALS0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" }, } },
#	{ .id = "", } /* end */
# };
#
# MODULE_DEVICE_TABLE(pnp_card, snd_als100_pnpids);
#
# to old form:
#
# static struct isapnp_card_id snd_opl3sa2_pnpids[] __devinitdata = {
#	{ ISAPNP_CARD_ID('A', 'L', 'S', 0x0001),
#	  .devs = {
#		ISAPNP_DEVICE_ID('@', '@', '@', 0x0001),
#		ISAPNP_DEVICE_ID('@', 'X', '@', 0x0001),
#		ISAPNP_DEVICE_ID('@', 'H', '@', 0x0001),
#	  }
#       },
#       { ISAPNP_CARD_END, }
# };
#
# ISAPNP_CARD_TABLE(variable);

function getstructname(line)
{
	split($line, a, " ")
	split(a[4], b, "[")
	return b[1]
}

function printdata(id, prefix,   h, l)
{
	h = "0x"
	l = substr(id, 4, 4)
	if (match(l, "^XXXX") != 0) {
		l = "ISAPNP_ANY_ID"
		h = ""
	}
	print prefix "('" substr(id, 1, 1) "', '" \
			  substr(id, 2, 1) "', '" \
			  substr(id, 3, 1) "', " \
			  h l "),"
}

function dataline(line,   a, b, idx, num)
{
	num = split($line, a, ",")
	split(a[1], b, "\"")
	printdata(b[2], "\t{ ISAPNP_CARD_ID")
	print "\t  .devs = {"
	for (idx = 2; idx < num; idx = idx + 1) {
		split(a[idx], b, "\"")
		if (b[2] != "") {
			printdata(b[2], "\t\tISAPNP_DEVICE_ID")
		}
	}
	print "\t  }"
	print "\t},"
}

/^static struct pnp_card_device_id/ {
	print "/*"
	print " * This file is autogenerated by the alsa-driver/utils/convert_isapnp_ids script..."
	print " * DO NOT EDIT!!!"
	print " * Source file: " ARGV[1]
	print " */"
	print ""
	print "#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)"
	print ""
	structname = getstructname($0)
	print "static struct isapnp_card_id " structname "_old[] __devinitdata = {"
	getline
	while (match($0, "^};") == 0) {
		if (match($0, ".id[ \t]*=[ \t]\"\"")) {
			print "\t{ ISAPNP_CARD_END, }"
		} else {
			if (match($0, "^# [0-9]+") == 0) {
				if (match($0, /\{.*\}/)) {
					dataline($0)
				} else {
					print $0
				}
			}
		}
		getline
	}
	print "};"
	print ""
	print "ISAPNP_CARD_TABLE(" structname "_old);"
	print ""
	print "#endif /* 2.5.0 */"
}
