#!/usr/bin/perl -W
# CAVEAT:	Assumes ./template/ directory exists
# Purpose:	make a plugin directory with Makefile and .c and .h plugin
#		source files from template/{TEMPLATE.[ch],Makefile}
# Usage:	newplugin.pl PluginName PluginId
#	PluginId is an integer, unique among your plugin IDs
# Example:	newplugin.pl COUNTER 432	# create/populate new directory COUNTER-432
# Output Files:
#	./PluginName-PluginId	New plugin directory
#	./PluginName-PluginId/Makefile		New Makefile
#	./PluginName-PluginId/PluginName.c	New PluginName.c file
#	./PluginName-PluginId/PluginName.h	New PluginName.h file
#	./PluginName-PluginId/stdinc.h		New stdinc.h file
# Input Files:
#	./template/TEMPLATE.c	Template for PluginName.c file
#	./template/common.c	Template for common.c file
#	./template/TEMPLATE.h	Template for PluginName.h file
#	./template/stdinc.h	Template for stdinc.h file (actually constant)
#	./template/Makefile	Template for .h file
# Semantics
#   o <TEMPLATE> is replaced by PluginName 
#   o <MYID> is replaced by PluginId
# History:
#   6/30/2005:	kenw, modified from Stephen Levine's newplugin.pl code
#

if ($#ARGV < 1)
{
  print "Usage: newplugin.pl <Plugin Name> <Plugin Id>\n";
  exit(1);
}

$name = $ARGV[0];
$id = $ARGV[1];
$dir = "$name-$id";

mkdir($dir,0755) or die ("Couldn't create directory $dir");

# create PluginName.h file
open(TEMPLATEH, "<./template/TEMPLATE.h") or die ("Couldn't open TEMPLATE.h");
open(HOUT, ">$dir/$name.h") or die ("Couldn't open $dir/$name.h");

while ($line = <TEMPLATEH>)
{
  $line =~ s/<TEMPLATE>/$name/g;
  $line =~ s/<MYID>/$id/;
  print HOUT $line;
}
close(TEMPLATEH);
close(HOUT);

# create PluginName.c file
open(TEMPLATEC, "<./template/TEMPLATE.c") or die ("Couldn't open TEMPLATE.c");
open(COUT, ">$dir/$name.c") or die ("Couldn't open $dir/$name.c");

while ($line = <TEMPLATEC>)
{
  $line =~ s/<TEMPLATE>/$name/g;
  print COUT $line;
}

close(TEMPLATEC);
close(COUT);

# create Makefile
open(TMAKEFILE, "<./template/Makefile") or die ("Couldn't open Makefile");
open(MOUT, ">$dir/Makefile") or die ("Couldn't open $dir/Makefile");

while ($line = <TMAKEFILE>)
{
  $line =~ s/<TEMPLATE>/$name/g;
  print MOUT $line;
}

close(TMAKEFILE);
close(MOUT);

$cmd = "cp ./template/stdinc.h ./$dir";
system("$cmd") == 0 or die "$cmd failed\n";

