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