dg-x/scripts/ptouch.pl

48 lines
962 B
Perl
Raw Normal View History

2022-02-07 06:47:31 +00:00
#!/usr/bin/env perl
use strict;
use utf8;
use Getopt::Std;
use POSIX qw(strftime);
use Tie::File;
$::VERSION = "1.0.0";
getopts("ne", \my %opts);
my $metavar;
if (defined $opts{n}) {
# "new" post
$metavar = "created";
} elsif (defined $opts{e}) {
# "edited" post
$metavar = "modified";
} else {
die "Neither -n[ew] nor -e[dited] are defined!";
}
my $pretty_date = strftime "%Y-%m-%d %H:%M", localtime;
my $fname = shift @ARGV;
tie my @fharr, 'Tie::File', $fname or die $!;
my $done = 0;
for (@fharr) {
if (s/^$metavar=.+/$metavar=$pretty_date/) {
print "$fname:$.: Overwriting existing \`$metavar\'\n";
$done++;
}
if (s/^(---)$/$metavar=$pretty_date\n$1/) {
print "$fname:$.: Inserting new \`$metavar\'\n";
$done++;
}
last if ($done);
}
if (!$done) {
print "$fname: No content and no \`$metavar\', inserting at end\n";
push @fharr, "$metavar=$pretty_date";
}
untie @fharr;