#!/usr/bin/perl -w

$from_plugin = 0;

while (defined($option = shift)) {
	last unless $option =~ /^-/;

	if ($option =~ /^-p$/) {
		# Derives from plugin.  DEPRECATED.  FIXME: remove.
		$from_plugin = 1;
		next;
	}
}

undef $/;
$std_header = <DATA>;
$/ = "\n";

$new = $option;
$base = shift;

sub usage() {
	print STDERR <<"EOF";
usage: $0 [-p] GdamNew GdamBase

Defines a new class named `GdamNew' deriving from `GdamBase'
and writes the most tedious bits of Gtk macro.

Options:
	-p        the base class is a plugin
EOF
}


die "usage: $0 GdamNewClass GdamBaseClass" unless defined $base;

$new_und_lc = $new;
$new_und_lc =~ s/([A-Z])/"_" . lc($1)/eg; $new_und_lc =~ s/^_//;
$new_und_uc = uc($new_und_lc);

$base_und_lc = $base;
$base_und_lc =~ s/([A-Z])/"_" . lc($1)/eg; $base_und_lc =~ s/^_//;
$base_und_uc = uc($base_und_lc);

$fname = $new_und_lc;
$fname =~ s/_//g;

$suffix_uc = $new_und_uc; $suffix_uc =~ s/^.+?_//;
$prefix_uc = $new_und_uc; $prefix_uc =~ s/_.*//;
$suffix_lc = lc($suffix_uc);
$prefix_lc = lc($prefix_uc);
$typemacro = "${prefix_uc}_TYPE_${suffix_uc}";

$base_suffix_uc = $base_und_uc; $base_suffix_uc =~ s/^.+?_//;
$base_prefix_uc = $base_und_uc; $base_prefix_uc =~ s/_.*//;
$base_suffix_lc = lc($base_suffix_uc);
#$base_prefix_lc = lc($base_prefix_uc);
$basetypemacro = "${base_prefix_uc}_TYPE_${base_suffix_uc}";

#
# Script to output a template of a gdam class.

$filename = "$fname.h";
$filename .= ".generated" if -e $filename;

open HFILE, ">$filename";
print HFILE $std_header;
print HFILE <<"EOF";
#ifndef __${new_und_uc}_H_
#define __${new_und_uc}_H_


typedef struct _${new} ${new};
typedef struct _${new}Class ${new}Class;

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

GtkType ${prefix_lc}_${suffix_lc}_get_type();
#define $typemacro			(${new_und_lc}_get_type ())
#define ${new_und_uc}(obj)              (GTK_CHECK_CAST ((obj), $typemacro, $new))
#define ${new_und_uc}_CLASS(klass)      (GTK_CHECK_CLASS_CAST ((klass), $typemacro, ${new}Class))
#define ${prefix_uc}_IS_${suffix_uc}(obj)           (GTK_CHECK_TYPE ((obj), $typemacro))
#define ${prefix_uc}_IS_${suffix_uc}_CLASS(klass)   (GTK_CHECK_CLASS_TYPE ((klass), $typemacro))

struct _${new}Class {
	${base}Class		${base_suffix_lc}_class;
};
struct _${new} {
	${base}			$base_suffix_lc;
};

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
EOF

$filename = "$fname.c";
$filename .= ".generated" if -e $filename;

if ($from_plugin) {
	$GET_PARENT = "gdam_plugin_get_type_by_name(\"$base\")";
} else {
	$GET_PARENT = $basetypemacro;
}

open CFILE, ">$filename";
print CFILE $std_header;
print CFILE <<"EOF";

#include "$fname.h"

static void ${new_und_lc}_init($new* $suffix_lc)
{
}
static void ${new_und_lc}_class_init(${new}Class* ${suffix_lc}_class)
{
}

GtkType ${new_und_lc}_get_type()
{
	static GtkType ${suffix_lc}_type = 0;
	if (!${suffix_lc}_type) {
		static const GtkTypeInfo ${suffix_lc}_info = {
			"$new",
			sizeof($new),
			sizeof(${new}Class),
			(GtkClassInitFunc) ${new_und_lc}_class_init,
			(GtkObjectInitFunc) ${new_und_lc}_init,
			/* reserved_1 */ NULL,
			/* reserved_2 */ NULL,
			(GtkClassInitFunc) NULL
		};
		GtkType parent = $GET_PARENT;
		${suffix_lc}_type = gtk_type_unique(parent,
		                                 &${suffix_lc}_info);
	}
	return ${suffix_lc}_type;
}
EOF


__END__
/*
    GDAM - Geoff & Dave's Audio Mixer
    Copyright (C) 1999    Dave Benson, Geoff Matters.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

    Contact:
        daveb@ffem.org <Dave Benson>
        geoff@ugcs.caltech.edu <Geoff Matters>
*/
