#!/usr/bin/perl
# Filename:      grml-tips-tags
# Purpose:       Extract available patterns from grml-tips file
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, (c) Alexander Wirt <formorer@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

use strict;
use Getopt::Long;

my $grml_tips = '/usr/share/grml-tips/grml_tips';

my $result = GetOptions (
        "tipsfile=s" => \$grml_tips
);

open (my $fh, '<', $grml_tips) or die "Could not open $grml_tips: $!";

my @tags;

while (my $line = <$fh>) {
    next unless $line =~ /^Tags: (.*)$/;
    push @tags, split(/[, ]+/, $1);
}

close ($fh);
#make tags unique
my %seen = ();
@tags = grep { ! $seen{$_} ++ } @tags;

open (my $fh, '>', '/usr/share/grml-tips/tags') 
	or die "Could not open '/usr/share/grml-tips/tags' for writing: $!";
print $fh join("\n", @tags) . "\n";
close($fh)
