aboutsummaryrefslogtreecommitdiff
path: root/sbofind
blob: 9b54c1725f345c5f6e27319c41f5ba5a9ebe9152 (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
#!/usr/bin/env perl
#
# 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 SBO::Lib;
use File::Basename;
use Getopt::Std;
use strict;
use warnings FATAL => 'all';

my %config = %SBO::Lib::config;
my $self = basename ($0);

sub show_usage {
	print <<EOF
Usage: $self (search_term)

Options:
  -h: this screen.
  -v: version information.
  -r: show the README for each found item.

Example:
  $self libsexy 

EOF
}

my %options;
getopts ('hvr', \%options);

show_usage () and exit (0) if (exists $options{h});
show_version () and exit (0) if (exists $options{v});

my $show_readme = exists $options{r} ? 'TRUE' : 'FALSE';

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
my (@findings, $name);
my $found = 'FALSE';
my $regex = qr/NAME:\s.*\Q$search\E.*/;
open my $sb_txt, '<', "$config{SBO_HOME}/SLACKBUILDS.TXT";
FIRST: while (my $line = <$sb_txt>) {
	unless ($found eq 'TRUE') {
		if ($line =~ $regex) {
			$found = 'TRUE';
			my @split = split (' ', $line);
			chomp ($name = $split[2]);
			next FIRST;
		}
	} else {
		if ($line =~ /LOCATION/) {
			$found = 'FALSE';
			my @split = split (' ', $line);
			chomp (my $location = $split[2]);
			$location =~ s#^\.##;
			my %hash = ($name => $config{SBO_HOME} . $location);
			push (@findings, \%hash);
		}
	}
}

if (exists $findings[0]) {
	# pretty formatting
	my @listing;
	for my $hash (@findings) {
		while (my ($key, $value) = each %{$hash}) {
			push (@listing, "SBo:    $key\n");
			push (@listing, "Path:   $value\n");
			if ($show_readme eq 'TRUE') {
				open my $fh, '<', "$value/README";
				my $readme = do {local $/; <$fh>};
				$readme =~ s/\n/\n        /g;
				push (@listing, "README: $readme");
				close $readme;
			}
			push (@listing, "\n");
		}
	}
	for my $list (@listing) {
		print $list;
	}
} else {
	print "Nothing found for search term: $search\n";
}

exit 0;