An init script for the selenium server rc. Not sure if this is the best location...
[sgn-devtools.git] / newtemplate
blobcb726df959e304366b48f59147a74a0147566454
1 #!/usr/bin/perl
3 eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
6 use strict;
7 use warnings;
9 use English;
10 use Carp;
11 use FindBin;
13 use File::Basename;
14 use File::Path;
16 use Getopt::Std;
17 use Pod::Usage;
19 #use Data::Dumper;
21 our %opt;
22 getopts('n',\%opt) or pod2usage(1);
24 @ARGV == 2 or pod2usage(1);
26 my ($tname,$newfile) = @ARGV;
27 my $tgtpath = dirname($newfile);
29 my ($sgn_tools_location) = $FindBin::RealBin =~ m!(^.+/sgn-tools)/!;
30 $sgn_tools_location && -d $sgn_tools_location
31 or die "could not figure out sgn-tools installed location";
33 my @templatematches = ( glob("~/{T,t}emplates/$tname*"),
34 glob("$sgn_tools_location/development/templates/$tname*"),
35 glob("$FindBin::RealBin/templates/$tname*"),
37 or die "No template found matching '$tname";
39 @templatematches = grep !/~$/,@templatematches; #filter out backup files
40 @templatematches == 1
41 or die "More than one template matches '$tname':\n",map {" - $_\n"} @templatematches;
43 my ($template) = @templatematches;
45 #add an extension to the target file if it doesn't have one
46 my (undef,undef,$ext) = fileparse($template,qr/\..+$/);
47 $newfile .= $ext unless $newfile =~ /$ext$/;
50 -d $tgtpath
51 or mkpath( $tgtpath, {verbose => 1})
52 or die "could not create dir '$tgtpath'";
54 #copy the template file there
55 system( cp => -aH => $template => $newfile );
56 $CHILD_ERROR and die "Could not copy template from $template to $newfile: $!";
58 exit if $opt{n};
60 #openoffice
61 if( $template =~ /\.od[pfts]$|\.sx[iwc]$|\.doc$|\.xls$|\.ppt$/ ) {
62 exec 'ooffice', $newfile;
64 #scripts or other things
65 else {
66 $ENV{EDITOR} or die "No \$EDITOR environment variable defined, please make sure it's in your .bashrc or similar\n";
67 exec e => $newfile;
70 __END__
72 =head1 NAME
74 newtemplate - script to do something
76 =head1 SYNOPSIS
78 newtemplate [options] template_name new_filename
80 Options:
82 -n if passed, do not open $EDITOR after making file
84 =head1 MAINTAINER
86 Robert Buels
88 =head1 AUTHOR
90 Robert Buels, E<lt>rmb32@cornell.eduE<gt>
92 =head1 COPYRIGHT & LICENSE
94 Copyright 2009 Boyce Thompson Institute for Plant Research
96 This program is free software; you can redistribute it and/or modify
97 it under the same terms as Perl itself.
99 =cut