Adjusted script so it matches case-insensitive
[grml-tips.git] / grml-tips
1 #!/usr/bin/perl
2 # Filename:      grml-tips
3 # Purpose:       query a signature file for a specific keyword and display results
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, (c) Alexander Wirt <formorer@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Sam Mär 03 15:35:38 CET 2007 [mika]
8 ################################################################################
9
10 use strict;
11 use Pod::Usage;
12
13 =head1 NAME
14
15 B<grml-tips> - query a signature file for a specific keyword and display results
16
17 =head1 SYNOPSIS
18
19 B<grml-tips> [OPTION] I<keyword>
20
21 =head1 DESCRIPTION
22
23 This manual page documents briefly the B<grml-tips> command.
24
25 =head1 OPTIONS
26
27 =over 8
28
29 =item B<--help>
30
31 Print this help and exit.
32
33 =back
34
35 =head1 EXAMPLES
36
37 =over 8
38
39 =item B<grml-tips> I<ntfs>
40
41 Query grml-tips file for tips / hints including keyword  "ntfs".
42
43 =item B<grml-tips> I<.>
44
45 Display all available B<grml-tips> at once.
46
47 =back
48
49 =head1 FILES
50
51 /usr/share/grml-tips/grml_tips
52
53 Signature file containing the tips.
54
55 =head1 SEE ALSO
56
57 L<grml(1)>
58
59 =head1 AUTHOR
60
61 grml-tips was written by Alexander Wirt <formorer@grml.org>
62
63 =cut
64
65
66 my $grml_tips = '/usr/share/grml-tips/grml_tips';
67 my $pattern = shift;
68
69 if ($pattern eq '') {
70     pod2usage( {
71             -message => 'No search pattern provided',
72             -exitval => -1, });
73 } elsif ($pattern eq  '--help') {
74     pod2usage();
75 }
76
77 my @tips;
78 my $fh;
79 if (! open ($fh, '<', "$grml_tips")) {
80         print "Error: $grml_tips not found.\nExiting.";
81         exit -1;
82 }
83
84 my $tip = '';
85 my $tip_flag = 0;
86
87 while (my $line = <$fh>) {
88     if ($line !~ /^-- $/) {
89         $tip .= $line;
90     } else {
91         if ( "$tip" =~ /$pattern/mi ) {
92             $tip .= $line;
93             print "$tip";
94             $tip = '';
95             $tip_flag = 1;
96         } else {
97             $tip = '';
98         }
99     }
100 }
101 close($fh);
102
103 print "Sorry, could not find a tip for '$pattern'. :-(\n"
104     . "If you want to submit a tip please mail it to tips\@grml.org - thank "
105     . "you!\n\n" unless $tip_flag;
106
107 ## END OF FILE #################################################################