Write into tag file
[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 ################################################################################
8
9 use strict;
10 use Pod::Usage;
11
12 use feature 'say';
13 use Term::ReadKey;
14 use Time::HiRes;
15 use LWP::UserAgent;
16 use Getopt::Long;
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<searchpattern|tag>
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 =item B<--tagsonly>
39
40 Match on tags only instead of the whole tip 
41
42 =item B<--tipsfile TIPSFILE>
43
44 Use TIPSFILE instead of /usr/share/grml-tips/grml_tips
45
46 =back
47
48 =head1 EXAMPLES
49
50 =over 8
51
52 =item B<grml-tips> I<ntfs>
53
54 Query grml-tips file for tips / hints including keyword  "ntfs".
55
56 =item B<grml-tips> I<.>
57
58 Display all available B<grml-tips> at once.
59
60 =back
61
62 =head1 FILES
63
64 /usr/share/grml-tips/grml_tips
65
66 Signature file containing the tips.
67
68 =head1 SEE ALSO
69
70 L<grml(1)>
71
72 =head1 AUTHOR
73
74 grml-tips was written by Alexander Wirt <formorer@grml.org>
75
76 =cut
77
78 my $grml_tips = '/usr/share/grml-tips/grml_tips';
79
80 my $help;
81 my $tagsonly;
82 my $tipsfile; 
83
84 my $result = GetOptions (
85     "help" => \$help,
86     "tagsonly" => \$tagsonly,
87     "tipsfile=s" => \$grml_tips, 
88 );
89
90 my $pattern   = shift;
91
92 #help if pattern is missing;
93 pod2usage(
94     {   -message => 'No search pattern provided',
95         -exitval => -1,
96     }
97 ) unless $pattern;
98
99 #help if help is wanted
100 pod2usage() if $help;
101
102 my @tips;
103
104 if ( !open( my $fh, '<', "$grml_tips" ) ) {
105     say STDERR "Error: File \"$grml_tips\" not found.";
106     say STDERR "Exiting.";
107     exit -1;
108 }
109 else {
110     my $tip = '';
111
112     my $tips_found = 0;
113     while ( my $line = <$fh> ) {
114         if ( $line !~ /^-- $/ ) {
115             $tip .= $line;
116         }
117         else {
118             my $header = "Grml Tip Number $tips_found\n";
119             my $line = "-" x ( length($header) - 1 ) . "\n\n";
120
121             $tips_found++;
122             if ($tagsonly) {
123                 #extract tags from tip
124                 my ($tag) = $tip =~ /^Tags: (.*)$/m;
125                 my @tags = split(/[, ]+/, $tag);
126                 if (grep(/^$pattern$/i, @tags) ) {
127                     push @tips, $header . $line . $tip . "\n";
128                 }
129                 $tip = '';
130             } else {
131                 if ( $tip =~ /$pattern/mi ) {
132                     push @tips, $header . $line . $tip . "\n";
133                     $tip = '';
134                 }
135                 else {
136                     $tip = '';
137                 }
138             }
139         }
140     }
141     close($fh);
142 }
143
144 if (@tips) {
145     if ( !open( my $fh, '|-', 'less -FRX' ) ) {
146         say @tips;
147     }
148     else {
149         say $fh @tips;
150     }
151 }
152 else {
153     say "Sorry, could not find a tip for '$pattern'. :-(\n\n",
154         "Do you want to submit the keyword '$pattern' to grml's keyword database?\n",
155         "The grml team will write tips for the most requested and useful keywords.\n",
156         "To use and contribute to this feature you'll need a working networking connection.\n",
157         "No personal data will be transmitted to the database.\n\n",
158         "Send \"$pattern\" to grml's keyword database? [y|N] ";
159
160     ReadMode 4;    # Turn off controls keys
161     my $x;
162     while ( not defined( $x = ReadKey(-1) ) ) {
163         Time::HiRes::sleep(0.5);
164     }
165     ReadMode 0;    # Reset tty mode before exiting
166     print "\n\n";
167     if ( $x =~ /(y|j)/i ) {
168         my $version;
169         if ( -f '/etc/grml_version' ) {
170             open( my $fh, '<', '/etc/grml_version' )
171                 or die "Could not open /etc/grml_version: $!";
172             $version = <$fh>;
173             chomp $version;
174             close($fh);
175         }
176         elsif ( -f '/etc/debian_version' ) {
177             open( my $fh, '<', '/etc/debian_version' )
178                 or die "Could not open /etc/debian_version: $!";
179             $version = <$fh>;
180             chomp $version;
181             close($fh);
182         }
183         else {
184             $version = 'unknown';
185         }
186         my $ua = new LWP::UserAgent;
187         $ua->agent("grml-tips 0.0 ");    # set the HTTP 'browser' type
188         my $res = $ua->post(
189             'http://deb.grml.org/~formorer/submissions/keyword.cgi',
190             [   'version' => $version,
191                 'keyword' => $pattern
192             ],
193         );
194         if ( $res->is_success ) {
195             my $content = $res->decoded_content;
196             if ( $content =~ /Submission received/ ) {
197                 say
198                     "Keyword '$pattern' has been submitted to grml's keyword database.\nThanks.";
199             }
200             else {
201                 say "Your pattern could not be submitted.\n",
202                     "Please file a bug against grml-tips at ",
203                     "http://bts.grml.org/\n",
204                     "Thanks!";
205             }
206         }
207         else {
208             print "Could not submitt '$pattern': " . $res->status_line . "\n";
209         }
210
211     }
212     else {
213         print
214             "'$pattern' has not been sent to grml's keyword database as requested.\n";
215         print
216             "If you want to submit a tip please mail it to tips\@grml.org - thank you!\n";
217     }
218 }
219
220 ## END OF FILE #################################################################