#!/usr/bin/perl
# -*- perl -*-

eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if 0;

use strict;
use POSIX;
use FindBin qw($RealBin);
use AppConfig;
use Pod::Usage;
use File::Basename;

# create a new AppConfig object
my $config = AppConfig->new({
			     CREATE=>1
			    });

$config->define(
		"templatedir|template=s",
		"package|p=s",
		"version" => { ALIAS => "V",
			       ARGS => "=s",
			       DEFAULT=>"1.0.0"
			     },
		"author|name|n|a=s",
		"mail|m=s",
		"verbose!",
		"bug|b=s",
		"help|h!",
		"dyalog_version=s",
		"dyalog_config" => { ARGS => "=s",
				     DEFAULT=> &default_dyalog_config()
				    }
		);

sub default_dyalog_config {
  # Detect if the script is run from the package or the installation place
  # useful to known where to fetch templates files
  my $dyalog_config='dyalog-config';
  $dyalog_config = "/home/mandrake/rpm/BUILD/DyALog-1.12.0/$dyalog_config" 
    if ($RealBin =~ m{^/home/mandrake/rpm/BUILD/DyALog-1.12.0});
  return $dyalog_config;
}

my $configfile = find_config_file('.templaterc',"$ENV{HOME}/.templaterc");

print "Reading config file $configfile\n";

$config->file($configfile) if ($configfile);

pod2usage(1) unless (@ARGV);

$config->getopt();

pod2usage(0) if $config->help();

pod2usage( -exitval=>1, -msg=>"$0: missing package name") unless $config->package();

$config->bug($config->mail()) unless $config->bug();

my $dyalog_config = $config->dyalog_config();

my $default_template_dir = `$dyalog_config --data`;
chomp $default_template_dir;
$default_template_dir .= '/templates' unless ($default_template_dir =~ /Templates$/);

my $package = $config->package();
my $version = $config->version();
my $templatedir=$config->templatedir() || $default_template_dir;

my $dyalog_version = $config->dyalog_version() || `$dyalog_config --version`;
chomp($dyalog_version);

verbose("dyalog_config $dyalog_config");
verbose("templatedir '$templatedir'");

my %subst = (
	     'APPPACKAGE' => "$package",
	     'APPVERSION' => "$version",
	     'APPAUTHOR' => $config->author(),
	     'APPEMAIL' => $config->mail(),
	     'APPBUG' => $config->bug(),
	     'APPNAME' => "$package",
	     'APPFILES' => "$package.pl",
	     'DYALOGVERSION' => "$dyalog_version"
	     );

my $pat = join('|',sort keys %subst);
$pat = qr/$pat/; 

######################################################################

my $dir = "$package-$version";

verbose("Create directory $dir");

mkdir $dir || die "can't create $dir";

foreach my $file (<$templatedir/*>) {
  verbose("Template file $file");
  my $base = basename($file);
  if ($base =~ s/^app-//) {
    verbose("Create file $dir/$base from template $file");
    open(TEMPLATE,"<$file") || die "can't open template $file";
    open(FILE,">$dir/$base") || die "can't open file $dir/$base";
    while (<TEMPLATE>) {
	s/\@($pat)\@/$subst{$1}/eg;
	print FILE $_;
    }
    close TEMPLATE;
    close FILE;
  } else {
    verbose("Copy file $file to $dir/$base");
    open(TEMPLATE,"<$file") || die "can't open file $file";
    open(FILE,">$dir/$base") || die "can't open file $dir/$base";
    while (<TEMPLATE>) {
	print FILE $_;
    }
    close TEMPLATE;
    close FILE;
  }
  chmod(0755,"$dir/$base") if $base =~ /\.sh$/;
}

print <<EOF;
DyALog project $package version $version $dir created in directory $dir.
To complete your project,
       cd $dir
       add your sources files
       edit Makefile.am
       run ./autogen.sh
       make
Good luck :-)
EOF


######################################################################

sub verbose (@) {
    print STDERR @_,"\n" if $config->verbose();
}

sub find_config_file {
  my @files = @_;
  foreach my $file (@files) {
    return $file if -r $file;
  }
  return 0;
}

__END__

=head1 NAME

dyalog-template - Perl script to set-up a project skeleton for DyALog

=head1 SYNOPSIS

B<dyalog-template> B<option>...

=head1 OPTIONS

=over 5

=item B<-p> B<--package> Package name

=item B<-v> B<--version> Package version (default 1.0.0)

=item B<-a> B<--author> Package's author name

=item B<-m> B<--mail> Author email

=item B<-b> B<--bug> Bugreport email (default Author email)

=item B<--verbose> Verbose mode

=item B<-h> B<--help> this help

=back

=head1 SEE ALSO

DyALog <http://atoll.inria.fr/~clerger>

=head1 AUTHORS

Eric de la Clergerie <Eric.De_La_Clergerie@inria.fr>

=cut
 
### Local Variables:
### comment-column:0
### comment-start: "### "
### comment-end:""
### mode: perl
### End:

