#!/usr/bin/perl -sw
#
# Created by Steve Voisey (srv) 06 Jun 2011.
# email: [steve.voisey@ericsson.com]
#
# file:    adiGen.pl
# version: V1.018
#
# Author  Date      Version Description
# srv     24/jan/12 V1.10  Renamed from generateAdi.pl to adiGen.pl
#                          Enable providerId and productType to be set as input parameters.
# srv     09/feb/12 V1.11  Introduce meta/content root/default. Makes easier to customize.
# srv     01/may/12 V1.12  Add display options only functionality.
# srv     11/may/12 V1.13  Verify existance of each content type before updating ADI, prevents undef errors.
# srv     13/may/12 V1.14  Change name to default to value of parameter content
# srv     15/may/12 V1.15  Add parameter metafile to support NON ADI.XML metadata
# srv     17/may/12 V1.16  Add repeat ingest and export for load testing. Add new text input parameter.
# srv     18/may/12 V1.17  Add support for a list of token files.
# srv     19/may/12 V1.18  Add support command line sub of ANY token, wait n seconds for load testing, locking.

use Switch;
use Cwd;
use File::stat;
use File::Basename;

my $DIVIDE = "=" x 80 . "\n" ;
my $numberDigits = 4;
my ($seedId, $element)  = "";
my $timeStamp = ` date '+%H%M%S'`;
chomp($timeStamp);

my $site = "CHANGE-ME";
my %checkSumHash;
my %propertiesHash;
my $thisDir = cwd();
my @tokenFileList = ();

# pbuilder stuff
#export pbscriptdir="$scrdir/pbuilder/scripts"
#alias ncdpb='cd $pbscriptdir'
#alias pbexample='echo "> cdpb; ./PackageExporter file PACC0011010000000036 /test/aditar/PACC0011010000000036"'
#


if (defined($help))        { help(); exit;}
unless(defined($scripts))  { $scripts = "/opt/tandbergtv/cms/scripts"; }
unless (defined($prop))    { $prop = "$scripts/conf/generateAdi.properties"; }
unless(defined($pbuilderDir))  { $pbuilderDir = "$scripts/pbuilder/scripts"; }

returnPropertyHash( $prop, \%propertiesHash );

if(defined($metaroot)) { $propertiesHash{metaroot} = "$metaroot"; } 
unless (defined($propertiesHash{metaroot})) { $propertiesHash{metaroot} = "$scripts/adi/meta"; } 

if(defined($metafile)) { $propertiesHash{metafile} = "$metafile"; } 
unless (defined($propertiesHash{metafile})) { $propertiesHash{metafile} = "ADI.XML"; } 

