#!/usr/bin/perl -sw
#
# Created by Steve Voisey (srv) 06 Jun 2011.
# email: [steve.voisey@ericsson.com]
#
# file:    metaGen.pl
# version: V1.025
#
# Author  Date      Version Description
# srv     24/jan/12 V1.10  Renamed from generateAdi.pl to metaGen.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.
# srv     24/may/12 V1.19  Rename from 'adiGen.pl' to 'metaGen.pl' as not adi specific ( ie Orange ). 
# srv     31/may/12 V1.20  Add delete functionality with auto ingest / export. Make unpack=yes when export=yes.
# srv     02/jul/12 V1.21  Fix bug updating provider and product from command line, failed to pass @ in token name.
# srv     30/aug/12 V1.22  Add logging and help text for Value.
# srv     26/sep/12 V1.23  Check for asset file size exceeding max value.
# srv     26/sep/12 V1.24  Drop encrypted input type and C from CID.
# srv     27/sep/12 V1.25  Add input parameter encrypt to support pgp and gpg encryption.

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

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 = ();

my @packageDirList = ();
my @packageTarList  = ();

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


if (defined($help))        { help(); exit;}

##############################################################
# Log all messages from now on.
##############################################################

if(defined($log))      { $logFile = $log; } 
unless(defined($log))  { $logFile = "metagen.log"; $log = "UNDEFINED"; } 

if ( $logFile =~ /date|name/i ) { $logFile = myDate() . "." . myTimeUnformatted() . "." . "metagen.log"; }

unless (open (LOG, ">$logFile"))  { die "cannot open log file [$logFile] $!"; }


unless(defined($scripts))  { $scripts = "/opt/tandbergtv/cms/scripts"; }
unless (defined($prop))    { $prop = "$scripts/conf/metaGen.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/metatar"; } 

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}") { 
        myMsg("\nERROR: Unable to perform auto ingest\n");
        myMsg("       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 ) {
    # can not export unless the package is unpacked.
    $propertiesHash{unpack} = "yes";
    
    unless( -f "$pbuilderDir/PackageExporter") { 
        myMsg("\nERROR: Unable to perform auto export\n");
        myMsg("       Tool pbuilder is not installed and configured under [$pbuilderDir/PackageExporter]\n");
        exit;
    }    
}

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

if(defined($delete)) { $propertiesHash{delete} = "$delete"; }
unless (defined($propertiesHash{delete})) { $propertiesHash{delete} = "prompt"; } 

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} = "01"; } 

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"; } 

if(defined($maxfilesize)) { $propertiesHash{maxfilesize} = $maxfilesize; }
unless (defined($propertiesHash{maxfilesize})) { $propertiesHash{maxfilesize} = "100"; } 

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

if(defined($sourceMovie)) { $propertiesHash{sourceMovie} = $sourceMovie; }
unless (defined($propertiesHash{sourceMovie})) { $propertiesHash{sourceMovie} = "movie.ts"; }

if(defined($sourcePreview)) { $propertiesHash{sourcePreview} = $sourcePreview; }
unless (defined($propertiesHash{sourcePreview})) { $propertiesHash{sourcePreview} = "preview.ts"; }

if(defined($sourcePoster)) { $propertiesHash{sourcePoster} = $sourcePoster; }
unless (defined($propertiesHash{sourcePoster})) { $propertiesHash{sourcePoster} = "poster.jpg"; }

if(defined($sourceBoxcover)) { $propertiesHash{sourceBoxcover} = $sourceBoxcover; }
unless (defined($propertiesHash{sourceBoxcover})) { $propertiesHash{sourceBoxcover} = "boxcover.jpg"; }

if(defined($sourceGpg)) { $propertiesHash{sourceGpg} = $sourceGpg; }
unless (defined($propertiesHash{sourceGpg})) { $propertiesHash{sourceGpg} = "$propertiesHash{sourceMovie}.gpg"; }

if(defined($sourcePgp)) { $propertiesHash{sourcePgp} = $sourcePgp; }
unless (defined($propertiesHash{sourcePgp})) { $propertiesHash{sourcePgp} = "$propertiesHash{sourceMovie}.pgp"; }

if(defined($destinationMovie)) { $propertiesHash{destinationMovie} = $destinationMovie; }
unless (defined($propertiesHash{destinationMovie})) { $propertiesHash{destinationMovie} = "movie.ts"; }

if(defined($destinationPreview)) { $propertiesHash{destinationPreview} = $destinationPreview; }
unless (defined($propertiesHash{destinationPreview})) { $propertiesHash{destinationPreview} = "preview.ts"; }

if(defined($destinationPoster)) { $propertiesHash{destinationPoster} = $destinationPoster; }
unless (defined($propertiesHash{destinationPoster})) { $propertiesHash{destinationPoster} = "poster.jpg"; }

if(defined($destinationBoxcover)) { $propertiesHash{destinationBoxcover} = $destinationBoxcover; }
unless (defined($propertiesHash{destinationBoxcover})) { $propertiesHash{destinationBoxcover} = "boxcover.jpg"; }

if(defined($destinationGpg)) { $propertiesHash{destinationGpg} = $destinationGpg; }
unless (defined($propertiesHash{destinationGpg})) { $propertiesHash{destinationGpg} = "$propertiesHash{destinationMovie}.gpg"; }

if(defined($destinationPgp)) { $propertiesHash{destinationPgp} = $destinationPgp; }
unless (defined($propertiesHash{destinationPgp})) { $propertiesHash{destinationPgp} = "$propertiesHash{destinationMovie}.pgp"; }

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         = "ID$propertiesHash{batch}";

# Drop encrypt type, not useful and confusing.
### 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.xml"; 
} 

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

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

### disable for now.
### unless( -f "$propertiesHash{content}/$propertiesHash{sourceMovie}" )   { die "ERROR: No content movie file found: [$propertiesHa###sh{content}/$propertiesHash{sourceMovie}] $!\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"; }
}

myMsg("\n\nusing input parameters:\n\n");

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

$msgTxt = sprintf("\t%-25s -- [%s]\n", "scripts", $scripts);
myMsg("$msgTxt");
$msgTxt = sprintf("\t%-25s -- [%s]\n", "id", $id);
myMsg("$msgTxt");

myMsg("\n");

displayPropertyHash( \%propertiesHash );

# Check file name of the longest possible asset "boxcover" against max file size.
# If asset name is too long, content will fail to ingest, so lets catch it now.
$padtmp = sprintf("%0${numberDigits}d" , 1);
if (length("${id}-${padtmp}_$propertiesHash{destinationBoxcover}") > $propertiesHash{maxfilesize}) {
     myMsg("\nFATAL: file length of asset\n");
     myMsg("       [ ${id}-${padtmp}_$propertiesHash{destinationBoxcover} ]\n");
     myMsg("       exceeds max file size [ $propertiesHash{maxfilesize} ]\n\n");
     exit
}

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/metaGen.${seedId}.seed" ) {
        if ($lock) { 
            if ( -f  "$propertiesHash{metaroot}/default/seed/metaGen.${seedId}.seed.LOCKED" ) {
                myMsg("FATAL: The seed file is locked!\n\n");
                myMsg("       If someone else is running metaGen please wait, otherwise remove the file.\n\n");
                myMsg("       lock file [ $propertiesHash{metaroot}/default/seed/metaGen.${seedId}.seed.LOCKED ]\n");
                exit;
            }
            `touch $propertiesHash{metaroot}/default/seed/metaGen.${seedId}.seed.LOCKED`;
        }
        $seed = `head $propertiesHash{seeddir}/metaGen.${seedId}.seed`;
        chomp($seed);
        $seed++;
    } else {
        $seed = 1;
    }  
    myMsg("\n"); 
    $msgTxt = sprintf("\t%-25s -- [%s]\n", "seed", $seed);
    myMsg("$msgTxt");
    myMsg("\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/metaGen.${seedId}.seed");
    
    substituteTokens();
    generatePackage();
    endMessage();
     
    if ( $propertiesHash{wait} > 0 ) {
        myMsg("\n");
        myMsg("Sleeping for [$propertiesHash{wait}] seconds........\n");
        sleep($propertiesHash{wait});
        myMsg("\n");
    }
}


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


if ((@packageDirList > 0) || (@packageTarList > 0)) { tidyOldFiles(\@packageDirList, \@packageTarList, \%propertiesHash); }

close LOG;
# Create a logfile unique to last package created.
if ( $log =~ /name/i ) { system("mv ${logFile}  ${id}-${paddedCount}.${logFile}"); }

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}); 
    
    myMsg("\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");
        }
    }
    
    myMsg("substitute tokens....\n\n");
    $paddedCount = sprintf("%0${numberDigits}d" , $count);
    # myMsg("$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}";
        myMsg("\n");
        myMsg("Invoking subToken.pl ...\n\n");
        myMsg("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}); 
  
  myMsg("\ngenerate temporary files....\n\n");
  if (-f "$propertiesHash{content}/$propertiesHash{sourceMovie}") {
      # For GPG/PGP we do NOT want to include the clear movie file.
      unless ( $propertiesHash{encrypt} =~ /gpg|pgp/i ) {
          system("cp -v $propertiesHash{content}/$propertiesHash{sourceMovie} $propertiesHash{tmp}/${id}-${paddedCount}_$propertiesHash{destinationMovie}");
      }
  }
  if (-f "$propertiesHash{content}/$propertiesHash{sourcePreview}") {  
      system("cp -v $propertiesHash{content}/$propertiesHash{sourcePreview} $propertiesHash{tmp}/${id}-${paddedCount}_$propertiesHash{destinationPreview}");
  }
  if (-f "$propertiesHash{content}/$propertiesHash{sourceBoxcover}") { 
      system("cp -v $propertiesHash{content}/$propertiesHash{sourceBoxcover} $propertiesHash{tmp}/${id}-${paddedCount}_$propertiesHash{destinationBoxcover}");
  }
  if (-f "$propertiesHash{content}/$propertiesHash{sourcePoster}") { 
      system("cp -v $propertiesHash{content}/$propertiesHash{sourcePoster} $propertiesHash{tmp}/${id}-${paddedCount}_$propertiesHash{destinationPoster}");
  }
  if (-f "$propertiesHash{content}/$propertiesHash{sourceGpg}") {
      system("cp -v $propertiesHash{content}/$propertiesHash{sourceGpg} $propertiesHash{tmp}/${id}-${paddedCount}_$propertiesHash{destinationGpg}");
  }
  if (-f "$propertiesHash{content}/$propertiesHash{sourcePgp}") {
      system("cp -v $propertiesHash{content}/$propertiesHash{sourcePgp} $propertiesHash{tmp}/${id}-${paddedCount}_$propertiesHash{destinationPgp}");
  }
  # Disable this type of encryption, not sure if we will ever use it.
  ###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");
  }

  myMsg("\ncreate package....\n\n");
  $cmd = "cd $propertiesHash{tmp}; tar -cvf ${id}-${paddedCount}.tar *; mv *.tar $propertiesHash{out}/";
  myMsg("$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}");
      push(@packageDirList, "$propertiesHash{out}/$packageId");
  }

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

# pbuilder stuff
#export pbscriptdir="$scrdir/pbuilder/scripts"
#alias ncdpb='cd $pbscriptdir'
#alias pbexample='echo "> cdpb; ./PackageExporter file PACC0011010000000036 /test/metatar/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";
      myMsg("\n\nThe new package [$propertiesHash{out}/$packageId] will now be exported as an ADI package and ingested...\n\n");
      myMsg("command: $cmd\n");
      system("$cmd");
      myMsg("\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 {


    myMsg("\n");
    myMsg("created package: $packageId\n");
    myMsg("location:        $propertiesHash{out}/${id}-${paddedCount}.tar\n");
    if(($propertiesHash{unpack} =~ /yes/) && ($propertiesHash{export} !~ /yes/i)) { 
        myMsg("location:        $propertiesHash{out}/$packageId\n"); 
        myMsg("\n");
        myMsg("example export command:\n\n");
        myMsg(" > pbexport $packageId $propertiesHash{out}/$packageId\n");
        myMsg("\n");
    }
    
    if($propertiesHash{unpack} !~ /yes/)  {
        myMsg("\nnote: to generate a package that can be passed to pbexport use the parameter \"metaGen.pl -unpack=yes\"\n\n");
    }
    myMsg("\n\texample command to log an ingest session:\n\t> tjlog | tee log/${id}-${paddedCount}.log\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) {
        myMsg("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) {
        myMsg("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) {
        myMsg("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;
    
    myMsg("\nupdating content file sizes and md5sums...\n\n");
    
     if ( -f "$propertiesHash{content}/$propertiesHash{sourceMovie}" ) { 
         $checkSum = `md5sum $propertiesHash{content}/$propertiesHash{sourceMovie}`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{movieClearCheckSum} = $tmp[0];
         $contentHash{movieClearSize} = stat("$propertiesHash{content}/$propertiesHash{sourceMovie}")->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}/$propertiesHash{sourcePreview}" ) { 
         $checkSum = `md5sum $propertiesHash{content}/$propertiesHash{sourcePreview}`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{previewClearCheckSum} = $tmp[0];
         $contentHash{previewClearSize} = stat("$propertiesHash{content}/$propertiesHash{sourcePreview}")->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}/$propertiesHash{sourceBoxcover}" ) { 
         $checkSum = `md5sum $propertiesHash{content}/$propertiesHash{sourceBoxcover}`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{boxcoverCheckSum} = $tmp[0];
         $contentHash{boxcoverSize} = stat("$propertiesHash{content}/$propertiesHash{sourceBoxcover}")->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}/$propertiesHash{sourcePoster}" ) { 
         $checkSum = `md5sum $propertiesHash{content}/$propertiesHash{sourcePoster}`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{posterCheckSum} = $tmp[0];
         $contentHash{posterSize} = stat("$propertiesHash{content}/$propertiesHash{sourcePoster}")->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}/movie.gpg" ) { 
         $checkSum = `md5sum $propertiesHash{content}/movie.gpg`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{movieGpgCheckSum} = $tmp[0];
         $contentHash{movieGpgSize} = stat("$propertiesHash{content}/movie.gpg")->size;
         
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-GPG-CHECKSUM-TOKEN\@=.*/\@MOVIE-CONTENT-GPG-CHECKSUM-TOKEN\@=$contentHash{movieGpgCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-GPG-FILE-SIZE-TOKEN\@=.*/\@MOVIE-CONTENT-GPG-FILE-SIZE-TOKEN\@=$contentHash{movieGpgSize}/\' $file";
         system("$cmd");
     }
     
    if ( -f "$propertiesHash{content}/movie.pgp" ) { 
         $checkSum = `md5sum $propertiesHash{content}/movie.pgp`;
         chomp($checkSum);
         @tmp = split(" ",$checkSum);
         $contentHash{moviePgpCheckSum} = $tmp[0];
         $contentHash{moviePgpSize} = stat("$propertiesHash{content}/movie.pgp")->size;
         
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-PGP-CHECKSUM-TOKEN\@=.*/\@MOVIE-CONTENT-PGP-CHECKSUM-TOKEN\@=$contentHash{moviePgpCheckSum}/\' $file";
         system("$cmd");
     
         $cmd = "sed -i \'s/^\@MOVIE-CONTENT-PGP-FILE-SIZE-TOKEN\@=.*/\@MOVIE-CONTENT-PGP-FILE-SIZE-TOKEN\@=$contentHash{moviePgpSize}/\' $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`;
    myMsg("\n\negrep: $line\n\n");
    
    if ($line =~ /$adiV101Pattern/) {
        # myMsg("match ADI V1.1 - $line\n");
        ($id) = $line =~ /$adiV101Pattern/;
    }
    if ($line =~ /$adiV300Pattern/ ) {
        # myMsg("match ADI V3.0 - $line\n");
        ($id) = $line =~ /$adiV300Pattern/;
    }   
    
    if ($line =~ /$orangeCmsPattern/ ) {
        # myMsg("match mpCmsWonVod - $line\n");
        ($id) = $line =~ /$orangeCmsPattern/;
    }  
    # myMsg("ID VALUE: $id\n");
    return $id;
}   
#            if (/Name=\"Content_FileSize\" Value=\"(.*?)\"/) {
#            ($contentFileSize) = /Name=\"Content_FileSize\" Value=\"(.*?)\"/;
#            myMsg("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;
             #myMsg("name [$propertyName] value [$propertyValue]\n");
             #myMsg("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};
            myMsg("$msgTxt");
        }
}

###################################################################################
#  subroutine: tidyOldFiles
###################################################################################

sub tidyOldFiles {
    my $packageDirListRef = $_[0];
    my $packageTarListRef = $_[1];
    my $propertiesHashRef = $_[2];
    
    if (@$packageDirListRef > 0) {
        if ( $$propertiesHashRef{export} =~ /yes/i ){ 
            myMsg("\nThe following packages have been exported....\n\n");
        } else {
            myMsg("\nThe following packages have been unpacked....\n\n");
        }
        foreach $element (@$packageDirListRef) { myMsg("\t$element\n"); }
        myMsg("\n");
    } 
        

    if (@$packageTarListRef > 0) {
        myMsg("\nThe following packages have been ingested....\n\n");
        foreach $element (@$packageTarListRef) { myMsg("\t$element\n"); }
    }

    if (( $$propertiesHashRef{ingest} =~ /yes/i ) || ( $$propertiesHashRef{export} =~ /yes/i )){    
        if ( $$propertiesHashRef{delete} =~ /prompt/i ){ 
            $$propertiesHashRef{delete} = verifyDelete(); 
            myMsg("\n");
        }   
    }    

    if (@$packageDirListRef > 0) {
        if (( $$propertiesHashRef{delete} =~ /yes/i ) && (( $$propertiesHashRef{ingest} =~ /yes/i ) || ( $$propertiesHashRef{export} =~ /yes/i ))) {
            myMsg("\n");
            foreach $element (@$packageDirListRef) {
                myMsg("deleting package directory\t[$element]\n");
                # 1 - verify delete
                rmtree("$element", 0);
            }
        }
    }

    if (@$packageTarListRef > 0) {
        if (( $$propertiesHashRef{delete} =~ /yes/i ) && (( $$propertiesHashRef{ingest} =~ /yes/i ) || ( $$propertiesHashRef{export} =~ /yes/i ))) {
            myMsg("\n");
            foreach $element (@$packageTarListRef) {
                myMsg("deleting package tar file\t[$element]\n");
                unlink("$element");
            }    
        }
    }
}
###################################################################################
#  subroutine: verifyDelete
###################################################################################
  
sub verifyDelete {
    while (1) {
        myMsg("\nDelete the ingested packages? [yes|no]\n");
        my $answer=<STDIN>;
        chomp $answer;
        if ( $answer =~ /y.*|n.*/i) {
            if ($answer =~ /y.*/i) { return "yes"; }
            if ($answer =~ /n.*/i) { return "no"; }
        }
        myMsg("Invalid response: [$answer]\n");
    }
}

##################################################################################
##################################################################################

sub myDate {
    my ($tmp,$tmpmin,$tmphour,$tmpmday,$tmpmon,$tmpyear); 
    # Get the new date.
    # ($tmp,$tmpmin,$tmphour,$tmpmday,$tmpmon,$tmpyear,$tmp,$tmp,$tmp) = localtime();
    ($tmp,$tmpmin,$tmphour,$tmpmday,$tmpmon,$tmpyear,$tmp,$tmp,$tmp) = localtime();

    # Format the date with the correct number leading zeros etc.
    # return(sprintf("%02d",($tmpmday)) . sprintf("%02d",($tmpmon + 1)) . sprintf("%04d",($tmpyear + 1900)));
    # srv 09may11 - change to 'date month day' so auto ordered for file listings.
    return(sprintf("%04d",($tmpyear + 1900)) . sprintf("%02d",($tmpmon + 1)) . sprintf("%02d",($tmpmday)));
}
 
##################################################################################
##################################################################################

sub myTimeUnformatted {
    my ($tmp,$tmpmin,$tmphour,$tmpmday,$tmpmon,$tmpyear); 
    # Get the new time.
    # ($tmp,$tmpmin,$tmphour,$tmpmday,$tmpmon,$tmpyear,$tmp,$tmp,$tmp) = localtime();
    ($tmp,$tmpmin,$tmphour,$tmpmday,$tmpmon,$tmpyear,$tmp,$tmp,$tmp) = localtime();

    # Format the time with the correct number leading zeros etc.
    return(sprintf("%02d", $tmphour) . sprintf("%02d", $tmpmin));
}
 
##################################################################################
##################################################################################

sub myMsg{
    my $message = $_[0];
    unless (defined($message)) { $message = ""; }
    
    print LOG $message;
    print $message;

}

###################################################################################
#  subroutine: help
###################################################################################

sub help {

$helpText1 = <<ENDHELPPAGE1;

  This wrapper script that calls the TOKEN substitution script 'subToken.pl' to generate test metadata packages.
  
  Typically used to generate ADI.XML files, however any kind of metadata files can be generated.
  
  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 populated 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

  To preserve current content filenames, simply use symbolic links, for example:
  
         # ls -al movie.ts
         lrwxrwxrwx 1 root root 15 May 28 12:35 movie.ts -> title1_movie.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=/aaaaa'
  
  Usage:
  
     > ./ metaGen.pl
     
  By default, this will create ONE unencrypted package with a base ID of '0001' in the sub directory 'metatar'.
  If the subdirectory 'tar' does not exist it will be created.
  
     # tar -tvf metatar/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:
  
      > ./metaGen.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:
  
      > ./metaGen.pl -token="metadata.adi.envivio.token,metadata.adi.token"
      
  For more information on metadata (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   [nn]
                    The numeric part of the ID. 
                    Typically this will always be '01' but can be selected if required.
                    Useful to identify specific sets of test data.
                    
                        Example: -batch=09 -type=clear  -> ID09
                        
                    If defined, will also change the token '\@BATCH-ID\@'.
                    Used to generate the 4th to 5th digits in the package / content unique identifiers.
                    
                        Example: MOVC001[01]0000000001
                    
                    default [01]    
                                                   
      -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]

      -delete     : value   [yes|no|prompt]
                    If auto ingest OR auto export is enabled, delete the packages before the script exits.
                    default [prompt] 
                    
      -encrypt     : value   [NONE|gpg|pgp]
                    If either gpg or pgp is specified, do NOT include the clear movie.ts in the package.
                    default [NONE] 
                    
      -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/CHANGE-ME]  
                    
      -limit      : value [integer]
                    The number of packages to generate.
                    default [1]
                    
      -log        : value [file name]
                    Name of the log file to capture metagen output.   
                    default [metagen.log]
                    
                    special [date] will create a date/time stamped file
                                   for example 20120831.1252.metagen.log
                            [name] same as date, but with the package name included.

      -maxfilesize: value   [INTEGER]
                    Maximum size of a content file, tested against the 'boxcover jpg'.
                    The boxcover tends to be the longest content file name generated.
                    Some CMS project implementations specify a maximum content file size.
                    Easier to trap now, than create an ingest error "Input File Name is not Valid"
                    default [100]                
      
      -metaroot   : value   [/path]
                    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 [metatar]

      -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]                    
                    
      -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]     

      -seed       : value [integer]
                    The starting value of all package identifiers.
                    default - 
                        The script looks for the file:
                            \${meta}/metaGen.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]
                                                
      -template   : Template file used to generate metadata file, for example 'ADI.XML'.
                    default [TEMPLATE.adi.V1.1.clear.xml ]

      -text       : value   [text_string]
      -text001    : value   [text_string]
      -text002    : value   [text_string]
      -text003    : value   [text_string]
      
                    If the tokens \@TEXT\@ \@TEXT-001\@ ( 002, 003 ) are contained in the template, 
                    their 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]   
                    
      -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"
                    
                    Tokens defined in the first files in the list take presedence over later files.
                    Identical tokens defined in later files are ignored.

                    Typically one or two files will be specified.
                    The last file in the list will define values for ALL tokens.
                    The first file will define a subset of tokens that need to be over written for 
                    a particular test requirement.
                    
                    Example:
                    
                        # cat disneyQcTest.token
                        \@PROVIDER-ID\@=Disney
                        \@QC-REQUIRED\@=true
 
                      Then use the command:
 
                        # ./metaGen.pl -token=disneyQcTest.token,metadata.adi.token

                    default [metadata.adi.token] 

      -tools      : Directory where dependent scripts 'subToken.pl' are located.
                    default [/opt/tandbergtv/cms/scripts/tools]
                    
      -type       : DO NOT USE - DEPRECATED!
                    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]

      -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]     

      -value      : value [\@TOKEN-ONE\@=xxxx,\@TOKEN-TWO\@=yyyy]
                    A comma separated list of tokens and values.
                    Allows any token in the template to be set or overridden from the command line.

                    Example: > metagen -value=\@PROVIDER-NAME\@=testProvider,\@PROVIDER-ID\@=testId.com
                            
                    default [none] 
                    
      -wait       : value [integer]
                    Number of seconds to wait between multiple ingests.
                    default [0]   
                    
ENDHELPPAGE1

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

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

# END
