#!/usr/bin/perl
# Filename:      grml-tips
# Purpose:       query a signature file for a specific keyword and display results
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, (c) Alexander Wirt <formorer@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

use strict;
use Pod::Usage;

use feature 'say';
use Term::ReadKey;
use Time::HiRes;
use LWP::UserAgent;
use Getopt::Long;

=head1 NAME

B<grml-tips> - query a signature file for a specific keyword and display results

=head1 SYNOPSIS

B<grml-tips> [OPTION] I<searchpattern|tag>

=head1 DESCRIPTION

This manual page documents briefly the B<grml-tips> command.

=head1 OPTIONS

=over 8

=item B<--help>

Print this help and exit.

=item B<--tagsonly>

Match on tags only instead of the whole tip 

=item B<--tipsfile TIPSFILE>

Use TIPSFILE instead of /usr/share/grml-tips/grml_tips

=back

=head1 EXAMPLES

=over 8

=item B<grml-tips> I<ntfs>

Query grml-tips file for tips / hints including keyword  "ntfs".

=item B<grml-tips> I<.>

Display all available B<grml-tips> at once.

=back

=head1 FILES

/usr/share/grml-tips/grml_tips

Signature file containing the tips.

=head1 SEE ALSO

L<grml(1)>

=head1 AUTHOR

grml-tips was written by Alexander Wirt <formorer@grml.org>

=cut

my $grml_tips = '/usr/share/grml-tips/grml_tips';

my $help;
my $tagsonly;
my $tipsfile; 

my $result = GetOptions (
    "help" => \$help,
    "tagsonly" => \$tagsonly,
    "tipsfile=s" => \$grml_tips, 
);

my $pattern   = shift;

#help if pattern is missing;
pod2usage(
    {   -message => 'No search pattern provided',
        -exitval => -1,
    }
) unless $pattern;

#help if help is wanted
pod2usage() if $help;

my @tips;

if ( !open( my $fh, '<', "$grml_tips" ) ) {
    say STDERR "Error: File \"$grml_tips\" not found.";
    say STDERR "Exiting.";
    exit -1;
}
else {
    my $tip = '';

    my $tips_found = 0;
    while ( my $line = <$fh> ) {
        if ( $line !~ /^-- $/ ) {
            $tip .= $line;
        }
        else {
            my $header = "Grml Tip Number $tips_found\n";
            my $line = "-" x ( length($header) - 1 ) . "\n\n";

            $tips_found++;
            if ($tagsonly) {
                #extract tags from tip
                my ($tag) = $tip =~ /^Tags: (.*)$/m;
                my @tags = split(/[, ]+/, $tag);
                if (grep(/^$pattern$/i, @tags) ) {
                    push @tips, $header . $line . $tip . "\n";
                }
                $tip = '';
            } else {
                if ( $tip =~ /$pattern/mi ) {
                    push @tips, $header . $line . $tip . "\n";
                    $tip = '';
                }
                else {
                    $tip = '';
                }
            }
        }
    }
    close($fh);
}

if (@tips) {
    if ( !open( my $fh, '|-', 'less -FRX' ) ) {
        say @tips;
    }
    else {
        say $fh @tips;
    }
}
else {
    say "Sorry, could not find a tip for '$pattern'. :-(\n",
        "Feel free to submit a pull request at https://github.com/grml/grml-tips\n";
}

## END OF FILE #################################################################
