Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

13 years ago
13 years ago
13 years ago
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use 5.10.0;
  5. use POSIX qw(strftime);
  6. use File::Basename;
  7. my $root;
  8. if (defined $ENV{P1K3_ROOT}) {
  9. $root = $ENV{P1K3_ROOT} . '/archives';
  10. } else {
  11. # Fall back to a default:
  12. $root = $ENV{HOME} . '/workspace/p1k3/archives';
  13. }
  14. # I used to think strftime didn't have a non-zero-padded month/day;
  15. # it turns out you can do this with a - after the % (this may not
  16. # work everywhere, so my original workaround is commented below):
  17. my $dayfile = strftime("%Y/%-m/%-e", localtime());
  18. # $dayfile =~ s{/[0 ]}{/}gx;
  19. #
  20. # (Or: Use it anyway and apply an ad-hoc regex fix, because though we often
  21. # feel we know better in this day and age, we are not ashamed of text
  22. # transformation. not when the chips are down. text is the most powerful
  23. # computational abstraction i've ever come close to understanding, if i'm
  24. # honest, and past a certain point life is too short and too precious a span to
  25. # abandon the only tools you have ready to hand.)
  26. my $today = "$root/$dayfile";
  27. if (-d $today) {
  28. print $today;
  29. } else {
  30. my $dirname = $today;
  31. until (-d ($dirname)) {
  32. # If a dir for the month or year doesn't exist, this moves one step up.
  33. # Should land in archives/ if no year.
  34. $dirname = dirname($dirname);
  35. }
  36. print $dirname;
  37. }