From 6f66ba27129c50610569b0ad1ccc0f0873cfbb42 Mon Sep 17 00:00:00 2001
From: "Martin Klimes (k11m1)" <xklimes5@dares.fi.muni.cz>
Date: Tue, 16 Feb 2021 15:47:49 +0100
Subject: [PATCH] auto updater via cron

---
 README.md  |  4 +++-
 cron.pl    | 36 +++++++++++++++++++++-------
 install.pl | 69 ++++++++++++++++++++++++++++++++----------------------
 3 files changed, 71 insertions(+), 38 deletions(-)

diff --git a/README.md b/README.md
index b18509e..387bbde 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,9 @@ If you are printing with unauthenticated IPP or Kerberized IPP, you will have to
 
 ## Usage
 
-Usage: `./install.pl <printer-name>` , where `<printer-name>` is a valid printer name. For example, to install printer `lj2b` run
+Usage: `./install.pl [--usage] <printer-name>` , where `<printer-name>` is a valid printer name. If supplied `--usage` script will only update drivers.
+
+For example, to install printer `lj2b` run
 
 ```shell
 ./install.pl lj2b
diff --git a/cron.pl b/cron.pl
index 28fdecd..caee240 100755
--- a/cron.pl
+++ b/cron.pl
@@ -1,15 +1,29 @@
 #!/usr/bin/perl -w
+use File::Copy;
+use strict;
+
+if (-d "/opt/fi-printers") {
+    #check
+    print "Opt dir exits\n";
+    my $output  = `git pull --ff-only`;
+    if ($output eq "Already up to date.\n") {
+        print "Nothing to do";
+        # exit;
+    }
+}
+else
+{
+    #clone
+    my $output = `git clone https://gitlab.fi.muni.cz/unix/fi-printers.git /opt/fi-printers`;
+}
+
+# copy cron.pl into weekly cron
+copy("/opt/fi-printers/cron.pl", "/etc/cron.weekly/fi-printers.pl");
+
+
 
-my $message = "Already up to date.";
 
-my $output  = `git pull --ff-only`;
 
-if ($output eq "Already up to date.\n") {
-    print "Nothing to do";
-    exit;
-}
-print $output;
- 
 my @files = glob( '/etc/cups/ppd/*.ppd' );
 
 
@@ -22,5 +36,9 @@ for my $item (@files) {
 }
 
 for my $printer (@files) {
-	system("./install.pl --update $printer")
+    my $command = "/opt/fi-printers/install.pl --update $printer";
+    print "$command \n";
+	system($command);
 }
+
+
diff --git a/install.pl b/install.pl
index e5ce923..6eed6ca 100755
--- a/install.pl
+++ b/install.pl
@@ -10,23 +10,10 @@ use File::Copy;
 use File::Basename;
 use Getopt::Long	qw(:config bundling);
 
-#my $opt = {
-
-#};
 
 #--------------------------------------------------------------------
-#  Option processing
+#  DATA
 #--------------------------------------------------------------------
-my $only_update = '';
-my $only_usage = '';
-GetOptions(
-    'update'     => \$only_update,
-    'help|h|?'   => \$only_usage
-) or die;
-
-########
-# DATA #
-########
 
 my $printers = {
     'copy4b' => {
@@ -111,9 +98,24 @@ my $models = {
     }
 };
 
-###############
-# END OF DATA #
-###############
+#--------------------------------------------------------------------
+#  Option processing
+#--------------------------------------------------------------------
+
+my $only_update = '';
+my $only_usage = '';
+GetOptions(
+    'update'     => \$only_update,
+    'help|h|?'   => \$only_usage
+) or die;
+
+if ($only_usage || @ARGV != 1) {
+   usage();
+}
+
+#--------------------------------------------------------------------
+#  Helper functions
+#--------------------------------------------------------------------
 
 sub get_description {
     my ($printer_name) = @_;
@@ -225,11 +227,12 @@ sub usage {
     say
       "Script for installing faculty printers on Linux via Samba print server.";
     say "Requires local CUPS server running and Samba client installed.";
-    say "usage: $0 <printer-name>";
+    say "usage: $0 [--update] <printer-name>";
     say "Must be run as root.";
     say "Triumph-Adler 50007ci needs Python 2.7 as a dependency";
     say "and will install 2 additional python modules automatically.";
-    print "\n\n";
+    say "Option --update only updates installed printer";
+    print "\n";
     print_printers();
 
     exit 1;
@@ -281,14 +284,22 @@ sub pre_TA5007ci {
     return;
 }
 
-#######################
-# START OF THE SCRIPT #
-#######################
-
-# Show help
-if ($only_usage || @ARGV != 1) {
-   usage(); 
+sub check_cron {
+    if (! -e "/etc/cron.weekly/fi_printers_cron.pl") {
+        print "[PROMPT] Do you want to enable automatic updates via cron? [Y/n]";
+
+        chomp(my $answer = <STDIN>);
+        if ($answer eq 'Y' || $answer eq 'y' || $answer eq '') {
+            print "CRON SHALL BE INSTALLED\n";
+            copy("cron.pl", "/etc/cron.weekly/fi_printers_cron.pl")
+                or die "Could not copy cron script\n";
+            chmod 0700, "/etc/cron.weekly/fi_printers_cron.pl"
+        }
+    }
 }
+#--------------------------------------------------------------------
+#  Start of the script
+#--------------------------------------------------------------------
 
 my ($printer_name) = @ARGV;
 
@@ -320,9 +331,11 @@ for my $filter (@{($models->{$model} // {})->{filters} // []}) {
 }
 if ($only_update) {
 	update_ppd("$printer_name");
-	say "[INFO] Update is completed.";
+	say "[INFO] Update of $printer_name is completed.";
 }
 else {
 	install_printer("$printer_name");
-	say "[INFO] Installation is completed.";
+	say "[INFO] Installation of $printer_name is completed.";
+    check_cron();
 }
+
-- 
GitLab