if(defined($meta)) { $propertiesHash{meta} = "$meta"; } 
unless (defined($propertiesHash{meta})) { $propertiesHash{meta} = "$scripts/adi/meta/default"; } 
# If meta does NOT start with a full directory path, append the path metaroot.
# Makes it easier to customize from the command line.
unless( $propertiesHash{meta} =~ /^\// ) { $propertiesHash{meta} = $propertiesHash{metaroot} . "/" . $propertiesHash{meta}; }

if(defined($contentroot)) { $propertiesHash{contentroot} = "$contentroot"; }
unless (defined($propertiesHash{contentroot})) { $propertiesHash{contentroot} = "$scripts/adi/content"; } 

if(defined($content)) { $propertiesHash{content} = "$content"; }
unless (defined($propertiesHash{content})) { $propertiesHash{content} = "default"; } 
# If content does NOT start with a full directory path, append the path contentroot.
unless( $propertiesHash{content} =~ /^\// ) { $propertiesHash{content} = $propertiesHash{contentroot} . "/" . $propertiesHash{content}; }

if(defined($out)) { $propertiesHash{out} = "$out"; }
unless (defined($propertiesHash{out})) { $propertiesHash{out} = "$thisDir/aditar"; } 

if(defined($tmp)) { $propertiesHash{tmp} = "$tmp"; }
unless (defined($propertiesHash{tmp})) { $propertiesHash{tmp} = "/var/tmp/adi.${timeStamp}"; } 

if(defined($tools)) { $propertiesHash{tools} = "$tools"; }
unless (defined($propertiesHash{tools})) { $propertiesHash{tools} = "$scripts/tools"; } 

if(defined($limit)) { $propertiesHash{limit} = "$limit"; }
unless (defined($propertiesHash{limit})) { $propertiesHash{limit} = 1; } 

if(defined($type)) { $propertiesHash{type} = "$type"; }
unless (defined($propertiesHash{type})) { $propertiesHash{type} = "clear"; } 

if(defined($ingest)) { $propertiesHash{ingest} = "$ingest"; }
unless (defined($propertiesHash{ingest})) { $propertiesHash{ingest} = "no"; } 

if(defined($ingestdir)) { $propertiesHash{ingestdir} = "$ingestdir"; }
unless (defined($propertiesHash{ingestdir})) { $propertiesHash{ingestdir} = "/content/ingest/${site}/"; } 

if(defined($unpack)) { $propertiesHash{unpack} = "$unpack"; }
unless (defined($propertiesHash{unpack})) { $propertiesHash{unpack} = "no"; } 

if( $propertiesHash{ingest} =~ /yes/i ) {
    unless( -d "$propertiesHash{ingestdir}") { 
        print "\nERROR: Unable to perform auto ingest\n";
        print "       The ingest directory [$propertiesHash{ingestdir}] does not exist!\n";
        exit;
    }    
}

if(defined($export)) { $propertiesHash{export} = "$export"; }
unless (defined($propertiesHash{export})) { $propertiesHash{export} = "no"; } 

if( $propertiesHash{export} =~ /yes/i ) {

    unless( -f "$pbuilderDir/PackageExporter") { 
        print "\nERROR: Unable to perform auto export\n";
        print "       Tool pbuilder is not installed and configured under [$pbuilderDir/PackageExporter]\n";
        exit;
    }    
}

if(($propertiesHash{ingest} =~ /yes/i) && ($propertiesHash{export} =~ /yes/i)) {
    print "\nERROR: Both parameters export AND ingest should NOT be set to 'yes'\n";
    print "       You should not ingest the same package twice via two different methods at the same time!\n";
    exit;    
}

if(defined($wait)) { $propertiesHash{wait} = "$wait"; }
unless (defined($propertiesHash{wait})) { $propertiesHash{wait} = 0; } 

if(defined($seeddir)) { $propertiesHash{seeddir} = "$seeddir"; }
unless (defined($propertiesHash{seeddir})) { $propertiesHash{seeddir} = "$propertiesHash{metaroot}/default/seed"; } 

if(defined($provider)) { $propertiesHash{providerid} = "$provider"; }
unless (defined($propertiesHash{providerid})) { $propertiesHash{providerid} = "NONE"; } 

if(defined($product)) { $propertiesHash{product} = "$product"; }
unless (defined($propertiesHash{product})) { $propertiesHash{product} = "NONE"; } 

if(defined($text)) { $propertiesHash{text} = "$text"; }
unless (defined($propertiesHash{text})) { $propertiesHash{text} = "NONE"; } 

if(defined($text001)) { $propertiesHash{text001} = "$text001"; }
unless (defined($propertiesHash{text001})) { $propertiesHash{text001} = "NONE"; } 

if(defined($text002)) { $propertiesHash{text002} = "$text002"; }
unless (defined($propertiesHash{text002})) { $propertiesHash{text002} = "NONE"; } 

if(defined($text003)) { $propertiesHash{text003} = "$text003"; }
unless (defined($propertiesHash{text003})) { $propertiesHash{text003} = "NONE"; } 

if(defined($batch)) { $propertiesHash{batch} = "$batch"; }
unless (defined($propertiesHash{batch})) { $propertiesHash{batch} = "001"; } 

if(defined($value)) { $propertiesHash{value} = "$value"; }
unless (defined($propertiesHash{value})) { $propertiesHash{value} = "NONE"; } 

if(defined($lock)) { $propertiesHash{lock} = $lock; }
unless (defined($propertiesHash{lock})) { $propertiesHash{lock} = "0"; } 

unless(defined($name)) {
    # want to get the directory immediately above file name to use as a meaningful default for name.
    my($tmpFile, $tmpDir) = fileparse($propertiesHash{content});
    #@tmp = split('/', $tmpDir);
    $name = $tmpFile;
}

my $id         = "CID$propertiesHash{batch}";


if($propertiesHash{type} =~ /enc/i) { $propertiesHash{type} = "encrypt"; $id  = "EID$propertiesHash{batch}";}

# Just want a unique seed for clear/encrypted - Ignore the ID with name appended.
$seedId  = $id;

# Add name AFTER we haveset the seedId value.
if (defined($name)) {
   $id = "$id-$name";
}

if(defined($template)) { $propertiesHash{template} = "$template"; }
unless (defined($propertiesHash{template})) {
    $propertiesHash{template} = "TEMPLATE.adi.V1.1.clear.xml"; 
} 

if(defined($token)) { $propertiesHash{token} = "$token"; }
unless (defined($propertiesHash{token})) {
    $propertiesHash{token} = "metadata.adi.token"; 
} 

if ( defined($display)) { displayPropertyHash( \%propertiesHash ); print "\n"; $display = $display; exit; }

unless( -f "$propertiesHash{content}/movie.ts" )   { die "ERROR: No content movie file found: [$propertiesHash{content}/movie.ts] $!\n"; }

unless( -f "$propertiesHash{meta}/$propertiesHash{template}" )   { die "ERROR: No metadata file found: [$propertiesHash{meta}/$propertiesHash{template}] $!\n"; }


    
@tokenFileList = split(/,/ , $propertiesHash{token});  
foreach $element (@tokenFileList) {
    unless( -f "$propertiesHash{meta}/$element" ) { die "ERROR: No token file found: [$propertiesHash{meta}/$element] $!\n"; }
}

print "\n\nusing input parameters:\n\n";

# scripts not in properties hash, used to locate properties file.

printf "\t%-25s -- [%s]\n", "scripts", $scripts;
printf "\t%-25s -- [%s]\n", "id", $id;
print "\n";

displayPropertyHash( \%propertiesHash );

unless( -d "$propertiesHash{out}" ) { system("mkdir -p $propertiesHash{out}"); }

# Keep track of seed in a file, so not have to worry what the previous value was to create new, unique, content.

unless(defined($seed)) {
    if ( -f  "$propertiesHash{metaroot}/default/seed/adigen.${seedId}.seed" ) {
        if ($lock) { 
            if ( -f  "$propertiesHash{metaroot}/default/seed/adigen.${seedId}.seed.LOCKED" ) {
                print "FATAL: The seed file is locked!\n\n";
                print "       If someone else is running adigen please wait, otherwise remove the file.\n\n";
                print "       lock file [ $propertiesHash{metaroot}/default/seed/adigen.${seedId}.seed.LOCKED ]\n";
                exit;
            }
            `touch $propertiesHash{metaroot}/default/seed/adigen.${seedId}.seed.LOCKED`;
        }
        $seed = `head $propertiesHash{seeddir}/adigen.${seedId}.seed`;
        chomp($seed);
        $seed++;
    } else {
        $seed = 1;
    }  
    print "\n";    
    printf "\t%-25s -- [%s]\n", "seed", $seed;
    print "\n\n";
} 

for($count = $seed; $count < ($seed + $propertiesHash{limit}); $count++ ) {

    # Store the last value of count, use it for our default seed next time.
    unless( -d "$propertiesHash{meta}/seed" ) { system("mkdir $propertiesHash{meta}/seed"); } 
    system("echo $count > $propertiesHash{meta}/seed/adigen.${seedId}.seed");
    
    substituteTokens();
    generatePackage();
    endMessage();
     
    if ( $propertiesHash{wait} > 0 ) {
        print "\n";
        print "Sleeping for [$propertiesHash{wait}] seconds........\n";
        sleep($propertiesHash{wait});
        print "\n";
    }
}


    
    if (($lock) && ( -f "$propertiesHash{metaroot}/default/seed/adigen.${seedId}.seed.LOCKED" )) {
        unlink("$propertiesHash{metaroot}/default/seed/adigen.${seedId}.seed.LOCKED");
    }

    
    
exit;


#########################################################################
# substituteTokens
#
# Perform some advanced TOKEN pre-substitution BEFORE we call subToken.pl
#########################################################################

# Split main code into three subs to handle multiple package generation.
# All done in a bit of a hurry, so not parameterized.........when I get more time :(
#       so watch out, global variables, yuk!

sub substituteTokens {

    my ($tmpToken, $tmpValue, $element) = "";
    my @valueList = ();
    my @tokenFileList = split(/,/ , $propertiesHash{token}); 
    
    print "\npreparing....\n\n";
    unless( -d "$propertiesHash{tmp}" ) { system("mkdir -p $propertiesHash{tmp}"); }
    if( -d "$propertiesHash{tmp}" ) { system("rm $propertiesHash{tmp}/* > /dev/null 2>&1"); }

    # Make a copy of our ADI TOKEN file, so we can modify checksums/filesizes to match current content if required.
     
    foreach $element (@tokenFileList) {
        system("cp -v $propertiesHash{meta}/$element $propertiesHash{tmp}/$element");
    }
    
    foreach $element (@tokenFileList) {
        getCheckSum("$propertiesHash{tmp}/$element");
    }
    
    # Lets update the standard NAME-TOKEN to be a bit more meaningful.
    setId("$propertiesHash{token}", "$propertiesHash{tmp}", "NAME-TOKEN", $id);
    
    # Update @PROVIDER-ID@, if specified as an input parameter.
    unless( $propertiesHash{providerid} =~ /NONE/ ) {
        setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "PROVIDER-ID", $propertiesHash{providerid});
    }

    # Update @PROVIDER-NAME@ with same value as @PROVIDER-ID@, if specified as an input parameter.
    unless( $propertiesHash{providerid} =~ /NONE/ ) {
        setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "PROVIDER-NAME", $propertiesHash{providerid});
    }
    
    # Update @PRODUCT-TYPE@, if specified as an input parameter.
    unless( $propertiesHash{product} =~ /NONE/ ) {
         setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "PRODUCT-TYPE", $propertiesHash{product});
    }

    # Update @TEXT@, if specified as an input parameter.
    unless( $propertiesHash{text} =~ /NONE/ ) {
         setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "\@TEXT\@", $propertiesHash{text});
    }
    
    # Update @TEXT-001@, if specified as an input parameter.
    unless( $propertiesHash{text001} =~ /NONE/ ) {
         setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "\@TEXT-001\@", $propertiesHash{text001});
    }
    
    # Update @TEXT-002@, if specified as an input parameter.
    unless( $propertiesHash{text002} =~ /NONE/ ) {
         setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "\@TEXT-002\@", $propertiesHash{text002});
    }
    
    # Update @TEXT-003@, if specified as an input parameter.
    unless( $propertiesHash{text003} =~ /NONE/ ) {
         setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "\@TEXT-003\@", $propertiesHash{text003});
    }

    # Update @BATCH-ID@, if specified as an input parameter.
    unless( $propertiesHash{batch} =~ /NONE/ ) {
         setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "\@BATCH-ID\@", $propertiesHash{batch});
    }
    
    # Update any other tokens, if specified as an input parameter using value.
    unless( $propertiesHash{value} =~ /NONE/ ) {
        @valueList = split(/,/ , $propertiesHash{value}); 
        foreach $element (@valueList) {
            ( $tmpToken, $tmpValue ) = $element =~ /^(.*)=(.*)$/;
             setToken("$propertiesHash{token}", "$propertiesHash{tmp}", "$tmpToken", "$tmpValue");
        }
    }
    
    print "substitute tokens....\n\n";
    $paddedCount = sprintf("%0${numberDigits}d" , $count);
    # print "$paddedCount\n";
    
    # To handle multiple token files, need to copy the template into the tmp directory.
    
    system("cp -v $propertiesHash{meta}/$propertiesHash{template} $propertiesHash{tmp}/$propertiesHash{metafile}");
    foreach $element (@tokenFileList) {
        $cmd = "$propertiesHash{tools}/subToken.pl -aline='$propertiesHash{tmp}/$propertiesHash{metafile},$propertiesHash{tmp}/$propertiesHash{metafile}' -token=$propertiesHash{tmp}/$element -seed=${count}";
        print "\n";
        print "Invoking subToken.pl ...\n\n";
        print "command: [$cmd]\n\n";
        system("$cmd");
    }
}

