#!/usr/bin/perl # # vim: set ts=4:noet # # sbofind # script to locate something in a local SlackBuilds tree. # # authors: Jacob Pipkin # Luke Williams # Andreas Guldstrand # license: WTFPL use 5.16.0; use strict; use warnings FATAL => 'all'; use SBO::Lib qw/ slackbuilds_or_fetch script_error open_read get_build_queue %config $slackbuilds_txt $repo_path show_version in indent /; use File::Basename; use Getopt::Long qw(:config bundling); my $self = basename($0); sub show_usage { print <<"EOF"; Usage: $self (search_term) Options: -h|--help: this screen. -v|--verison: version information. -i|--info: show the .info for each found item. -r|--readme: show the README for each found item. -q|--queue: show the build queue for each found item. Example: $self libsexy EOF return 1; } my ($help, $vers, $show_info, $show_readme, $show_queue); GetOptions( 'help|h' => \$help, 'version|v' => \$vers, 'info|i' => \$show_info, 'readme|r' => \$show_readme, 'queue|q' => \$show_queue, ); if ($help) { show_usage(); exit 0 } if ($vers) { show_version(); exit 0 } if (!@ARGV) { show_usage(); exit 1 } my $search = $ARGV[0]; # if we can't find SLACKBUILDS.TXT in $config{HOME}, prompt to fetch the tree slackbuilds_or_fetch(); # find anything with $search in its name sub perform_search { script_error 'perform_search requires an argument.' unless @_ == 1; my $search_arg = shift; # first get a bunch of names from the TAGS.txt if it's available my $tags_file = "$config{SBO_HOME}/repo/TAGS.txt"; my @names; if (-r $tags_file) { my ($t_fh, $t_exit) = open_read "$config{SBO_HOME}/repo/TAGS.txt"; unless ($t_exit) { while (my $line = <$t_fh>) { if ($line =~ /^(\S+):\s.*\Q$search_arg\E/i) { push @names, $1; } } } } my $loc_regex = qr/LOCATION:\s+\.?(.*)$/; my ($fh, $exit) = open_read $slackbuilds_txt; if ($exit) { warn $fh; exit $exit; } my (%local, @findings); FIRST: while (my $line = <$fh>) { next FIRST unless $line =~ /NAME:/; # Try to match either the search string or any of the names from the TAGS.txt foreach my $search (".*" . quotemeta($search_arg) . ".*", map { quotemeta } @names) { if (my ($name) = $line =~ /NAME:\s+($search)$/i) { # If the name matches a local override, use its location if ($config{LOCAL_OVERRIDES} ne 'FALSE' and -d "$config{LOCAL_OVERRIDES}/$name") { push @findings, {name => $name, location => "$config{LOCAL_OVERRIDES}/$name", local => 1 }; $local{$name} = 1; next FIRST; } # Otherwise the location should be in the next line LOCATION: { my $loc_line = <$fh>; if (my ($location) = $loc_line =~ $loc_regex) { push @findings, {name => $name, location => $repo_path . $location}; next FIRST; } else { redo LOCATION; # But if it isn't, we try again... } } } } } if ($config{LOCAL_OVERRIDES} ne 'FALSE') { opendir(my $dh, $config{LOCAL_OVERRIDES}); while (my $dir = readdir($dh)) { next if $local{$dir}; if ($dir =~ /\Q$search_arg\E/ or in($dir, @names)) { push @findings, {name => $dir, location => "$config{LOCAL_OVERRIDES}/$dir", local => 1 }; } } closedir $dh; } return \@findings; } # pull the contents of a file into a variable and format it for output sub get_file_contents { script_error 'get_file_contents requires an argument' unless @_ == 1; my $file = shift; -f $file or return "$file doesn't exist.\n"; my ($fh, $exit) = open_read($file); if ($exit) { warn $fh; return(); } my $contents = do {local $/; <$fh>}; return "\n" . indent 6, $contents; } # get build queue and return it as a single line. sub show_build_queue { script_error('show_build_queue requires an argument.') unless @_ == 1; my $queue = get_build_queue([shift], {}); return join(" ", reverse @$queue); } my $findings = perform_search($search); # pretty formatting if (exists $$findings[0]) { for my $hash (@$findings) { my $name = $hash->{name}; my $location = $hash->{location}; my $sbo = "SBo: "; $sbo = "Local: " if $hash->{local}; say "$sbo $name"; say "Path: $location"; say "info: ". get_file_contents("$location/$name.info") if $show_info; say "README: ". get_file_contents("$location/README") if $show_readme; say "Queue: ". show_build_queue($name) if $show_queue; say ''; } } else { say "Nothing found for search term: $search"; } exit 0;