#!/usr/bin/env perl

use strict;
use warnings;

# Splits the output of the rug command
# and checks if the given key fingerprint matches
# one of the key list

sub Logic {

        # Variables init
        my $name = $_[0];
        my $key_id = $_[1];
        my $key_fingerprint = $_[2];
        my $index = $_[3];

        my $rug_cmd = '/usr/bin/rug kl';

        open(RUG_CMD, "$rug_cmd |") or die;

        while ( defined( my $line = <RUG_CMD> ) )
        {
                                # print $line;
                                chomp($line); # Suppress CR/LF

                                if ( my ($regexp) = ( $line =~ m/^(.*)\s+\| (.*?) +\| (\S*)\s*/ ) )
                                {
                        # Does this words matches the wanted ones ?
                        if ( ( $name eq $1 )&&( $key_id eq $2 )&&( $key_fingerprint eq $3 ) )
                        {
                                print("+key_".$index."_matched");
                                exit(0);
                        }
                }
        }
        print("+key_".$index."_not_matched");
}

Logic(@ARGV);