#########################################################################
# generatePackage 
#########################################################################

# Split main code into three subs to handle multiple package generation.
# All done in a bit of a hurry, so not parameterized.........when I get more time :(
#       so watch out, global variables, yuk!

sub generatePackage {

  my @tokenFileList = split(/,/ , $propertiesHash{token}); 
  
  print "\ngenerate temporary files....\n\n";
  if (-f "$propertiesHash{content}/movie.ts") {
      system("cp -v $propertiesHash{content}/movie.ts $propertiesHash{tmp}/${id}-${paddedCount}_movie.ts");
  }
  if (-f "$propertiesHash{content}/preview.ts") {  
      system("cp -v $propertiesHash{content}/preview.ts $propertiesHash{tmp}/${id}-${paddedCount}_preview.ts");
  }
  if (-f "$propertiesHash{content}/boxcover.jpg") { 
      system("cp -v $propertiesHash{content}/boxcover.jpg $propertiesHash{tmp}/${id}-${paddedCount}_boxcover.jpg");
  }
  if (-f "$propertiesHash{content}/poster.jpg") { 
      system("cp -v $propertiesHash{content}/poster.jpg $propertiesHash{tmp}/${id}-${paddedCount}_poster.jpg");
  }
  
  if($propertiesHash{type} =~ /encrypt/i) { 
      if (-f "$propertiesHash{content}/encrypted.ts") { 
          system("cp -v $propertiesHash{content}/encrypted.ts $propertiesHash{tmp}/${id}-${paddedCount}_encrypted.ts"); 
      }      
  }
  
  if(-f "$propertiesHash{content}/ADI.DTD") { 
      system("cp -v $propertiesHash{content}/ADI.DTD $propertiesHash{tmp}/ADI.DTD"); 
  }

  system("chown nobody:nobody $propertiesHash{tmp}/*");
  
  # No need for the distinct token file, don't want it in our ADI package.
  foreach $element (@tokenFileList) {
      system(" rm -v $propertiesHash{tmp}/$element");
  }

  print "\ncreate package....\n\n";
  $cmd = "cd $propertiesHash{tmp}; tar -cvf ${id}-${paddedCount}.tar *; mv *.tar $propertiesHash{out}/";
  print "$cmd\n";
  #system("cd $propertiesHash{tmp}; tar -cvf ${id}-${paddedCount}.tar *; mv *.tar $propertiesHash{out}/");
  system("$cmd");
  
  $packageId = getPackageId("$propertiesHash{tmp}/$propertiesHash{metafile}", "${paddedCount}");
  if($propertiesHash{unpack} =~ /yes/i) {
      system("mkdir $propertiesHash{out}/$packageId");
      system("cp -v $propertiesHash{tmp}/* $propertiesHash{out}/$packageId");
      system("chown -R nobody:nobody $propertiesHash{out}");
  }

  print "\ntidy....\n\n"; 
  system("rm -v $propertiesHash{tmp}/*");
  system("rmdir -v $propertiesHash{tmp}");
  
  if($propertiesHash{ingest} =~ /yes/i) { 
      print "\n\nThe new package [$propertiesHash{out}/${id}-${paddedCount}.tar] will now be copied into the watchfolder and ingested...\n\n";
      system("cp -v $propertiesHash{out}/${id}-${paddedCount}.tar $propertiesHash{ingestdir}/; chown nobody:nobody $propertiesHash{ingestdir}/${id}-${paddedCount}.tar");
      print "\n";
  }
 
# my $thisDir = cwd();

# pbuilder stuff
#export pbscriptdir="$scrdir/pbuilder/scripts"
#alias ncdpb='cd $pbscriptdir'
#alias pbexample='echo "> cdpb; ./PackageExporter file PACC0011010000000036 /test/aditar/PACC0011010000000036"'
# $pbuilderDir/PackageExporter
# function pbexport () { tmpMyDirectory=`pwd`; cd $pbscriptdir; ./PackageExporter file "$1" "$2"; cd "$tmpMyDirectory"; }
#
  if(($propertiesHash{export} =~ /yes/i) && ($propertiesHash{unpack} =~ /yes/)) { 
      $cmd = "cd $pbuilderDir;  ./PackageExporter file $packageId $propertiesHash{out}/$packageId; cd  $thisDir";
      print "\n\nThe new package [$propertiesHash{out}/$packageId] will now be exported as an ADI package and ingested...\n\n";
      print "command: $cmd\n";
      system("$cmd");
      print "\n";
  }

}

