#!/usr/bin/perl

use Tie::File;
if(-e "log.txt") {
        unlink("log.txt"); }
open(OUTPUT_LOG,'>', "log.txt");
my $steps =1;
$icd_tarfile="icd-registration.tgz";
$lib32="/lib/x86";
$lib64="/lib/x86_64";
print OUTPUT_LOG  "$steps )Starting Installation of AMD APP.... \n";
$steps=$steps+1;
#Finiding the OS level#
$OS_TYPE = `uname -a`;
if($OS_TYPE =~ /x86_64/) {
   	print OUTPUT_LOG  "$steps )64-bit Operating System Found.. \n"; $steps=$steps+1;
   	print "64-bit Operating System Found.. \n ";
   	$SDK_NAME="AMD-APP-SDK-v2.5-RC2-lnx64.tgz";
   	$OS = "x64"; } else {
   	print OUTPUT_LOG "$steps )32-bit Operating System Found...\n"; 	$steps=$steps+1; 
   	$SDK_NAME="AMD-APP-SDK-v2.5-RC2-lnx32.tgz";
   	$OS = "x86"; }
$dirname =  substr($SDK_NAME,0,length($SDK_NAME)-4);
print "Starting Installation of AMD APP.... \n ";
print "SDK package name is :$dirname.tgz\n";
#Remove the tgz and creat a new tgz#
if(-e $dirname) {
	print "\nDirectory already exists deleting\n ";
	$result = system("rm -rf $dirname"); }
$cur_dir = `pwd`;
print "Current directory path is  : $cur_dir";
$cmd = "tar -xzf $SDK_NAME";
$cmd_icd = "tar -xzf $icd_tarfile -C /";
$result = system ($cmd);
$result_icd =system ($cmd_icd);
if($result) {
	print OUTPUT_LOG "\n $steps )Failed to untar the sdk package\n";  $steps=$steps+1; 
        print "\nFailed to untar the sdk package\n"; } else {
	print OUTPUT_LOG "$steps )Untar command executed succesfully, The SDK package available\n"; $steps=$steps+1;
        print "Untar command executed succesfully, The SDK package available\n"; }
if($result_icd) {
	print OUTPUT_LOG "\n $steps )Failed to untar the icd setting tar file\n"; $steps=$steps+1;
        print "Failed to untar the icd setting tar file\n"; } else {
	print OUTPUT_LOG "$steps )Untar command executed succesfully, The ICD package available \n"; $steps=$steps+1;
	print "Untar command executed succesfully, The ICD package available \n"; }
chomp($dirname);
chomp($cur_dir);
#Copy files to destination folder#
$AMDAPPSDKROOT= "/opt/AMDAPP";
print OUTPUT_LOG "$steps )SDK Binaries/Source/Samples/Other files copying to  = $AMDAPPSDKROOT \n"; $steps=$steps+1;
print "Copying files to $AMDAPPSDKROOT/ ... \n";
$dt = "rm -rf /opt/AMDAPP/";
$result = system ($dt);
$cp = "mv -f $dirname /opt/AMDAPP/";
$result = system ($cp);
if(!$result) {
	print OUTPUT_LOG "$steps )SDK files copied successfully at /opt/AMDAPP/\n"; $steps=$steps+1;
	print "SDK files copied successfully at /opt/AMDAPP/\n";       } else {
	print OUTPUT_LOG "$steps )Filed to copy SDK files to /opt/AMDAPP/, please check the root permission and try 
                          again   \n";  $steps=$steps+1;
        print "Filed to copy SDK files to /opt/AMDAPP/, please check the root permission and try again \n"; }
print OUTPUT_LOG "$steps )Updating Environment vairables... \n"; $steps=$steps+1;
print "Updating Environment vairables... \n";
#Setting the Env vairables#
my $file_name = "/etc/profile" ;
$delSDK = "perl -i -nle 'print if !/AMDAPPSDKROOT/' $file_name";
$result = system ($delSDK);

$del = "perl -i -nle 'print if !/AMDAPP/' $file_name";
$result = system ($del);

$delLDPATH = "perl -i -nle 'print if !/export LD_LIBRARY_PATH/' $file_name";
$result = system ($delLDPATH);

$lib32="/lib/x86";
$lib64="/lib/x86_64";
print OUTPUT_LOG "$steps )Updating LD_LIBRARY_PATH in /etc/profile";
$steps=$steps+1;
$libpath32 = "\$"."LD_LIBRARY_PATH:"."\"".$AMDAPPSDKROOT.$lib32."\"";
$libpath64 = "\$"."LD_LIBRARY_PATH:"."\"".$AMDAPPSDKROOT.$lib64."\"".":"."\"".$AMDAPPSDKROOT.$lib32."\"";
if($OS eq "x64"){
	$libpath =$libpath64;
	$dt = "rm -f /etc/ld.so.conf.d/amdapp*";
	$result = system ($dt);
	print "32-bit path is :";
	$cr = "echo /opt/AMDAPP/lib/x86 | sudo tee /etc/ld.so.conf.d/amdapp_x86.conf";
	$result = system ($cr);
	print "64-bit path is :";
	$cr = "echo /opt/AMDAPP/lib/x86_64 | sudo tee /etc/ld.so.conf.d/amdapp_x86_64.conf";
	$result = system ($cr); } else {
	$dt = "rm -f /etc/ld.so.conf.d/amdapp*";
	$result = system ($dt);
	print "32-bit path is :";
	$cr = "echo /opt/AMDAPP/lib/x86 | sudo tee /etc/ld.so.conf.d/amdapp_x86.conf";
	$result = system ($cr);
	$libpath =$libpath32; }
	$result = `echo AMDAPPSDKROOT='\"$AMDAPPSDKROOT\"' >> $file_name`;
	$result = `echo LD_LIBRARY_PATH='$libpath' >> $file_name`;
	$result = `echo export AMDAPPSDKROOT >> $file_name` ;
	$result = `echo export LD_LIBRARY_PATH >> $file_name` ;
if(!$result) {
	print OUTPUT_LOG "$steps )Environment vairables updated successfully\n"; $steps=$steps+1;
        print "Environment vairables updated successfully\n"; } else {
	print OUTPUT_LOG "$steps )Filed to update the Environment vairables, please check the root permissions \n"; 
        $steps=$steps+1;
	print "Filed to update the Environment vairables, please check the root permissions \n"; }
print OUTPUT_LOG "$steps )Initialize global environment vairables... \n"; $steps=$steps+1;
$in = "sudo ldconfig -v >ldconfig";
$result = system ($in);
print OUTPUT_LOG "$steps )Global environment vairables Initialized\n"; $steps=$steps+1;
print "Global environment vairables Initialized\n";
print OUTPUT_LOG "$steps )AMD APP installation Completed\n"; $steps=$steps+1;
print "AMD APP installation  Completed\n";
print OUTPUT_LOG "$steps )Reboot required to reflect the changes"; $steps=$steps+1;
print ">>Reboot required to reflect the changes\n";
close OUTPUT_LOG;
