BEGIN {

  $srcdir = shift;

};

if( /LIBSPECTRUM_AUTOGEN_WARNING/ ) {
  $_ = << "CODE";
/* NB: This file is autogenerated from libspectrum.h.in. Do not edit
   unless you know what you're doing */
CODE
}

if( /LIBSPECTRUM_SNAP_ACCESSORS/ ) {

  open( DATAFILE, '<' . "${srcdir}/snap_accessors.txt" ) or die "Couldn't open `snap_accessors.txt': $!";

  $_ = '';
  while( <DATAFILE> ) {

    next if /^\s*$/; next if /^\s*#/;

    my( $type, $name, $indexed ) = split;

    my $return_type;
    if( $type =~ /^(.*)\*/ ) {
	$return_type = "WIN32_DLL $1 *";
    } else {
	$return_type = "WIN32_DLL $type";
    }

    if( $indexed ) {

	print << "CODE";
$return_type libspectrum_snap_$name( libspectrum_snap *snap, int idx );
WIN32_DLL void libspectrum_snap_set_$name( libspectrum_snap *snap, int idx, $type $name );
CODE

    } else {

	print << "CODE";
$return_type libspectrum_snap_$name( libspectrum_snap *snap );
WIN32_DLL void libspectrum_snap_set_$name( libspectrum_snap *snap, $type $name );
CODE

    }
  }
}

if( /LIBSPECTRUM_TAPE_ACCESSORS/ ) {

    open( DATAFILE, '<' . "${srcdir}/tape_accessors.txt" )
	or die "Couldn't open `tape_accessors.txt': $!";

    $_ = '';
    while( <DATAFILE> ) {

	# Remove comments and blank lines
	s/#.*//;
	next if /^\s*$/;

	# Skip which block types each accessor applies to
	next if /^\s/;
	
	my( $type, $name, $indexed, undef ) = split;
	
	my $return_type;
	if( $type =~ /^(.*)\*/ ) {
	    $return_type = "WIN32_DLL $1 *";
	} else {
	    $return_type = "WIN32_DLL $type";
	}

	if( $indexed ) {

	  print << "CODE";
$return_type libspectrum_tape_block_$name( libspectrum_tape_block *block, size_t idx );
WIN32_DLL libspectrum_error libspectrum_tape_block_set_$name( libspectrum_tape_block *block, $type \*$name );
CODE

	} else {

	print << "CODE";
$return_type libspectrum_tape_block_$name( libspectrum_tape_block *block );
WIN32_DLL libspectrum_error libspectrum_tape_block_set_$name( libspectrum_tape_block *block, $type $name );
CODE
    
	}
    }

    close DATAFILE or die "Couldn't close `tape_accessors.txt': $!";
}
