#! /usr/bin/perl

use strict;
use warnings;
use File::Copy qw/ cp /; # For cp and move command

# Paths for FMS server
my $installFolderOnFMS = "/appl/plvt/install";
my $tcdata = $ENV{'TC_DATA'};
my $tcroot = $ENV{'TC_ROOT'};

if($#ARGV==-1) {
    print("Error : Missing arguments\n");
	print("First argument accepted values are [fms|bas]\n");
} else {
	print("==== BEGIN ====\n");
	my $serveur = $ARGV[0];
	
	if($serveur eq "fms") {
		if(!($#ARGV==2)) {
			print("Arguments <new version> and <old version> are mandatory [version number format X.XX.X]\n");
		} else {
			my $newVersion = $ARGV[1];
			my $oldVersion = $ARGV[2];
			
			print("Server : FMS | new version : $newVersion | old version: $oldVersion \n");
			
			if(!(-d "$installFolderOnFMS/rapt$newVersion")) {
				die("Process stopped : $installFolderOnFMS/rapt$newVersion does not exist");
			} else {
				if(-e "$installFolderOnFMS/rapt$newVersion/snecma-reports-$newVersion.zip") {
					
					print("Step 1/2 : Unzip delivery package .. \n");
					
					system("unzip $installFolderOnFMS/rapt$newVersion/snecma-reports-$newVersion.zip -d $installFolderOnFMS/rapt$newVersion");
				
					print("Step 2/2 : Copy of the report_prefs.xml file from the previous installation (version $oldVersion).. \n");
				
					if(-e "$installFolderOnFMS/rapt$oldVersion/Installation/teamcenter/data/report_prefs.xml") {
						cp("$installFolderOnFMS/rapt$oldVersion/Installation/teamcenter/data/report_prefs.xml","$installFolderOnFMS/rapt$newVersion/Installation/teamcenter/data") or die "Copy failed: $!\n";
						chmod(0755,"$installFolderOnFMS/rapt$newVersion/Installation/teamcenter/data/report_prefs.xml") or die "Cannot chmod file: $!\n";
					} else {
						die("Process stopped : The file report_prefs.xml does not exist in $installFolderOnFMS/rapt$oldVersion/Installation/teamcenter/data \n");
					}
				} else {
					die("Process stopped : snecma-reports-$newVersion.zip delivery package not found in $installFolderOnFMS/rapt$newVersion/");
				}
			}
		}
	} elsif($serveur eq "bas") {
		if (!($#ARGV==1)) {
			print("Environment argument is mandatory. Accepted values [int|form|mco|ap|op]\n");
		} else {		
			my $environment = $ARGV[1];
			
			if(("$environment" ne "ap") and ("$environment" ne "op") and ("$environment" ne "op") and ("$environment" ne "int") and ("$environment" ne "mco") and ("$environment" ne "form")) {
				die("Environment argument accepted values are [int|form|mco|ap|op]\n");
			}
			
			print("Copy reports policies from FMS to all BAS | environment : $environment \n");
			if(defined($tcdata)) {
			
				chdir("$tcdata/soa/policies") or die("$tcdata/soa/policies does not exist");
				
				if($environment eq "ap") {
					system("scp R*pol*xml sp0plv02\@s04995h0:/appl/plvt/simu/tcdata_101/tcdbapdata/soa/policies");
					system("scp R*pol*xml sp0plv02\@s04996s0:/appl/plvt/simu/tcdata_101/tcdbapdata/soa/policies");
				}
				if($environment eq "op") {
					system("scp R*pol*xml sp0plv12\@s05196f0:/appl/plvt/oper/tcdata_101/tcdbodata/soa/policies");
					system("scp R*pol*xml sp0plv12\@s05197p0:/appl/plvt/oper/tcdata_101/tcdbodata/soa/policies");
					system("scp R*pol*xml sp0plv12\@s05198y0:/appl/plvt/oper/tcdata_101/tcdbodata/soa/policies");
					system("scp R*pol*xml sp0plv12\@s08702t0:/appl/plvt/oper/tcdata_101/tcdbodata/soa/policies");
					system("scp R*pol*xml sp0plv12\@s08914l0:/appl/plvt/oper/tcdata_101/tcdbodata/soa/policies");
				}
				if($environment eq "int") {
					system("scp R*pol*xml sp0plv42\@s04616m0:/appl/plvt/int/tcdata/tcdbint4data/soa/policies");
					system("scp R*pol*xml sp0plv42\@s07680f0:/appl/plvt/int/tcdata/tcdbint4data/soa/policies");
					system("scp R*pol*xml sp0plv42\@s07681p0:/appl/plvt/int/tcdata/tcdbint4data/soa/policies");
				}
				if($environment eq "form") {
					system("scp R*pol*xml sp0plv32\@s05804t0:/appl/plvt/form/tcdata_101/tcdbfodata/soa/policies");
				}
				if($environment eq "mco") {
					system("scp R*pol*xml sp0plv22\@s04648r0:/appl/plvt/mco/tcdata_101/tcdbm2data/soa/policies");
				}
			} else {
				print("Process stopped : Environment variables $tcdata is not defined \n");
			}
		}
	} else {
		print("First argument accepted values are [fms|bas]\n");
	}
	
	print("==== END ====\n");
}