aboutsummaryrefslogtreecommitdiff
path: root/sbocheck
blob: b551b1af8312dde53d6686235b446fa2b2c13185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl
#
# vim: set ts=4:noet
#
# sbocheck
# script to update the local sbo tree and check for updates
#
# authors: Jacob Pipkin <j@dawnrazor.net>
#          Luke Williams <xocel@iquidus.org>
#          Andreas Guldstrand <andreas.guldstrand@gmail.com>
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>

use 5.16.0;
use strict;
use warnings FATAL => 'all';
use SBO::Lib qw/ update_tree get_available_updates script_error open_fh is_local /;
use Getopt::Long;
use File::Basename;
use List::Util 'max';

my $self = basename($0);

sub show_usage {
	print <<EOF
Usage: $self

Options:
  -h|--help:
    this screen.
  -v|--version:
    version information.

EOF
}

my ($help, $vers);

GetOptions('help|h' => \$help, 'version|v' => \$vers);

show_usage() and exit 0 if $help;
show_version() and exit 0 if $vers;

update_tree();

# retrieve and format list of available updates
sub get_update_list {
	print "Checking for updated SlackBuilds...\n";
	my $updates = get_available_updates();
	return() unless exists $$updates[0];
	# consistent formatting - determine longest version string, which will tell
	# us the max minimum length of the left side of the output for stuff that
	# fits in 80 chars; stuff that doesn't will overflow.
	my @up_lengths;
	push @up_lengths, length $$updates[$_]{update} for keys @$updates;
	my $up_length = max @up_lengths;
	# "needs updating" bit without version is 30 characters
	my $remaining = 80 - ($up_length + 44);
	my @lengths;
	push @lengths, length "$$updates[$_]{name}-$$updates[$_]{installed}"
		for keys @$updates;
	my @s_lengths = sort {$b <=> $a} @lengths;
	my $min;
	FIRST: for my $len (@s_lengths) {
		if ($len < $remaining) {
			$min = $len;
			last FIRST;
		}
	}
	$min = $remaining unless $min;

	my @listing;
	for my $update (@$updates) {
		my $name = sprintf "%s %s", $$update{name}, $$update{installed};
		my $upd = "SBo has %s";
		if (is_local($$update{name})) { $upd = "%s from local overrides"; }
		push(@listing, sprintf "%-${min}s  <  needs updating ($upd)",
			$name, $$update{update});
	}
	return \@listing;
}

# print list of updates
sub print_output {
	exists $_[0] or script_error('print_output requires an argument');
	my $listing = shift;
	if (exists $$listing[0]) {
		print "\n";
		say $_ for @$listing;
		print "\n";
		# save a log of available updates
		my $logfile = '/var/log/sbocheck.log';
		unlink $logfile if -f $logfile;
		my ($log_fh, $exit) = open_fh($logfile, '>');
		# non-fatal
		if ($exit) {
			warn $log_fh if $exit;
		} else {
			say {$log_fh} $_ for @$listing;
			close $log_fh;
			say "A copy of the above result is kept in $logfile\n";
		}
	} else {
		say "\nNo updates available.";
	}
}

my $output = get_update_list();
print_output($output);

exit 0;