#------------------------------------------------------------ 
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------ 
#!/usr/bin/perl

use strict;

use lib '/app/tools/common/lib';
use Common::RSApp;

use lib '/app/tools/rps/lib/';
use RPS::DB::Item::TrackLicense;
use RPS::DB::Item::ControlledCompositionLicense;


create_licenses();

sub create_licenses
{
	# Instantiate the application singleton object.
	#
	my $clientID = $ARGV[0];
	unless ($clientID =~ /^\d+$/ && $clientID > 0)
	{
		die usage("You must specify a valid client ID");
	}

	my $appSingleton = Common::RSApp->new(clientID => $clientID);

	my $trackLicenses = RPS::DB::Item::TrackLicense->GetByType(RPS::DB::Item::TrackLicense::kControlledComposition);
	my $count = 0;
	while($trackLicenses->hasNext())
	{
		my $trkLicense = $trackLicenses->next();

		my $newCCompLic = RPS::DB::Item::ControlledCompositionLicense->Create();
		$newCCompLic->controlled_composition_id($trkLicense->id);
		$newCCompLic->save();

		$trkLicense->id($newCCompLic->controlled_composition_license_id);
		$trkLicense->save();
		$count++;
	}
	print "Created $count controlled comp licenses.\n";
}



sub usage
{
	my $errstr = shift;
	my $text = ($errstr) ? "ERROR: $errstr\n" : '';
	$text .= "Usage: $0 <clientID>\n";
	return $text;
}

