about summary refs log tree commit diff stats
path: root/leo.pl
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-25 14:53:30 +0530
committerAndinus <andinus@nand.sh>2020-08-25 14:53:30 +0530
commit5a88b1e4a36d84e4989d2c96690595744914a44a (patch)
tree8e11fc2385ed0eb75f5cecb6f15c01e3b02b61f0 /leo.pl
parent73a9e1fe3d9d91b88fd786c0a6d857beac47d3f7 (diff)
downloadleo-5a88b1e4a36d84e4989d2c96690595744914a44a.tar.gz
Remove unused files/modules, add archive sub
`archive' will create tars of files, there is a pre-defined list.
Diffstat (limited to 'leo.pl')
-rwxr-xr-xleo.pl70
1 files changed, 68 insertions, 2 deletions
diff --git a/leo.pl b/leo.pl
index c7c6da7..1964d43 100755
--- a/leo.pl
+++ b/leo.pl
@@ -4,9 +4,20 @@ use strict;
 use warnings;
 use feature 'say';
 
-use Path::Tiny;
 use IPC::Run3;
+use Path::Tiny;
+use Getopt::Long qw/ GetOptions /;
+
+my %options = ();
+GetOptions(
+    \%options,
+    qw{ verbose }
+) or die "Error in command line arguments\n";
 
+
+# There will be multiple dispatch tables, to avoid confusion the main
+# one is named %dispatch & others will be named like %archive_dispatch
+# & so on.
 my %dispatch = (
     "archive" => \&archive,
 );
@@ -23,5 +34,60 @@ if ( $ARGV[0]
 sub HelpMessage {
     say qq{Usage:
     archive
-        Create an archive.}
+        Create an archive.};
+}
+
+sub rsync { run3 ["openrsync", @_]; }
+sub tar_create { run3 ["/bin/tar", "cf", @_]; }
+sub tar_list { run3 ["/bin/tar", "tvf", @_]; }
+
+# Creating tars of files.
+sub archive {
+    my %archive_dispatch = (
+        "documents" => sub {
+            tar_create("/tmp/archive/documents.tar",
+                       # Won't print "tar: Removing leading / from
+                       # absolute path names in the archive" to
+                       # STDERR.
+                       "-C", "$ENV{HOME}/documents", ".");
+
+            $? # tar returns 1 on errors.
+                ? die "Archive creation failed :: $?\n"
+                : say "$ENV{HOME}/documents archived.";
+            tar_list("/tmp/archive/documents.tar") if $options{verbose};
+        },
+        "journal" => sub {
+            tar_create("/tmp/archive/journal.tar",
+                       "-C", "$ENV{HOME}/documents",
+                       "andinus.org.gpg", "archive.org.gpg");
+
+            $?
+                ? die "Archive creation failed :: $?\n"
+                : say "$ENV{HOME}/documents/andinus.org.gpg,
+$ENV{HOME}/documents/archive.org.gpg archived.";
+            tar_list("/tmp/archive/journal.tar") if $options{verbose};
+        },
+    );
+
+    shift @ARGV;
+    if ( $ARGV[0]
+             and $archive_dispatch{ $ARGV[0] } ) {
+        path("/tmp/archive")->mkpath; # Create archive directory.
+        $archive_dispatch{ $ARGV[0] }->();
+    } elsif ( scalar @ARGV == 0 ) {
+        archive_HelpMessage();
+    } else {
+        say "leo/archive: no such option";
+    }
+
+    sub archive_HelpMessage {
+        say qq{Archive files to /tmp/archive.
+
+Usage:
+    documents
+        Archive $ENV{HOME}/documents
+    journal
+        Archive $ENV{HOME}/documents/andinus.org.gpg,
+                $ENV{HOME}/documents/archive.org.gpg};
+    }
 }