ec790d96d625fb2bc70b2b1356a71bec41798c67
[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 use feature 'say';
14 use Term::ReadKey;
15 use Time::HiRes;
16 use LWP::UserAgent;
17
18 =head1 NAME
19
20 B<grml-tips> - query a signature file for a specific keyword and display results
21
22 =head1 SYNOPSIS
23
24 B<grml-tips> [OPTION] I<keyword>
25
26 =head1 DESCRIPTION
27
28 This manual page documents briefly the B<grml-tips> command.
29
30 =head1 OPTIONS
31
32 =over 8
33
34 =item B<--help>
35
36 Print this help and exit.
37
38 =back
39
40 =head1 EXAMPLES
41
42 =over 8
43
44 =item B<grml-tips> I<ntfs>
45
46 Query grml-tips file for tips / hints including keyword  "ntfs".
47
48 =item B<grml-tips> I<.>
49
50 Display all available B<grml-tips> at once.
51
52 =back
53
54 =head1 FILES
55
56 /usr/share/grml-tips/grml_tips
57
58 Signature file containing the tips.
59
60 =head1 SEE ALSO
61
62 L<grml(1)>
63
64 =head1 AUTHOR
65
66 grml-tips was written by Alexander Wirt <formorer@grml.org>
67
68 =cut
69
70 my $grml_tips = '/usr/share/grml-tips/grml_tips';
71 my $pattern   = shift;
72
73 if ( $pattern eq '' ) {
74     pod2usage(
75         {   -message => 'No search pattern provided',
76             -exitval => -1,
77         }
78     );
79 }
80 elsif ( $pattern eq '--help' ) {
81     pod2usage();
82 }
83
84 my @tips;
85 if ( !open( my $fh, '<', "$grml_tips" ) ) {
86     say STDERR "Error: File \"$grml_tips\" not found.";
87     say STDERR "Exiting.";
88     exit -1;
89 }
90 else {
91     my $tip = '';
92
93     my $tips_found = 0;
94     while ( my $line = <$fh> ) {
95         if ( $line !~ /^-- $/ ) {
96             $tip .= $line;
97         }
98         else {
99             $tips_found++;
100             if ( $tip =~ /$pattern/mi ) {
101
102                 #$tip .= $line;
103
104                 my $header = "Grml Tip Number $tips_found\n";
105                 my $line = "-" x ( length($header) - 1 ) . "\n\n";
106
107                 push @tips, $header . $line . $tip . "\n";
108                 $tip = '';
109             }
110             else {
111                 $tip = '';
112             }
113         }
114     }
115     close($fh);
116 }
117
118 if (@tips) {
119     if ( !open( my $fh, '|-', 'less -FRX' ) ) {
120         say @tips;
121     }
122     else {
123         say $fh @tips;
124     }
125 }
126 else {
127     say "Sorry, could not find a tip for '$pattern'. :-(\n\n",
128         "Do you want to submit the keyword '$pattern' to grml's keyword database?\n",
129         "The grml team will write tips for the most requested and useful keywords.\n",
130         "To use and contribute to this feature you'll need a working networking connection.\n",
131         "No personal data will be transmitted to the database.\n\n",
132         "Send \"$pattern\" to grml's keyword database? [y|N] ";
133
134     ReadMode 4;    # Turn off controls keys
135     my $x;
136     while ( not defined( $x = ReadKey(-1) ) ) {
137         Time::HiRes::sleep(0.5);
138     }
139     ReadMode 0;    # Reset tty mode before exiting
140     print "\n\n";
141     if ( $x =~ /(y|j)/i ) {
142         my $version;
143         if ( -f '/etc/grml_version' ) {
144             open( my $fh, '<', '/etc/grml_version' )
145                 or die "Could not open /etc/grml_version: $!";
146             $version = <$fh>;
147             chomp $version;
148             close($fh);
149         }
150         elsif ( -f '/etc/debian_version' ) {
151             open( my $fh, '<', '/etc/debian_version' )
152                 or die "Could not open /etc/debian_version: $!";
153             $version = <$fh>;
154             chomp $version;
155             close($fh);
156         }
157         else {
158             $version = 'unknown';
159         }
160         my $ua = new LWP::UserAgent;
161         $ua->agent("grml-tips 0.0 ");    # set the HTTP 'browser' type
162         my $res = $ua->post(
163             'http://deb.grml.org/~formorer/submissions/keyword.cgi',
164             [   'version' => $version,
165                 'keyword' => $pattern
166             ],
167         );
168         if ( $res->is_success ) {
169             my $content = $res->decoded_content;
170             if ( $content =~ /Submission received/ ) {
171                 say
172                     "Keyword '$pattern' has been submitted to grml's keyword database.\nThanks.";
173             }
174             else {
175                 say "Your pattern could not be submitted.\n",
176                     "Please file a bug against grml-tips at ",
177                     "http://bts.grml.org/\n",
178                     "Thanks!";
179             }
180         }
181         else {
182             print "Could not submitt '$pattern': " . $res->status_line . "\n";
183         }
184
185     }
186     else {
187         print
188             "'$pattern' has not been sent to grml's keyword database as requested.\n";
189         print
190             "If you want to submit a tip please mail it to tips\@grml.org - thank you!\n";
191     }
192 }
193
194 ## END OF FILE #################################################################