#!/usr/bin/perl use strict; use warnings; my $prgname = $0; #### parse_command_line ################################################ # Return a digested form of the command line arguments sub parse_command_line { my ($pn, $inifile) = @_; usage() unless defined($inifile); my @pkgnames = split ',', $pn; usage("no packages given") if @pkgnames == 0; usage("cannot read INI file") unless -r $inifile; return ($inifile, \@pkgnames); } #### parse_cygwin_setup_ini_file ####################################### # Extract dependency info from the Cygwin setup.ini file. sub parse_cygwin_setup_ini_file { my ($inifile, $piref) = @_; open my $ini, '<', $inifile or die "Cannot read INI file $inifile: $!\n"; # Skip to first package entry while (<$ini>) { last if /^@/; } # Parse package entries my %deps; while (defined $_) { chomp; my $p = substr $_, 2; my $obs = 0; while (<$ini>) { if (/^@/) { # Found next package entry; restart outer loop last; } elsif (/^category: Base$/) { # Mark this one as a special sort of root package: one # we're going to install regardless of user selection, # so we need not list it in our output. $piref->{$p} = 2; } elsif (/^category: _obsolete$/) { # Select this package's replacement instead below. $piref->{$p} = 0; $obs = 1; } elsif (/^requires:/) { # Save this package's requirements as its dependents list. my ($junk, @deps) = split; $deps{$p} = \@deps; # If this package was marked obsolete above, select its # replacement as provisionally to-be-installed. That # package still might end up removed from our output list # if it in turn is a dependent of one of the packages we # consider a "root" package at the end. $piref->{$deps[0]} = 1 if $obs; } } } close $ini; return \%deps; } #### usage ############################################################# # Print usage message plus optional error string, then exit sub usage { my ($error) = @_; print "ERROR: $error\n\n" if length($error); print <<"USAGE"; usage: $prgname packages ini-path packages is a comma-separated list of Cygwin package names, as produced by: \$ tail -n+2 /etc/setup/installed.db | cut -f1 -d' ' | tr '\\n' , ini-path is the path to a Cygwin setup.ini file. USAGE exit ($error ? 1 : 0); } #### main ############################################################## my ($inifile, $pkgnames) = parse_command_line(@ARGV); # Convert package list to a hash so we can mark them non-root by name my %packages = map { $_ => 1 } @$pkgnames; my $deps = parse_cygwin_setup_ini_file($inifile, \%packages); # For each given package name, mark any of its dependencies also found # on the command line as as non-root. for my $p (@$pkgnames) { my $pdref = $deps->{$p}; for my $d (@$pdref) { $packages{$d} = 0; } } # Collect list of root packages and print it out print join ',', sort(grep { $packages{$_} == 1 } @$pkgnames);