#!/usr/bin/env perl
#
# vim: set ts=4:noet
#
# sbofind
# script to locate something in a local SlackBuilds tree.
#
# authors:	Jacob Pipkin <j@dawnrazor.net>
#			Luke Williams <xocel@iquidus.org>
#			Andreas Guldstrand <andreas.guldstrand@gmail.com>
# 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.
  -q|--queue:
	show the build queue for each found item.

Example:
  $self libsexy 

EOF
}

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,
);

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, $exit) = open_read "$config{SBO_HOME}/SLACKBUILDS.TXT";
	if ($exit) {
		warn $fh;
		exit $exit;
	}
	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, $exit) = open_read(shift);
	if ($exit) {
		warn $fh;
		return();
	}
	my $contents = do {local $/; <$fh>};
	for ($contents) {
		s/\n/\n        /g;
		s/\n        $//g;
	}
	return $contents;
}

# get build queue and return it as a single line. 
sub show_build_queue {
	exists $_[0] or script_error('show_build_queue requires an argument.');
	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) {
		for my $key (keys %$hash) {
			my $val = $hash->{$key};
			say "SBo:    $key";
			say "Path:   $val";
			say "info:   ". get_file_contents("$val/$key.info") if $show_info;
			say "README: ". get_file_contents("$val/README") if $show_readme;
			say "Queue:  ". show_build_queue("$key") if $show_queue;
			say '';
		}
	}
} else {
	say "Nothing found for search term: $search";
}

exit 0;
