# Initial data for a fresh AT Installation.
use RTx::AssetTracker;
use RTx::AssetTracker::Type;

@Groups = (
    { Name        => '',
      Type        => 'Owner',                                  # loc
      Domain      => 'RTx::AssetTracker::System-Role',
      Instance    => '',
      Description => 'SystemRolegroup for internal use',    # loc
    },
    { Name        => '',
      Type        => 'Admin',                             # loc
      Domain      => 'RTx::AssetTracker::System-Role',
      Instance    => '',
      Description => 'Pseudogroup for internal use',        # loc
    }, );

@Types = ({ Name               => 'Servers',
             Description       => 'The default asset type',
          });

@Final = ( \&SystemRights, \&AssetTypes, \&AssetScripConditions, \&AssetScripActions, \&AssetTemplates, );

my $CurrentUser = RT::CurrentUser->new();
$CurrentUser->LoadByName('RT_System');

sub SystemRights {
    $| = 1;

    print "Creating Superuser ACL...";

    my $superuser_ace = RT::ACE->new($CurrentUser);
    $superuser_ace->_BootstrapCreate(
                         PrincipalId => ACLEquivGroupId( $CurrentUser->Id ),
                         PrincipalType => 'Group',
                         RightName     => 'SuperUser',
                         ObjectType    => 'RTx::AssetTracker::System',
                         ObjectId      => '1' );
    
    my $root_user = RT::User->new($CurrentUser);
    $root_user->Load("root");
    $root_user->PrincipalObj->GrantRight( Right => 'SuperUser',
                                          Object => $RTx::AssetTracker::System );
    
    print "done.\n";
}

sub AssetTypes {
    print "Creating default asset types...";

    for $item (@Types) {
        my $new_entry = new RTx::AssetTracker::Type($CurrentUser);
        my ( $return, $msg ) = $new_entry->Create(%$item);
        print "(Error: $msg)" unless ($return);
        print $return. ".";
    }
    print "done.\n";
}

sub AssetScripConditions {
    print "Creating default asset scrips conditions...";

	my @Conditions = (
	    { Name => 'User Defined',
		  Description => 'Whenever a user-defined condition occurs',
		  ExecModule => 'UserDefined',
		  Argument => undef,
		  ApplicableTransTypes => 'Any' },
	);

    for $item (@Conditions) {
        my $new_entry = new RTx::AssetTracker::ScripCondition($CurrentUser);
        my ( $return, $msg ) = $new_entry->Create(%$item);
        print "(Error: $msg)" unless ($return);
        print $return. ".";
    }
    print "done.\n";
}

sub AssetScripActions {
    print "Creating default asset scrips actions...";

	my @Conditions = (
	    { Name => 'User Defined',
		  Description => 'Perform a user-defined action',
		  ExecModule => 'UserDefined',
		  Argument => undef },
	);

    for $item (@Conditions) {
        my $new_entry = new RTx::AssetTracker::ScripAction($CurrentUser);
        my ( $return, $msg ) = $new_entry->Create(%$item);
        print "(Error: $msg)" unless ($return);
        print $return. ".";
    }
    print "done.\n";
}

sub AssetTemplates {
    print "Creating default asset templates...";

	my @Conditions = (
	    { AssetType => 0,
		  Name => 'Blank',
		  Description => 'A blank template',
		  Content => undef },
	);

    for $item (@Conditions) {
        my $new_entry = new RTx::AssetTracker::Template($CurrentUser);
        my ( $return, $msg ) = $new_entry->Create(%$item);
        print "(Error: $msg)" unless ($return);
        print $return. ".";
    }
    print "done.\n";
}
