#!C:\Interwoven\TeamSite/iw-perl/bin/iwperl
use File::Copy;
use TeamSite::Config;
use XML::DOM;
use XML::Parser;
use open ':utf8';
use TeamSite::DCRnode;
use TeamSite::DCRparser;
use Time::localtime;
use File::Basename;
use Encode;
use strict;
use warnings;
open(LOG ,">C:/Interwoven/TeamSite/tmp/Cloning.log");
my $Workarea = "";
my $fileLine = $ARGV[0];
if ($fileLine ne "") {
my @fline = split("####",$fileLine);
my @clone = split("~",$fline[1]);
for (my $c=1;$c<= $#clone ;$c++) {
my $cloneName = $clone[$c];
my $pathSource = $fline[0];
print LOG "Arguments Provided :-\n[$cloneName]\n[$pathSource]\n";
print LOG " The source of the HTMLS:- $pathSource \n";
print LOG " The dest of the HTMLS:- $cloneName \n";
my $path = "Y:/";
$pathSource =~ s/\\/\//g;
my @pathSourceArr = split(/\//,$pathSource);
my $source_branch =$pathSourceArr[$#pathSourceArr-1];
print LOG " The source of the HTMLS after replacement :- $pathSource \n";
my @spiltSource = split("/",$pathSource);
$pathSource = "Y:/";
my $templateData = "Y:/";
print LOG "The splitted source path :- \n @spiltSource\n";
for (my $i=3;$i < $#spiltSource ;$i++) {
$path .= $spiltSource[$i]."/";
$pathSource .= $spiltSource[$i]."/";
if (($spiltSource[$i] ne "html")&& ($spiltSource[$i] ne "content") ) {
$templateData .= $spiltSource[$i]."/";
}
if($spiltSource[$i] eq "WORKAREA")
{
$Workarea = $spiltSource[$i+1];
}
}
print LOG "Workarea Name :- [$Workarea]\n";
my $xmlFile = $templateData . "xml/" . $Workarea . "/siteName.xml";
print LOG " siteName xml file Path :-[$xmlFile]\n";
my $AddToMetadata = "Yes";
if(-e $xmlFile)
{
my $parser=new XML::DOM::Parser;
my $doc=$parser->parsefile($xmlFile);
my $nodes = $doc->getElementsByTagName("itemID");
my $nname = $doc->toString;
my $rootnode= TeamSite::DCRnode->new($nname);
my @new_node = $rootnode->get_node_list('itemID'); # getting the array of all the siteName nodes.
my $n = $nodes->getLength;
for (my $i = 0; $i <= $#new_node; $i++)
{
my $att_value = $new_node[$i]->value();
if( $att_value eq $cloneName )
{
print LOG " ****** siteName Updation Starts Here(If File exists in Xml) ***** \n";
$new_node[$i]->set_inner_xml($cloneName);
my $pro_node_xml = $rootnode->get_xml();
print LOG "\nNew Meta Data XML :-\n $pro_node_xml \n";
open(FILE,">$xmlFile");
print FILE "";
print FILE $pro_node_xml;
close(FILE);
$AddToMetadata = "No";
print LOG "After checking the siteName\.xml AddToMetadata or not :- [$AddToMetadata] \n";
print LOG " ****siteName Updation Ends Here**** ";
last;
}
}
if ($AddToMetadata eq 'Yes') {
print LOG " *****siteName Updation Starts Here(If File doesnot exists in the Xml) ***** \n";
$rootnode->set_inner_xml($rootnode->get_inner_xml() . "\n$cloneName\n");
my $pro_node_xml = $rootnode->get_xml();
print LOG "\nNew Meta Data XML :-\n $pro_node_xml \n";
open(FILE,">$xmlFile");
print FILE "";
print FILE $pro_node_xml;
close(FILE);
print LOG " *****siteName Updation Ends Here**** ";
}
}
else
{
print LOG " **** siteName Creation Starts Here(IF siteName.xml does not exists) **** \n";
open(FILE,">$xmlFile");
print FILE "";
print FILE "\n$cloneName\n";
close(FILE);
print LOG " *****siteName Updation Ends Here**** ";
}
$path .= $cloneName ;
$pathSource .= $spiltSource[$#spiltSource];
print LOG "The constructed source and destination path is :- \n $pathSource and $path \n";
print LOG "The constructed templatedata path is :- \n $templateData \n";
if( -e $path){}
else{
mkdir($path,"0777") or die "Unable to create folder: $!";
}
#opendir(DIR, $from_dir);
#my @dir1 = readdir(DIR);
#closedir(DIR);
sub copy_recursively {
my ($from_dir,$to_dir) = @_;
opendir(DIR, $from_dir) or die "Could not open dir '$from_dir': $!";
my @dir = readdir(DIR);
for(my $j=2 ; $j <= $#dir; $j++){
my $source = "$from_dir/$dir[$j]";
my $destination = "$to_dir/$dir[$j]";
next if($dir[$j] eq "." || $dir[$j] eq "..");
if (-d $source) {
print LOG "The source and destination in the directory section :-\n $source and $destination\n";
mkdir($destination,"0777") or die "mkdir '$destination' failed: $!" if not -e $destination;
copy_recursively($source, $destination);
} else {
print LOG "The source and destination in the File section :-\n $source and $destination\n";
open(FILE,"<", $source);
my @file_content = ;
close(FILE);
for(my $u=0;$u<=$#file_content;$u++)
{
$file_content[$u] = s/logo-$source_branch/logo-$cloneName/g ;
print LOG "$file_content[$u]";
}
open(FILEOUT,">", $destination);
for(my $v=0;$v<=$#file_content;$v++)
{
print FILEOUT "$file_content[$v]";
}
#copy($source, $destination) or die "copy failed: $!";
my $cmd = "iwextattr -g TeamSite/Templating/PrimaryDCR $source";
my $dcrName = `$cmd`;
my $cmdDcrType = "iwextattr -g TeamSite/Templating/PrimaryDocumentType $source";
my $dcrType =`$cmdDcrType`;
print LOG "CMD command execueted with the HTML for DCR Type :- [$cmdDcrType]\n\n$dcrType\n";
if($dcrName ne "" && $dcrType ne "")
{
my @dcrNameSplit = split("/",$dcrName);
print LOG "CMD command execueted with the HTML for DCR Name :- [$cmd]\n\n[$dcrName]\n\n@dcrNameSplit\n";
my $dcrPath = "$templateData";
$dcrPath .= "templatedata/$dcrType/data/$cloneName";
mkdir($dcrPath,"0777")or die "mkdir '$dcrPath' failed: $!" if not -e $dcrPath;
$dcrPath .= "/";
my $setDCRName = "$cloneName";
for (my $k=1;$k<$#dcrNameSplit ;$k++) {
$dcrPath .= $dcrNameSplit[$k];
mkdir($dcrPath,"0777")or die "mkdir '$dcrPath' failed: $!" if not -e $dcrPath;
$dcrPath .= "/";
$setDCRName .= "/$dcrNameSplit[$k]"
}
my @splitDcrType = split("/",$dcrType);
my $PT = $splitDcrType[$#splitDcrType]."\.xsl";
$dcrPath .= $dcrNameSplit[$#dcrNameSplit];
$setDCRName .= "/$dcrNameSplit[$#dcrNameSplit]";
print LOG "\n The New DCR Pathh is:- $dcrPath\n";
my $dcrSource = "$templateData";
$dcrSource .= "templatedata/$dcrType/data/$dcrName";
print LOG "\n The DCR Pathh is:- $dcrSource\n";
copy($dcrSource,$dcrPath) or die "copy of the dcr failed: $!";
my $cmd1 = "iwextattr -s TeamSite/Templating/DCR/Type=$dcrType $dcrPath";
my $new1 = `$cmd1`;
my $cmdToSet = "iwextattr -s TeamSite/Templating/PrimaryDCR=$setDCRName $destination";
my $newDcrName = `$cmdToSet`;
my $cmd2 = "iwextattr -s TeamSite/Templating/PrimaryDocumentType=$dcrType $destination";
my $new2 = `$cmd2`;
my $cmd3 = "iwextattr -s TeamSite/Templating/PrimaryPT=$PT $destination";
my $new3 = `$cmd3`;
print LOG "\n The New output Type[$newDcrName][$cmdToSet] is set for the DCR :- $dcrPath\n";
}
}
}
closedir(DIR);
return;
}
sub alter_content_for_logo{
}
copy_recursively($pathSource, $path);
}
}
close(LOG);
#unlink($fileName);
exit;