#!/bin/bash
# vim: expandtab:ts=4:sw=4:syntax=perl

# read rc files if exist
unset PROFILEDOTD
[ -e /etc/thruk/thruk.env  ] && . /etc/thruk/thruk.env
[ -e ~/etc/thruk/thruk.env ] && . ~/etc/thruk/thruk.env
[ -e ~/.thruk              ] && . ~/.thruk
[ -e ~/.profile            ] && . ~/.profile

BASEDIR=$(dirname $0)/..

# git version
if [ -d $BASEDIR/.git -a -e $BASEDIR/lib/Thruk.pm ]; then
  export PERL5LIB="$BASEDIR/lib:$BASEDIR/plugins/plugins-available/conf/lib:$PERL5LIB";
  if [ "$OMD_ROOT" != "" -a "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$OMD_ROOT/etc/thruk"; fi
  if [ "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$BASEDIR/"; fi

# omd
elif [ "$OMD_ROOT" != "" ]; then
  export PERL5LIB=$OMD_ROOT/share/thruk/lib:$OMD_ROOT/share/thruk/plugins/plugins-available/conf/lib:$PERL5LIB
  if [ "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$OMD_ROOT/etc/thruk"; fi

# pkg installation
else
  export PERL5LIB=$PERL5LIB:@DATADIR@/lib:@DATADIR@/plugins/plugins-available/conf/lib:@THRUKLIBS@;
  if [ "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG='@SYSCONFDIR@'; fi
fi

eval 'exec perl -x $0 ${1+"$@"} ;'
    if 0;
# / this slash makes vscode syntax highlighting work
#! -*- perl -*-
#line 35

##############################################
use strict;
use warnings;
use Pod::Usage;
use Monitoring::Config;

if(!defined $ARGV[0]) {
    pod2usage( { -verbose => 2,  -exit => 3, -message => "\nERROR: missing argument: object.cache\n\n" } );
}
if(!-f $ARGV[0]) {
    pod2usage( { -verbose => 2,  -exit => 3, -message => "\nERROR: ".$ARGV[0].': '.$!."\n\n" } );
}

my $odb      = Monitoring::Config->new({ obj_file => $ARGV[0], 'force' => 1 })->init();
my $contacts = $odb->get_objects_by_type('contact');
my $groups   = $odb->get_objects_by_type('contactgroup');
my $def      = $odb->get_default_keys('contact', { no_alias => 1, sort => 1 });

# convert groups
my $groups_by_contact = {};
for my $g (@{$groups}) {
    my $name = $g->{'conf'}->{'contactgroup_name'};
    for my $m (@{$g->{'conf'}->{'members'}}) {
        $groups_by_contact->{$m}->{$name} = 1;
    }
}

# print header
print join(';', map(ucfirst, @{$def})), ";Groups\n";

# print data
for my $o (@{$contacts}) {
    for my $attr (@{$def}) {
        if(!defined $o->{'conf'}->{$attr}) {
            print ";";
        } else {
            if(ref $o->{'conf'}->{$attr} eq 'ARRAY') {
                print join(',', @{$o->{'conf'}->{$attr}}), ";";
            } else {
                print $o->{'conf'}->{$attr}, ";";
            }
        }
    }
    my $name = $o->{'conf'}->{'contact_name'};
    print join(',', keys %{$groups_by_contact->{$name}});
    print "\n";
}

exit 0;

##############################################

=head1 NAME

contacts2csv - Command Line Utility which converts contacts from a object.cache file into csv

=head1 SYNOPSIS

Usage: contacts2csv .../object.cache

=head1 DESCRIPTION

This script coverts contacts from a object.cache file into csv

=head1 EXAMPLE

contacts2csv /tmp/object.cache > /tmp/contacts.csv

=head1 AUTHOR

2012, Sven Nierlein, <sven@consol.de>

=cut