#########################################################################
# endMessage
#########################################################################

# Split main code into three subs to handle multiple package generation.
# All done in a bit of a hurry, so not parameterized.........when I get more time :(
#       so watch out, global variables, yuk!

sub endMessage {


    print "\n";
    print "created package: $packageId\n";
    print "location:        $propertiesHash{out}/${id}-${paddedCount}.tar\n";
    if(($propertiesHash{unpack} =~ /yes/) && ($propertiesHash{export} !~ /yes/i)) { 
        print "location:        $propertiesHash{out}/$packageId\n"; 
        print "\n";
        print "example export command:\n\n";
        print " > pbexport $packageId $propertiesHash{out}/$packageId\n";
        print "\n";
    }
    
    if($propertiesHash{unpack} !~ /yes/)  {
        print "\nnote: to generate a package that can be passed to pbexport use the parameter \"adigen.pl -unpack=yes\"\n\n";
    }

}
###################################################################################
#  subroutine: setId                                               
###################################################################################

sub setId {
    my $line  = $_[0];
    my $dir   = $_[1];
    my $token = $_[2];
    my $id    = $_[3];

    my @tokenFileList = ();
    
    @tokenFileList = split(/,/ , $line);  
    foreach $file (@tokenFileList) {
        print "setID in token file [$dir/$file]...\n\n";
        # @NAME-TOKEN@=CID01-@COUNT_NNNN@
        $cmd = "sed -i \'s/^\@${token}\@=.*/\@${token}\@=${id}-\@COUNT_NNNN\@/\' $dir/$file";
        system("$cmd");
    }
}

