aboutsummaryrefslogtreecommitdiff
path: root/sbofind
blob: a2f731da6d093f325dc8de357764a8ff8f5597f9 (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
111
112
113
114
115
#!/usr/bin/env perl
#
# vim: set ts=4:noet
#
# sbofind
# script to locate something in a local SlackBuilds tree.
#
# author: Jacob Pipkin <j@dawnrazor.net>
# date: Boomtime, the 39th day of Discord in the YOLD 3178
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>

use 5.16.0;
use strict;
use warnings FATAL => 'all';
use SBO::Lib;
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.

Example:
  $self libsexy 

EOF
}

my ($help, $vers, $show_info, $show_readme);

GetOptions (
	'help|h'	=> \$help,
	'version|v'	=> \$vers,
	'info|i'	=> \$show_info,
	'readme|r'	=> \$show_readme,
);

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

show_usage and exit 1 unless exists $ARGV[0];
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 ($) {
	exists $_[0] or script_error 'perform_search requires an argument.';
	my $search = shift;
	my (@findings, $name, $found);
	my $name_regex = qr/NAME:\s+(.*\Q$search\E.*)$/i;
	my $loc_regex = qr/LOCATION:\s+(.*)$/;
	my $fh = open_read "$config{SBO_HOME}/SLACKBUILDS.TXT";
	FIRST: while (my $line = <$fh>) {
		unless ($found) {
			$found++, next FIRST if $name = ($line =~ $name_regex)[0];
		} else {
			if (my ($location) = ($line =~ $loc_regex)[0]) {
				$found = 0;
				$location =~ s#^\.##;
				push @findings, {$name => $config{SBO_HOME} . $location};
			}
		}
	}
	return \@findings;
}

# pull the contents of a file into a variable and format it for output
sub get_file_contents ($) {
	exists $_[0] or script_error 'get_file_contents requires an argument';
	-f $_[0] or return "$_[0] doesn't exist.\n";
	my $fh = open_read shift;
	my $contents = do {local $/; <$fh>};
	for ($contents) {
		s/\n/\n        /g;
		s/        $//g;
	}
	return $contents;
}

my $findings = perform_search $search;

# pretty formatting
if (exists $$findings[0]) {
	my @listing = ("\n");
	for my $hash (@$findings) {
		while (my ($key, $value) = each %$hash) {
			push @listing, "SBo:    $key\n";
			push @listing, "Path:   $value\n";
			push @listing, "info:   ". get_file_contents "$value/$key.info"
				if $show_info;
			push @listing, "README: ". get_file_contents "$value/README"
				if $show_readme;
			push @listing, "\n";
		}
	}
	print $_ for @listing;
} else {
	say "Nothing found for search term: $search";
}

exit 0;