#! /usr/bin/perl

# testing java in the system

print STDERR "Testing default java virtual machine in the system ... ";

$flag = 0;
$out = `java -version 2>&1`;
$jpath = "java";
if ( $out !~ /\w/ || $out=~/GNU/){

   show_java_msg();
   $flag = 1;
}

if($flag==0){
  $out =~ /java version \"1\.(\d)\.*/;
  if($1<5){
    show_java_msg();
    $flag = 1;
  }
}


if($flag == 1){

   print STDERR "\nIf you do have compatible JRE in the system, specify the path to java\n";
   $jpath = <STDIN>;
   chomp $jpath;
   $out = `$jpath -version 2>&1`;
   if ( $out !~ /\w/ || $out=~/GNU/){
      print STDERR "Sorry, $jpath is not a compatible JRE\n";
      exit(1);
   }

   $out =~ /java version \"1\.(\d)\.*/;
   if($1<5){
      print STDERR "Sorry, $jpath (version 1\.$1) is not a compatible JRE \n";
      exit(1);
   }
   $flag = 0;
}

if($flag == 0){
  
   print STDERR "OK\n";
}       

$install_path = "/usr/local/Structure/";
if(defined($ARGV[0])){
    $install_path = "$ARGV[0]";
} 





if( ! -d "$install_path"){
    `mkdir $install_path`;
} 

if( ! -d "$install_path" ){
    exit(1);
}

# copy files

print STDERR "Copy files to $install_path ... \n";
`cp -r class $install_path`;
`cp -r bin   $install_path`;
`cp -r images $install_path`;
`cp -r doc    $install_path`;
`cp -r sample $install_path`;
`cp -r library $install_path`;
# startup script


open START, ">structure";
print START "\#\!/bin/sh\n";
print START "cd $install_path\n";
print START "$jpath -cp class/Structure.jar RunStructure\n";
`chmod +x structure`;

print STDERR "structure v2.3.2 is installed successfully\n"; 



sub show_java_msg{
     print STDERR "\nStructure version 2.3.2 requires Sun Java Runtime Environment (version > 1.5.0)\n";
     print STDERR "If you don't have it already installed in the system, download and install the\n";
     print STDERR "the package from http\:\/\/www.java.com/download/ for free\n";
}

     