###################################################################################
#  subroutine: OLDsetToken                                              
###################################################################################

sub OLDsetToken {
    my $line  = $_[0];
    my $dir   = $_[1];
    my $token = $_[2];
    my $value = $_[3];
    
    # @PROVIDER-ID@
    # @PROVIDER-NAME@
    #
    my @tokenFileList = ();
    
    @tokenFileList = split(/,/ , $line);  
    foreach $file (@tokenFileList) {
        print "setToken [\@$token\@] in token file [$dir/$file]...\n\n";    
        $cmd = "sed -i \'s/^\@${token}\@=.*/\@${token}\@=${value}/\' $dir/$file";
        system("$cmd");
    }
}

###################################################################################
#  subroutine: setToken                                              
###################################################################################

sub setToken {
    my $line  = $_[0];
    my $dir   = $_[1];
    my $token = $_[2];
    my $value = $_[3];
    
    # @PROVIDER-ID@
    # @PROVIDER-NAME@
    #
    my @tokenFileList = ();
    
    @tokenFileList = split(/,/ , $line);  
    foreach $file (@tokenFileList) {
        print "setToken [$token] in token file [$dir/$file]...\n\n";    
        $cmd = "sed -i \'s/^${token}=.*/${token}=${value}/\' $dir/$file";
        system("$cmd");
    }
}

###################################################################################
#  subroutine: getCheckSum                                                
###################################################################################

sub getCheckSum {
    my $file = $_[0];
    
    #$hashValue = $$tokenHashRef{$key};
    my %contentHash;
    my @tmp;
    my $cmd;
    
    print "\nupdating content file sizes and md5sums...\n\n";
    
     if ( -f "$propertiesHash{content}/movie.ts" ) { 
         $checkSum = `md5sum $propertiesHash{content}/movie.ts`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{movieClearCheckSum} = $tmp[0];
         $contentHash{movieClearSize} = stat("$propertiesHash{content}/movie.ts")->size;

         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-CHECKSUM-TOKEN\@=.*/\@MOVIE-CONTENT-CHECKSUM-TOKEN\@=$contentHash{movieClearCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-FILE-SIZE-TOKEN\@=.*/\@MOVIE-CONTENT-FILE-SIZE-TOKEN\@=$contentHash{movieClearSize}/\' $file";
         system("$cmd");
     }
         
     if ( -f "$propertiesHash{content}/preview.ts" ) { 
         $checkSum = `md5sum $propertiesHash{content}/preview.ts`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{previewClearCheckSum} = $tmp[0];
         $contentHash{previewClearSize} = stat("$propertiesHash{content}/preview.ts")->size;
         
         $cmd = "sed -i \'s/^\@PREVIEW-CONTENT-CHECKSUM-TOKEN\@=.*/\@PREVIEW-CONTENT-CHECKSUM-TOKEN\@=$contentHash{previewClearCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@PREVIEW-CONTENT-FILE-SIZE-TOKEN\@=.*/\@PREVIEW-CONTENT-FILE-SIZE-TOKEN\@=$contentHash{previewClearSize}/\' $file";
         system("$cmd");  
     }

     if ( -f "$propertiesHash{content}/boxcover.jpg" ) { 
         $checkSum = `md5sum $propertiesHash{content}/boxcover.jpg`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{boxcoverCheckSum} = $tmp[0];
         $contentHash{boxcoverSize} = stat("$propertiesHash{content}/boxcover.jpg")->size;
         
         $cmd = "sed -i \'s/^\@BOXCOVER-CONTENT-CHECKSUM-TOKEN\@=.*/\@BOXCOVER-CONTENT-CHECKSUM-TOKEN\@=$contentHash{boxcoverCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@BOXCOVER-CONTENT-FILE-SIZE-TOKEN\@=.*/\@BOXCOVER-CONTENT-FILE-SIZE-TOKEN\@=$contentHash{boxcoverSize}/\' $file";
         system("$cmd");
     }

     if ( -f "$propertiesHash{content}/poster.jpg" ) { 
         $checkSum = `md5sum $propertiesHash{content}/poster.jpg`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{posterCheckSum} = $tmp[0];
         $contentHash{posterSize} = stat("$propertiesHash{content}/poster.jpg")->size;
         
         $cmd = "sed -i \'s/^\@POSTER-CONTENT-CHECKSUM-TOKEN\@=.*/\@POSTER-CONTENT-CHECKSUM-TOKEN\@=$contentHash{posterCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@POSTER-CONTENT-FILE-SIZE-TOKEN\@=.*/\@POSTER-CONTENT-FILE-SIZE-TOKEN\@=$contentHash{posterSize}/\' $file";
         system("$cmd");
     }

    if ( -f "$propertiesHash{content}/encrypted.ts" ) { 
         $checkSum = `md5sum $propertiesHash{content}/encrypted.ts`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{movieEncryptedCheckSum} = $tmp[0];
         $contentHash{movieEncryptedSize} = stat("$propertiesHash{content}/encrypted.ts")->size;
         
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-ENCRYPTED-CHECKSUM-TOKEN\@=.*/\@MOVIE-CONTENT-ENCRYPTED-CHECKSUM-TOKEN\@=$contentHash{movieEncryptedCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-ENCRYPTED-FILE-SIZE-TOKEN\@=.*/\@MOVIE-CONTENT-ENCRYPTED-FILE-SIZE-TOKEN\@=$contentHash{movieEncryptedSize}/\' $file";
         system("$cmd");
     }
}

###################################################################################
#  subroutine: getPackageId                                                
###################################################################################

sub getPackageId {
    my $file = $_[0];
    my $id   = $_[1];
    my $line = "";
    my $cmd  = "";

    # <Title uriId="ericsson.com/Title/OFFP0007010000000004"
    # <AMS Asset_Class="package" Asset_ID="PACC0011010000000001"
    # <EditorialInformation opusCode="@PACKAGE-REF01-ID-TOKEN@" opusSource="VOD">
    my $adiV101Pattern = "<AMS Asset_Class=\"package\" Asset_ID=\"(.*?)\"";
    my $adiV300Pattern = "<Title uriId=.*/.*/(.*?)\"";
    my $orangeCmsPattern = "<EditorialInformation opusCode=\"(.*?)\" +";
    
    #$cmd = "egrep '$adiV101Pattern|$adiV300Pattern' $file > /dev/null";
    $cmd = "egrep '$adiV101Pattern|$adiV300Pattern|$orangeCmsPattern' $file";
    
    $line = `$cmd`;
    print "\n\negrep: $line\n\n";
    
    if ($line =~ /$adiV101Pattern/) {
        # print "match ADI V1.1 - $line\n";
        ($id) = $line =~ /$adiV101Pattern/;
    }
    if ($line =~ /$adiV300Pattern/ ) {
        # print "match ADI V3.0 - $line\n";
        ($id) = $line =~ /$adiV300Pattern/;
    }   
    
    if ($line =~ /$orangeCmsPattern/ ) {
        # print "match mpCmsWonVod - $line\n";
        ($id) = $line =~ /$orangeCmsPattern/;
    }  
    # print "ID VALUE: $id\n";
    return $id;
}   
#            if (/Name=\"Content_FileSize\" Value=\"(.*?)\"/) {
#            ($contentFileSize) = /Name=\"Content_FileSize\" Value=\"(.*?)\"/;
#            print "contenttFileSize: $contentFileSize\n";
            
###################################################################################
#  subroutine: returnPropertyHash                                                 
###################################################################################

sub returnPropertyHash  {
    my $propertiesInputFile = $_[0];
    my $propertiesHashRef   = $_[1];
    
    
    my ($line, $propertyName, $propertyValue) = "";
    my @propArray = ();
    
    
    unless (open (FILE2, "<$propertiesInputFile"))   { die "cannot open properties file $propertiesInputFile $!"; }
    @propArray   = <FILE2>;
    close FILE2;
    
    foreach $line (@propArray) {
        #chomp ($line);
        if ( $line =~ /^#/ )    { next; }
        if ( $line =~ /^\s*$/ ) { next; }
        if ( $line =~ /^.*=/i ) {
             ( $propertyName, $propertyValue ) = $line =~ /^(.*)\s*=\s*(.*)$/; 
             chomp($propertyValue);
             $$propertiesHashRef{$propertyName} = $propertyValue;
             #print "name [$propertyName] value [$propertyValue]\n";
             #print "hash [$$propertiesHashRef{$propertyName}]\n";
        }
    }
    return;
}

###################################################################################
#  subroutine: display properties hash
###################################################################################

sub displayPropertyHash  {
    my $propertiesHashRef   = $_[0];
    my @keys = ();
    
    my ($element, $msgTxt) = "";
    
        @keys = sort(keys(%$propertiesHashRef));

        foreach $element (@keys) {           
            $msgTxt = sprintf "\t%-25s -> %-50s\n", $element, $$propertiesHashRef{$element};
            print "$msgTxt";
        }
}
    
    
###################################################################################
#  subroutine: help
###################################################################################

sub help {

$helpText1 = <<ENDHELPPAGE1;

  This wrapper script that calls the TOKEN substitution script 'subToken.pl' to generate test ADI packages.
  
  When installed as part of the module 'scac-tools' all default values should be valid, however, alternative locations of:
  
         ADI templates  - [default location] /opt/tandbergtv/cms/scripts/adi/meta/default
         ADI tokens     - [default location] /opt/tandbergtv/cms/scripts/adi/meta/default
         content files  - [default location] /opt/tandbergtv/cms/scripts/adi/content/default
         
  Can be used to override the defaults.
  
  By default, the following directory needs to be filled with appropriate content.
  The following file names MUST be used: 
  
         ls -1 /opt/tandbergtv/cms/scripts/adi/content
         boxcover.jpg
         encrypted.ts
         movie.ts
         preview.ts

  The script will automatically calculate the correct file sizes and MD5 checksums for the provided content.
  
  The same content files will then be copied and renamed for each package.
  
  An alternative content location can be specified using '-content=/nnn'
  
  Usage:
  
     > ./ adiGen.pl
     
  By default, this will create ONE unencrypted package with a base ID of '0001' in the sub directory 'aditar'.
  If the subdirectory 'tar' does not exist it will be created.
  
     # tar -tvf aditar/CID01-0001.tar
     -rw-r--r-- nobody/nobody 15406 2011-11-23 11:45:46 ADI.XML
     -rw-r--r-- nobody/nobody     0 2011-11-23 11:45:46 CID01-0001_boxcover.jpg
     -rw-r--r-- nobody/nobody     0 2011-11-23 11:45:46 CID01-0001_movie.ts
     -rw-r--r-- nobody/nobody     0 2011-11-23 11:45:46 CID01-0001_preview.ts

  By default, identifiers used for unique content and package name generation use the following types of token.

      \@PACKAGE-REF01-ID-TOKEN\@=PACC001\@BATCH-ID\@\@COUNT_NNNNNNNNNN\@
      \@TITLE-REF01-ID-TOKEN\@=TITC002\@BATCH-ID\@\@COUNT_NNNNNNNNNN\@
      \@MOVIE-REF01-ID-TOKEN\@=MOVC001\@BATCH-ID\@\@COUNT_NNNNNNNNNN\@
      
      \@COUNT_NNNNNNNNNN\@ - Generates a unique numeric reference based on the current value of seed.
      \@BATCH-ID\@         - Generates a default value of 001, but can be over ridden if required.

  To specify an explicit metadata token mapping file to be applied to the template, use the command:
  
      > ./adigen.pl -token=my.metadata.token  [default token file] metadata.adi.token
      
  The 'token' input parameter can be a list of token files. 
  So if most of the required values are defined in 'metadata.adi.token' and only a few new values need to be defined,
  or existing values changed, create a separate token file with ONLY the required values, and specify this first in the list.
  
  For example:
  
      > ./adigen.pl -token="metadata.adi.envivio.token,metadata.adi.token"
      
  For more information on ADI generation see:
  
     > ./subToken.pl -help
     
  Function:

  Wrapper script that calls the TOKEN substitution script 'subToken.pl' to generate test ADI packages.

      -help       : print this help text and exit.

      -display    : print current input parameters and exit.

      ------------------------------------------------------------------

      -batch      : value   [nnn]
                    The three digit numeric part of the ID. 
                    Typically this will always be '001' but can be selected if required.
                    Useful to identify specific sets of test data.
                    
                        Example: -batch=009 -type=clear  -> CID009
                        
                    If defined, will also change the token '\@BATCH-ID\@'.
                    Used to generate the 4th to 6th digits in the package / content unique identifiers.
                    
                        Example: MOVC001[001]0000000001
                    
                    default [001]    
                                                   
      -content    : Directory where the example test content is located.
                    default [default]                    

                    So the default location will be [contentroot/content]:
                    /opt/tandbergtv/cms/scripts/adi/content/default
                    
                    For ease of use 'content' can contain the full path (if matches ^/)
                    in which case 'contentroot' is ignored.

      -contentroot: Default path where the example test content directory is located.
                    default [/opt/tandbergtv/cms/scripts/adi/content]

                    default [01]
      -ingest     : value   [yes|no]
                    Automatically copy the new package into the watchfolder for instant ingestion?
                    default [no]     

      -ingestdir  : value   [/path]
                    Directory path of the CMS watchfolder.
                    default [/content/ingest/telenor]  
                    
      -limit      : value [integer]
                    The number of packages to generate.
                    default [1]

      -metaroot   : Default path where the meta data directory is located.
                    default [/opt/tandbergtv/cms/scripts/adi/meta]
                   
      -meta       : Directory where the ADI templates and token files are located.
                    default [default]
                    
                    So the default location will be [metaroot/meta]:
                    /opt/tandbergtv/cms/scripts/adi/meta/default
                    
                    For ease of use 'meta' can contain the full path (if matches ^/) 
                    in which case 'metaroot' is ignored.

      -metafile   : value   [META DATA FILE NAME]
                    Typically this will be ADI.XML, but can be anything.
                    Allow user to specify their own meta data file name.
                    default [ADI.XML]     
                    
      -name       : A meaningful name that will be added to the generated ID to identify the package.
                    For example:
                       -name=burlesque -seed=52 -type=clear  
                    generates:
                        CID01-burlesque-0052.tar
                        CID01-burlesque-0052_boxcover.jpg
                        CID01-burlesque-0052_movie.ts
                        CID01-burlesque-0052_preview.ts
                    default [parameter -content]   

      -out        : Directory where the output tar files will be created.
                    By default a 'tar' sundirectory is created in the current directory.
                    default [aditar]
                    
      -provider   : value   [PROVIDER-ID]
                    Quick way to change 'provider Id', over writes value in the token file.
                    'provider name' will also be set to equal 'provider Id'.
                    Example values "sf-anytime.com, sony.com, ericsson.com"
                    
                    NONE - No action is taken, value from token mapping file will be used.
                    default [NONE]     

      -product    : value   [PRODUCT-TYPE]
                    Quick way to change 'product type', over writes value in the token file.
                    Example values "MOD, SVOD"
                    
                    NONE - No action is taken, value from token mapping file will be used.
                    default [NONE]                    

      -seed       : value [integer]
                    The starting value of all package identifiers.
                    default - 
                        The script looks for the file:
                            \${meta}/adigen.ID.seed
                        If the file exists: 
                            The value is read from the file and incremented. 
                            The updated value is written back to the file.
                            This enables unique ID's to be generated per unique ID each time the script is run.
                        Else
                            default [1]
                                                
      -type       : value [clear|encrypt]
                    Generate encrypted test content or clear, unencrypted content.
                    Note: NO ENCRYPTION is performed, will simply use whatever files are provided in 'content'
                    with the encryption section of the ADI.XML
                    default [clear]

      -tools      : Directory where dependent scripts 'subToken.pl' are located.
                    default [/opt/tandbergtv/cms/scripts/tools]

      -template   : Template file used to generate 'ADI.XML'.
                    default [TEMPLATE.adi.V1.1.clear.xml ]

      -token      : Token file used to generate 'ADI.XML'.
                    Either a single file ( typical usage ) or a list of token files can be provided as 
                    a comma separated list:
                    
                        example: -token="file01.token"
                        example: -token="file01.token,file02.token,file03.token"
                        
                    default [metadata.adi.token]                    

      -text001    : value   [text_string]
      -text002    : value   [text_string]
      -text003    : value   [text_string]
      
                    If the token \@TEXT-001\@ ( 002, 003 ) is contained in the template, its value can be set from 
                    the command line.
                    Provides and easy way to add unique, descriptive, test message to description fields.
                    
                    NONE - No action is taken, value from token mapping file will be used.
                    default [NONE]   
                    
      -unpack     : value   [yes|no]
                    Create a directory containing the unpacked files in addition to the tar file.
                    The directory name will be based on the package ID.
                    default [no]     
                    
ENDHELPPAGE1

$continueMsg = "Enter any key to continue\n";

print $helpText1, $continueMsg;
$continue=<STDIN>; $continue = $continue;
}

# END
