Last active
August 20, 2020 09:12
-
-
Save ElectricCoffee/d0b51ad6088a5465a2fabd9379bcb627 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
=for comment | |
Copyright 2020 Niko Lepka | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
=cut | |
use v5.30; | |
use warnings; | |
no warnings qw( experimental::smartmatch ); | |
use autodie; | |
use POSIX; | |
use constant { | |
DAY => 3, | |
MONTH => 4, | |
YEAR => 5, | |
}; | |
my $scripts_folder = '/home/nle/scripts'; | |
my $date_path = "$scripts_folder/files/lastupdate"; | |
sub check_date { | |
my ($old, $new) = @_; | |
return $old->[YEAR] < $new->[YEAR] | |
|| $old->[MONTH] < $new->[MONTH] | |
|| $old->[DAY] < $new->[DAY]; | |
} | |
sub read_date { | |
open(my $handle, '<', $date_path); | |
chomp(my $line = <$handle>); | |
$line; | |
} | |
sub write_date { | |
my ($reason, @date) = @_; | |
my $str = join(',', @date, uc($reason)); | |
open(my $handle, '>', $date_path); | |
say $handle $str; | |
} | |
my $line = read_date; | |
my @current_time = localtime; | |
my @old_time = split(',', $line); | |
# perform check | |
if (!$line || check_date(\@old_time, \@current_time)) { | |
# prompt user | |
if (!$line) { | |
print "Last update never performed. "; | |
} else { | |
my $time = mktime @old_time; | |
printf( | |
"Last update performed on %4d-%02d-%02d. ", | |
$old_time[YEAR] + 1900, | |
$old_time[MONTH] + 1, | |
$old_time[DAY] | |
); | |
} | |
while() { | |
print "Check for updates? (y/n) "; | |
chomp(my $response = <STDIN>); | |
# execute command if needed | |
given ($response) { | |
when (m/(n|no)/i) { | |
write_date 'aborted', @current_time; | |
say 'Performing no updates.'; | |
exit; | |
} | |
when (m/(y|yes)/i) { | |
write_date 'updated', @current_time; | |
system "$scripts_folder/autoupdate"; | |
exit; | |
} | |
default { | |
say "Did not understand input $response, try again"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment