updated git and svn scripts
[xrzperl.git] / editpipe
blob8923bf174f32e5964766a39b148ebc2af10f562f
1 #!/usr/bin/perl -w
2 # $Id$
3 use strict;
4 require v5.10.0;
5 our $VERSION = 'v0.1';
7 BEGIN
9 my $PROGRAM_DIR = $0;
10 $PROGRAM_DIR =~ s/[^\/\\]+$//;
11 $PROGRAM_DIR = "./" unless($PROGRAM_DIR);
12 unshift @INC,
13 map "$PROGRAM_DIR$_",qw{modules lib ../modules ..lib};
16 my %OPTS;
17 my @OPTIONS = qw/help|h|? version|ver edit-me manual|man/;
19 if(@ARGV)
21 require Getopt::Long;
22 require MyPlace::Usage;
23 Getopt::Long::GetOptions(\%OPTS,@OPTIONS);
24 MyPlace::Usage::Process(\%OPTS,$VERSION);
26 else
28 require MyPlace::Usage;
29 MyPlace::Usage::PrintHelp();
32 my $editor="r-edit";
33 $editor = "notepad++" if($^O =~ /win/i);
34 my $fname=shift @ARGV;
36 if(!$fname) {
37 print STDERR "Usage: $0 [options] filename\n";
38 exit 1;
41 if (-f $fname) {
42 print STDERR "$fname exists, openning it...\n";
43 exec $editor,$fname;
44 exit 0;
46 print STDERR "Creating \"$fname\"...\n";
47 open PIPE,">:utf8",$fname or die("$!\n");
48 print PIPE <STDIN>;
49 close PIPE;
51 if(-f $fname)
53 use File::stat;
54 my $oldst = stat($fname);
55 print STDERR "Opening it in editor...\n";
56 system("$editor","$fname");
57 my $newst = stat($fname);
58 if($oldst->mtime < $newst->mtime) {
59 print STDERR "$fname modified.\n";
61 else {
62 print STDERR "File doesn't be modified, cancel.\n";
63 unlink($fname) or print STDERR "$!\n";
67 __END__
69 =pod
71 =head1 NAME
73 editpipe - Editor helper, creating file from PIPE
75 =head1 SYNOPSIS
77 editpipe [options] filename
79 cat template | editpipe filename
81 =head1 OPTIONS
83 =over 12
85 =item B<--version>
87 Print version infomation.
89 =item B<-h>,B<--help>
91 Print a brief help message and exits.
93 =item B<--manual>,B<--man>
95 View application manual
97 =item B<--edit-me>
99 Invoke 'editor' against the source
101 =back
103 =head1 DESCRIPTION
105 Editor helper program, which creates file from
106 PIPE, invokes the editor(r-edit), delete the
107 file if no modifition.
109 =head1 CHANGELOG
111 2010-08-12 xiaoranzzz <xiaoranzzz@myplace.hell>
113 * file created.
115 =head1 AUTHOR
117 xiaoranzzz <xiaoranzzz@myplace.hell>
119 =cut