blob: c59fcfb125915ca682bd29c154c51d82af6ea71a (
plain)
1 #!/usr/bin/env perl
2 #
3 # oldfiles - show outdated packages and sources for a CRUX system
4 #
5 # by Simone Rota <sip@varlock.com>,
6 # Mark Rosenstand <mark@borkware.net>
7 # License: GNU GPL v2
8 #
9 # Requires prt-get by Johannes Winkelmann
10
11 use warnings;
12 use strict;
13
14 my %options = %{getoptions(@ARGV)};
15 my $pkgdir = "";
16 my $srcdir = "";
17 my %wanted;
18 my %keepme;
19
20 # Cannot specify -p -s together (use no options instead)
21 if ( $options{"-p"} && $options{"-s"} ) {
22 usage();
23 exit 1;
24 }
25
26 # Assume both packages and sources if no options specified
27 if ( !$options{"-p"} && !$options{"-s"} ) {
28 $options{"-p"} = 1;
29 $options{"-s"} = 1;
30 }
31
32 # Read pkgmk.conf
33 open CONFIG, "/etc/pkgmk.conf" or die "Could not open /etc/pkgmk.conf";
34 while (<CONFIG>) {
35 $srcdir = $1 if m/^PKGMK_SOURCE_DIR="(.*)"\n/;
36 $pkgdir = $1 if m/^PKGMK_PACKAGE_DIR="(.*)"\n/;
37 }
38 close CONFIG;
39
40 # Check if dirs are specified / exists
41 if ( $options{"-p"} ) {
42 if ($pkgdir eq ""){
43 print "error: no PKGMK_PACKAGE_DIR specified in /etc/pkgmk.conf\n";
44 exit 1;
45 } else {
46 if (! -d $pkgdir) {
47 print "error: $pkgdir is not a directory\n";
48 exit 1;
49 }
50 }
51 }
52 if ( $options{"-s"} ) {
53 if ($srcdir eq ""){
54 print "error: no PKGMK_PACKAGE_DIR specified in /etc/pkgmk.conf\n";
55 exit 1;
56 } else {
57 if (! -d $srcdir) {
58 print "error: $srcdir is not a directory\n";
59 exit 1;
60 }
61 }
62 }
63
64 # Collect curent sources / packages
65 foreach (split('\n', `prt-get printf "%p:%n:%v:%r\n"`)) {
66 my ($path, $name, $version, $release) = split(':', $_, 4);
67 if ( $options{"-p"} ) {
68 $wanted{$pkgdir}{"$name\#$version-$release.pkg.tar.gz"} = 1;
69 }
70 if ( $options{"-s"} ) {
71 open MD5SUMS, "$path/$name/.md5sum" or next;
72 while (<MD5SUMS>) {
73 m/^[a-f0-9]{32}\s\s(\S+)\n/;
74 $wanted{$srcdir}{$1} = 1;
75 }
76 close MD5SUMS;
77 }
78 }
79
80 # Keep user-specified files
81 if ( -f "/etc/oldfiles.conf") {
82 my $keep;
83 open KEEPME, "/etc/oldfiles.conf" or die "Could not open /etc/oldfiles.conf";
84 while ($keep = <KEEPME>) {
85 chomp($keep);
86 $keepme{$keep} = 1;
87 }
88 }
89 close KEEPME;
90
91 # Display unwanted files
92 foreach my $dir (keys %wanted) {
93 opendir DIR, $dir;
94 foreach (readdir DIR) {
95 next if ($_ eq '.' or $_ eq '..');
96 print "$dir/$_\n" unless ($wanted{$dir}{$_} or $keepme{"$dir/$_"});
97 }
98 closedir DIR;
99 }
100
101 ######################## subroutines ########################
102
103 # Adapted from Martin Opel's prtorphan script
104 sub getoptions {
105 my @args = reverse @_;
106 my %options = ();
107
108 while (my $argument = pop @args) {
109 if ( $argument eq "-p" ) {
110 $options{"-p"} = 1;
111 } elsif ( $argument eq "-s" ) {
112 $options{"-s"} = 1;
113 } else {
114 usage();
115 exit 1;
116 }
117 }
118 return \%options;
119 }
120
121 # Show usage
122 sub usage {
123 print "usage: oldfiles [-p|-s]\n";
124 }
|