#!/usr/bin/perl

$if=$ARGV[0];

open(IF,"<$if");
$mode=0;
$which=0;
while (<IF>) 
	{
	if (m/^!/) 
		{
		$action = substr($_,1);
		if ($action eq "END\n") 
			{
			$mode=0;
			$which++;
			}
		elsif (index($action,"UNSERIALIZE") == 0)
			{
			($hmm2,$hmm) = split(/UNSERIALIZE /,$action);
			for($i=0;$i<$#group+1;$i++)
				{
				if ($hmm eq "@group[$i]")
					{
					$useme = $i;
					}
				}

			$_=@test[$useme];
			@stuff = split();

			for ($i=0;$i<$#stuff;$i+=2)
				{
				($name) = split(/;/,@stuff[$i+1]);
				if (@stuff[$i] eq "S8")
					{
					$thingy = "(S8)getc";
					}
				elsif (@stuff[$i] eq "U8")
					{
					$thingy = "getc";
					}
				elsif (@stuff[$i] eq "S16")
					{
					$thingy = "(S16)read_16ubit";
					}
				elsif (@stuff[$i] eq "U16")
					{
					$thingy = "read_16ubit";
					}
				elsif (@stuff[$i] eq "S32")
					{
					$thingy = "(S32)read_32ubit";
					}
				elsif (@stuff[$i] eq "U32")
					{
					$thingy = "read_32ubit";
					}
				print("	item->$name = $thingy(fd);\n");
				}
			}
		else 
			{
			$mode=1;
			@group[$which] = $action;
			}
		}
	else
		{
		if ($mode == 1)
			{
			@test[$which] = @test[$which].$_;
			}
		print($_);
		}
	}
close(IF);
