Add a nice header for every tip
[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
71 my $grml_tips = '/usr/share/grml-tips/grml_tips';
72 my $pattern = shift;
73
74 if ($pattern eq '') {
75     pod2usage( {
76             -message => 'No search pattern provided',
77             -exitval => -1, });
78 } elsif ($pattern eq  '--help') {
79     pod2usage();
80 }
81
82 my @tips;
83 if (! open (my $fh, '<', "$grml_tips")) {
84         say STDERR "Error: File \"$grml_tips\" not found.";
85         say STDERR "Exiting.";
86         exit -1;
87 } else {
88         my $tip = '';
89
90         my $tips_found = 0; 
91         while (my $line = <$fh>) {
92                 if ($line !~ /^-- $/) {
93                         $tip .= $line;
94                 } else {
95                         $tips_found++; 
96                         if ( $tip =~ /$pattern/mi ) {
97                                 #$tip .= $line;
98
99                                 my $header = "Grml Tip Number $tips_found\n";
100                                 my $line = "-" x (length($header) -1 ) . "\n\n"; 
101
102                                 push @tips, $header . $line . $tip . "\n"; 
103                                 $tip = '';
104                         } else {
105                                 $tip = '';
106                         }
107                 }
108         }
109         close($fh);
110 }
111
112 if (@tips) {
113         if (! open (my $fh, '|-', 'less -FRX') ) {
114                 say @tips; 
115         } else {
116                 say $fh @tips;
117         }
118 }else {
119         say "Sorry, could not find a tip for '$pattern'. :-(\n\n",
120         "Do you want to submit the keyword '$pattern' to grml's keyword database?\n",
121         "The grml team will write tips for the most requested and useful keywords.\n",
122         "To use and contribute to this feature you'll need a working networking connection.\n",
123         "No personal data will be transmitted to the database.\n\n",
124         "Send \"$pattern\" to grml's keyword database? [y|N] ";
125
126         ReadMode 4; # Turn off controls keys
127         my $x; 
128         while (not defined ($x = ReadKey(-1))) {
129                 Time::HiRes::sleep(0.5);
130         }
131         ReadMode 0; # Reset tty mode before exiting
132         print "\n\n";
133         if ($x =~ /(y|j)/i) {
134                 my $version; 
135                 if ( -f '/etc/grml_version' ) {
136                         open (my $fh, '<', '/etc/grml_version') or die "Could not open /etc/grml_version: $!";
137                         $version = <$fh>; 
138                         chomp $version; 
139                         close ($fh); 
140                 } elsif ( -f '/etc/debian_version') {
141                         open (my $fh, '<', '/etc/debian_version') or die "Could not open /etc/debian_version: $!"; 
142                         $version = <$fh>;
143                         chomp $version;
144                         close ($fh);
145                 } else {
146                         $version = 'unknown'; 
147                 }
148                 my $ua = new LWP::UserAgent;
149                 $ua->agent("grml-tips 0.0 "); # set the HTTP 'browser' type
150                 my $res = 
151                         $ua->post('http://deb.grml.org/~formorer/submissions/keyword.cgi', 
152                                         [ 'version' => $version,
153                                         'keyword' => $pattern
154                                         ],
155                                  );
156                 if ($res->is_success) {
157                         my $content =  $res->decoded_content;
158                         if ($content =~ /Submission received/) {
159                                 say "Keyword '$pattern' has been submitted to grml's keyword database.\nThanks.";
160                         } else {
161                                 say "Your pattern could not be submitted.\n",
162                                       "Please file a bug against grml-tips at ",
163                                       "http://bts.grml.org/\n",
164                                       "Thanks!";
165                         }
166                 } else {
167                         print "Could not submitt '$pattern': " . $res->status_line . "\n";
168                 }
169
170
171         } else {
172                 print "'$pattern' has not been sent to grml's keyword database as requested.\n";
173                 print "If you want to submit a tip please mail it to tips\@grml.org - thank you!\n";
174         }
175 }
176
177 ## END OF FILE #################################################################