#!/usr/bin/perl -w
# BEGIN LICENSE BLOCK
# 
#  Copyright (c) 2002-2003 Jesse Vincent <jesse@bestpractical.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License 
#  as published by the Free Software Foundation.
# 
#  A copy of that license should have arrived with this
#  software, but in any event can be snarfed from www.gnu.org.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
# END LICENSE BLOCK

use strict;

use lib qw(/opt/rt3/lib/);

use RT;
RT::LoadConfig();
RT::Init();

use RT::FM::ClassCollection;
use RT::FM::CustomFieldCollection;

use vars qw/ @Classes @CustomFields @ClassCFs/;

my $datafile = shift;

require "$datafile";


# Create classes
print "Creating classes: ";
foreach my $class (@Classes) {
        my $obj = RT::FM::Class->new($RT::SystemUser);
        my ($val, $msg) = $obj->Create(%$class);
        if ( $val  ) { 
                print $val .".";
        } else {
                die $msg;
        }       
}
        print "\n";


# Create custom fields
foreach my $CustomField (@CustomFields) {
        my $obj = RT::FM::CustomField->new($RT::SystemUser);

        my $Classes = $CustomField->{'Classes'};
        delete $CustomField->{'Classes'};
        my $values = $CustomField->{'Values'};
        delete $CustomField->{'Values'};

        my ($val, $msg) = $obj->Create(%$CustomField);
        if ( $val  ) { 
                print $val .".";
        } else {
                die $msg;
        }       


        foreach my $value (@$values) {
                $obj->AddValue(%$value);
        }
        foreach my $class (@$Classes) {
                $obj->AddToClass($class);
        }
        print "\n";
}
