;# this is a part of TVP (KIRIKIRI) software source.
;# see other sources for license.
;# (C)2001-2009 W.Dee <dee@kikyou.info> and contributors


;# make tvpgl_ia32.c and tvpgl_ia32.h from *.nah, *.nas.

open FH, ">tvpgl_ia32_intf_.h";
open FC, ">tvpgl_ia32_intf_.c";

$c_emits = '';

sub process
{
	local($fn) = @_;

	open FA , $fn;
	@all = <FA>;
	$all = join('', @all);

	while($all =~ /;;\[emit_c_h\](.*?)\n/sg)
	{
		print FH "$1\n";
	}

	;# equ conversion
	print FH "/*[*/\n";
	while($all =~ /;;\[emit_c_h_equ_begin\](.*?);;\[emit_c_h_equ_end\]/sg)
	{
		@each = split(/\n/, $1);
		foreach $each(@each)
		{
			if($each =~ /(.*?)\s+equ\s+(.*)/)
			{
				$name = $1;
				$value = $2;
				if($value =~ /(\d.*?)[hH]/)
				{
					$value = "0x$1";
				}
				print FH "#define $name $value\n";
			}
		}
	}
	print FH "/*]*/\n";


	while($all =~ /;;\[function\](.*?)\n/sg)
	{
		print FH "TVP_GL_IA32_FUNC_EXTERN_DECL($1);\n";
	}

	while($all =~ /;;\[function_replace_by\s+(.*?)\](.*?)\n;;(.*?),(.*?),(.*?)\n/sg)
	{
		print FH "TVP_GL_IA32_FUNC_EXTERN_DECL($3, $4, $5);\n";

		print FC "if($1)\n\t" if($1 ne '1');
		print FC "$2 = $4;\n";
	}

	while($all =~ /;;\[emit_c_c\](.*?)\n/sg)
	{
		$c_emits .= $1."\n";
	}

}



print FH <<EOF;
/*
	this is a part of TVP (KIRIKIRI) software source.
	see other sources for license.
	(C)2001-2009 W.Dee <dee\@kikyou.info> and contributors
*/

/* C-language interface to tvpgl_ia32.lib */
/* this file is always generated by summary.pl */

#ifndef __TVPGL_IA32_H__
#define __TVPGL_IA32_H__


#include "tjsTypes.h"
#include "tvpgl.h"

#ifdef _WIN32
#define TVP_GL_IA32_FUNC_DECL(rettype, funcname, arg)  rettype __cdecl funcname arg
#define TVP_GL_IA32_FUNC_EXTERN_DECL(rettype, funcname, arg)  extern rettype __cdecl funcname arg
#define TVP_GL_IA32_FUNC_PTR_DECL(rettype, funcname, arg) rettype __cdecl (*funcname) arg
#define TVP_GL_IA32_FUNC_PTR_EXTERN_DECL(rettype, funcname, arg) extern rettype __cdecl (*funcname) arg
#endif

#ifdef __cplusplus
 extern "C" {
#endif

extern void TVPGL_IA32_Init();

EOF

print FC <<EOF;
/*
	this is a part of TVP (KIRIKIRI) software source.
	see other sources for license.
	(C)2001-2009 W.Dee <dee\@kikyou.info> and contributors
*/

/* C-language interface to tvpgl_ia32.lib */
/* this file is always generated by summary.pl */

#include "tjsTypes.h"

#include "tvpgl.h"
#include "tvpgl_ia32_intf.h"

extern tjs_uint32 TVPCPUType;

void TVPGL_IA32_Init()
{


EOF


@files = <*.nah>;
foreach $file(@files)
{
	process($file);
}


@files = <*.nas>;
foreach $file(@files)
{
	process($file);
}



print FH <<EOF;

#ifdef __cplusplus
 }
#endif


#endif
EOF

print FC <<EOF

}


$c_emits

EOF


