#!/usr/bin/perl	-w
# Description: Create Installer

if($> != 0)
{
	die("You need to be root to install");
}

$discreetDir = "/usr/discreet/sparks";
if(!(-e $discreetDir))
{
	die("error: directory '$discreetDir' does not exist");
}

print("Starting Installation\n\n");

# Backwards compatabile
#if((-e "$discreetDir/PHOTRON") && (! -l "$discreetDir/PHOTRON"))
#{
#	if((-e "$discreetDir/PHOTRON_BACKUP") && (! -l "$discreetDir/PHOTRON_BACKUP"))
#	{		
#		print("Removing existing PHOTRON backup\n");
#		system("rm -rf $discreetDir/PHOTRON_BACKUP");
#	}
#	print("Making PHOTRON backup\n");
#	system("mv $discreetDir/PHOTRON $discreetDir/PHOTRON_BACKUP");
#}
#if((! -l "$discreetDir/PHOTRON"))
#{
#	system("ln -s $discreetDir/PRIMATTE $discreetDir/PHOTRON");
#}

mkdir("$discreetDir/PRIMATTE");


$version = "5.0";
print("Installing sparks:\n");
print "Installing Primatte $version ... \n\n";

#print "1. 32-Bit version  \n2. or 64-Bit version \n\n?";

$bit_version = "2";

if($bit_version != "1" && $bit_version != "2")
{
	print "Error, do not recognize! Exit... \n";
	exit;
}

print "1. Node-locked version  \n2. or Floating (rlm) version \n\n?";

$rel_version = <STDIN>;

if($rel_version != "1" && $rel_version != "2")
{
	print "Error, do not recognize! Exit... \n";
	exit;
}


# delete old files
if(-e "$discreetDir/PRIMATTE/PrimatteV5.spark_x86")
{
	system("rm $discreetDir/PRIMATTE/PrimatteV5.spark_x86");
}
if(-e "$discreetDir/PRIMATTE/PrimatteV5.spark_x86_64")
{
	system("rm $discreetDir/PRIMATTE/PrimatteV5.spark_x86_64");
}
if(-e "$discreetDir/PRIMATTE/PrimatteV5_rlm.spark_x86")
{
	system("rm $discreetDir/PRIMATTE/PrimatteV5_rlm.spark_x86");
}
if(-e "$discreetDir/PRIMATTE/PrimatteV5_rlm.spark_x86_64")
{
	system("rm $discreetDir/PRIMATTE/PrimatteV5_rlm.spark_x86_64");
}

if($bit_version == "2" && $rel_version == "2")
{
	print "Installing 64-bit Floating version...\n";
	system("cp plugins/PrimatteV5_rlm.spark_x86_64 $discreetDir/PRIMATTE/.");
	system("ln -s  $discreetDir/PRIMATTE/PrimatteV5_rlm.spark_x86_64 $discreetDir/PRIMATTE/PrimatteV5.spark_x86_64");
}

if($bit_version == "2" && $rel_version == "1")
{
	print "Installing 64-bit Node Locked version...\n";
	system("cp plugins/PrimatteV5.spark_x86_64 $discreetDir/PRIMATTE/.");
}

if($bit_version == "1")
{
	$pluginFile = $discreetDir . "/PRIMATTE/PrimatteV5.spark_x86";
}
else
{
	$pluginFile = $discreetDir . "/PRIMATTE/PrimatteV5.spark_x86_64";
}
if(!(-e $pluginFile))
{
	print "Error, plugin not installed! Cannot find $pluginFile \nExit... \n";
	exit;	
}


print "	 $discreetDir/PRIMATTE/\n\n";



print("Installing Licensing executable:\n");

if($rel_version == "1")
{
	print "	 $discreetDir/PRIMATTE/GetSerialCode\n\n";
	system("cp GetSerialCode $discreetDir/PRIMATTE/.");
}
else
{
	print "	 $discreetDir/PRIMATTE/rlmutil\n\n";
	system("cp rlmutil $discreetDir/PRIMATTE/.");
}

print("Installing Html Documentation:\n");
$docDir = " /usr/local/PRIMATTE/SPARK";
system("mkdir -p $docDir");
system("cp -rf doc $docDir/.");
system("chmod 777 $docDir/doc/*.htm");
print("	$docDir/doc/Prim_Spark_Manual.htm\n\n");

if(-e "IMG")
{
	print("Installing Images:\n");
	system("cp -rf IMG $discreetDir/PRIMATTE/.");
	print("	$discreetDir/PRIMATTE/IMG\n\n");
}

print("Finished Installation\n\nExit root user & read Html Documentation for licensing instructions\n\n");
