#!/usr/bin/env perl -w
#
# Copyright (c) 2002 Jordan DeLong
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the author nor the names of contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

use strict;

#
# create a C file containing routines that will initialize and destroy
# modules that are being compiled statically
#

my @staticmodules = `perl .. ./listmods.pl static`;
chomp @staticmodules;

open FILE, "> staticmod.c" or die "can't open file for writing";

# make sure no one starts editing and wonders where their changes go
print FILE "/* generated by mkstatmodc.pl; do not edit by hand! */\n";
print FILE "#include \"wm.h\"";

# spit out extern decls to prevent compiler warnings
print FILE "\n";
foreach my $module (@staticmodules) {
	print FILE "extern int ${module}_init();\n";
	print FILE "extern void ${module}_shutdown();\n";
	print FILE "extern int ${module}_start();\n";
	print FILE "extern int ${module}_xevent_handler(XEvent *);\n";
}
print FILE "\n";

# create load function
print FILE "plugin_t *getstaticplugin(char *name) {\n";

# see if it's a static thing
foreach my $module (@staticmodules) {
	print FILE "	if (!strcmp(name, \"${module}\")) {\n";
	print FILE "		plugin_t *plug = malloc(sizeof(plugin_t));\n\n";
	print FILE "		if (!plug) err(1, \"malloc\");\n";
	print FILE "		plug->hnd = NULL;\n";
	print FILE "		plug->name = name;\n";
	print FILE "		plug->init = ${module}_init;\n";
	print FILE "		plug->shutdown = ${module}_shutdown;\n";
	print FILE "		plug->start = ${module}_start;\n";
	print FILE "		plug->xevent_handler = ${module}_xevent_handler;\n";
	print FILE "		warnx(\"%s: loaded statically\", name);\n";
	print FILE "		return plug;\n";
	print FILE "	}\n";
}
print FILE "\n	return NULL;\n}\n";

close FILE;
