summaryrefslogtreecommitdiff
path: root/oldfiles
blob: c581ba0a4f192c1be039a69a5a6bab74cc26da76 (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 $compress = "gz";
   18 my %wanted;
   19 my %keepme;
   20 
   21 # Cannot specify -p -s together (use no options instead)
   22 if ( $options{"-p"} && $options{"-s"} ) {
   23 	usage();
   24 	exit 1;
   25 }
   26 
   27 # Assume both packages and sources if no options specified
   28 if ( !$options{"-p"} && !$options{"-s"} ) {
   29 	$options{"-p"} = 1;
   30 	$options{"-s"} = 1;
   31 }
   32 
   33 # Read pkgmk.conf
   34 open CONFIG, "/etc/pkgmk.conf" or die "Could not open /etc/pkgmk.conf";
   35 while (<CONFIG>) {
   36 	$srcdir = $1 if m/^PKGMK_SOURCE_DIR="(.*)"\n/;
   37 	$pkgdir = $1 if m/^PKGMK_PACKAGE_DIR="(.*)"\n/;
   38 	$compress = $1 if m/^PKGMK_COMPRESSION_MODE="(.*)"\n/;
   39 }
   40 close CONFIG;
   41 
   42 # Check if dirs are specified / exists
   43 if ( $options{"-p"} ) {
   44 	if ($pkgdir eq ""){
   45 		print "error: no PKGMK_PACKAGE_DIR specified in /etc/pkgmk.conf\n";
   46 		exit 1;
   47 	} else {
   48 		if (! -d $pkgdir) {
   49 			print "error: $pkgdir is not a directory\n";
   50 			exit 1;
   51 		}
   52 	}
   53 }
   54 if ( $options{"-s"} ) {
   55 	if ($srcdir eq ""){
   56 		print "error: no PKGMK_PACKAGE_DIR specified in /etc/pkgmk.conf\n";
   57 		exit 1;
   58 	} else {
   59 		if (! -d $srcdir) {
   60 			print "error: $srcdir is not a directory\n";
   61 			exit 1;
   62 		}
   63 	}
   64 }
   65 
   66 # Collect curent sources / packages
   67 foreach (split('\n', `prt-get printf "%p:%n:%v:%r\n"`)) {
   68 	my ($path, $name, $version, $release) = split(':', $_, 4);
   69 	if ( $options{"-p"} ) {
   70 		$wanted{$pkgdir}{"$name\#$version-$release.pkg.tar.$compress"} = 1;
   71 	}
   72 	if ( $options{"-s"} ) {
   73 		open SIGNATURES, "$path/$name/.signature" or next;
   74 		while (<SIGNATURES>) {
   75 			m/^SHA256\s\((.+)\)\s.+\n/;
   76 			if ($1 && !($1 =~ "Pkgfile") && !($1 =~ ".footprint")) {
   77 				$wanted{$srcdir}{$1} = 1;
   78 			}
   79 		}
   80 		close SIGNATURES;
   81 	}
   82 }
   83 
   84 # Keep user-specified files
   85 if ( -f "/etc/oldfiles.conf")   {
   86 	my $keep;
   87 	open KEEPME, "/etc/oldfiles.conf" or die "Could not open /etc/oldfiles.conf";
   88 	while ($keep = <KEEPME>) {
   89 		chomp($keep);
   90 		$keepme{$keep} = 1;
   91 	}
   92 }
   93 close KEEPME;
   94 
   95 # Display unwanted files
   96 foreach my $dir (keys %wanted) {
   97 	opendir DIR, $dir;
   98 	foreach (readdir DIR) {
   99 		next if ($_ eq '.' or $_ eq '..');
  100 		print "$dir/$_\n" unless ($wanted{$dir}{$_} or $keepme{"$dir/$_"});
  101 	}
  102 	closedir DIR;
  103 }
  104 
  105 ######################## subroutines ########################
  106 
  107 # Adapted  from Martin Opel's prtorphan script
  108 sub getoptions {
  109   my @args = reverse @_;
  110   my %options = ();
  111   
  112   while (my $argument = pop @args) {
  113     if ( $argument eq "-p" ) {
  114 		$options{"-p"} = 1;
  115     } elsif ( $argument eq "-s" ) {
  116 		$options{"-s"} = 1;
  117 	} else {
  118 		usage();
  119 		exit 1;
  120 	}
  121   } 
  122   return \%options;
  123 }
  124 
  125 # Show usage
  126 sub usage {
  127 	print "usage: oldfiles [-p|-s]\n";
  128 }

Generated by cgit