#!/usr/bin/perl

use diagnostics;
use strict;
use warnings;

#use Modern::Perl;
use File::Find;

#################
# Configuration #
#################
#use constant PT_LIBRARY => "/var/rudder/configuration-repository/policy-templates";
use constant PT_LIBRARY => "/home/jclarke/Documents/RUDDER/github/rudder-policy-templates/policies";

my $ptName, my $ptVersion;
sub getPolicyTemplateFullName {
	( $ptName, $ptVersion ) = @_;
	return find ( \&matchPolicyTemplateDirectory, PT_LIBRARY );

	sub matchPolicyTemplateDirectory {
		$File::Find::name =~ /$ptName\/$ptVersion$/ && return "Found $ptName: $File::Find::name\n";
	}

}

print getPolicyTemplateFullName("common", "1.0");
print "\n";



# TODO: get PostgreSQL username and password from rudder-web.properties
my $psqlUsername = "rudder";
my $psqlPassword = "Normation";

# Connect to database
$dbh = DBI->connect("dbi:Pg:dbname=rudder;host=localhost", $psqlUsername, $psqlPassword, {AutoCommit => 1});

# Get all Policy Template name-version couples in use
my $ptsUsed = $dbh->prepare("SELECT DISTINCT policypackagename, policypackageversion FROM policyinstances WHERE PolicyTemplateHumanName IS NULL");
$ptsUsed->execute();

# Cycle through each name-version couple
while(my $ptNameVersion = $ptsUsed->fetchrow_hashref()) {
	    print "$ptNameVersion->{'policypackagename'} is used in version $ptNameVersion->{'policypackageversion'}\n";
}

# Clean up
$dbg->disconnect();
