#!/usr/bin/perl -w
# Copyright 2001 Adam Thornton
#
#  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.
#
#  See http://www.gnu.org/copyleft/gpl.html for the terms of the
#  GNU General Public License.  If you don't have internet access, write to
#  the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#  Boston, MA 02111, USA.
#
#  The author can be reached at adam@fsf.net

# Usage: makebli resource-file
#
# Resource file is as defined in L. Ross Raszewski's front.txt
# Resource records are
# CODE                 file (Zcode or Glulx)
# COPYRIGHT Text
# AUTHOR    Text
# NOTE      Text
# RELEASE   Number
# PICTURE   ResourceID file
# SOUND     ResourceID file
# PALETTE              file (blorb palette format)
# RESOLUTION           file (blorb resolution format)
# HIGHCOLOR                 (16-bit palette)
# TRUECOLOR                 (32-bit palette)
# ! Comment

# This program generates an Inform include file which assigns constants to 
#  Pict and Snd records defined in a resource file
# Therefore, only PICTURE and SOUND records actually do anything.


open FILE, "<$ARGV[0]" or die "Could not open $ARGV[0]: $!\n";

my $time = scalar localtime;
my $bli = "";
($bli) = split /\./,$ARGV[0];
$bli .= ".bli";
open BLI, ">$bli" or die "Could not open $bli: $!\n";
print BLI << "EOF";
! Blorb Resource Definitions file
! Generated automatically at $time by $0 @ARGV
! Edit at your own risk!
!
! Include in your Inform source with \'Include \"$bli\"\;\'
! 
EOF
    ;

print BLI "Message \"[Including Blorb Resource Declarations generated $time]\";\n";


my $number = 0;
my $short = "";
my $picnum = 0;
my $sndnum = 0;
my $typedesc = "";
my $resid = "";
my $f = "";

while (<FILE>) {
    chomp;
    next if /^$/;
    next if /^!/;
    if ((/^PICTURE/) || (/^SOUND/)) {
	($typedesc,$resid,$f) = split;
	if ($typedesc eq "PICTURE") {
	    $picnum++;
	    $number = $picnum;
	    $short = "Pict";
	} else {
	    $sndnum++;
	    $number = $sndnum;
	    $short = "Snd";
	}
	print BLI "Constant $resid \t $number;\t! Blorb $short:$f\n";
	next;
    }
}
