#! /usr/bin/perl
# 
# $Id$
# Perl generator for the bundled files with UDPGate.
# Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
# 
# 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; exactly version 2 of June 1991 is required
# 
# 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


use vars qw($VERSION);
$VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
use strict;
use warnings;

use File::Basename;
no utf8;
use bytes;	# REQUIRED


print <<"EOH";
/* AUTOGENERATED FILE! DO NOT EDIT!
 * Bundled files with UDPGate.
 */


#include "config.h"

#include <glib/ghash.h>

#include "bundle.h"	/* self */


GHashTable *bundle_hash_new(void)
{
static GHashTable *hash;
/* Prefixed 32-bit network byte order file length. */
EOH
my @files;
for my $pathname (@ARGV) {
	local *F;
	open F,$pathname or die "Cannot open $pathname: $!";
	local $/=undef();
	my $F=<F>;
	close F or warn "Cannot close $pathname: $!";
	my $filename=basename $pathname;
	(my $filename_sym=$filename)=~tr/a-zA-Z/_/c;
	$filename_sym=~s/^_*//;	# Prevent conflict with system internal symbols.
	push @files,{
			"filename"    =>$filename,
			"filename_sym"=>$filename_sym,
			};
	# Do not: pack("N",length($F)).$F
	# as there would be utf8 encoding hassle.
	$F=pack("Na*",length($F),$F);	# Prefix 32-bit network byte order file length.
	my $Fout=unpack "H*",$F;
	$Fout=~s/../0x$&,/g;
	$Fout=~s/(?:.....){1,16}/\t\t$&\n/g;
	$Fout=~s/,\n$//;
	print <<"EOH";
static const guint8 ${filename_sym}_data[]={
$Fout
		};
EOH
	}
print <<"EOH";

	if (!hash) {
		hash=g_hash_table_new(g_str_hash,g_str_equal);
EOH
	for (@files) {
		print "\t\tg_hash_table_insert(hash,\"".$_->{"filename"}."\",(gpointer)".$_->{"filename_sym"}."_data);\n";
		}
print <<"EOH";
		}
	return hash;
}

/* EOF */
EOH
