#!/usr/bin/env perl
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
# COPYRIGHT (c) 2000 European Southern Observatory
# LICENSE
#
# PROJECT:   VLT Data Flow System
# AUTHOR:    Michele Zamparelli - ESO/DMD/SEG
# CI NUMBER: 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
$rcs_version = '$Revision: 1.4 $';
#' this for xemacs
$| = 1 ;

# last checks first

sub dielog { 
    local($msg) = $_[0];
    $message =  "module1-failure: $msg" ;
    shmwrite($shm_id,$message ,0,200) || die "$!" ;
    die "$msg\n";
}


# here we start

$shm_id = $ARGV[0] ; 
$instrument = $ARGV[1] ; 
$basecamp =  $ARGV[2] ;
$tarfile = $ARGV[3];
$version = $ARGV[4];
$offline = $ARGV[5] ; 

if (   ( $ENV{MIDASHOME} eq "")  || ( $ENV{MIDVERS} eq "" )  ) { 
    print "MIDASHOME or MIDVERS undefined or wrongly set \n";
    dielog "MIDASHOME or MIDVERS undefined or wrongly set\n";
}

print "*" x 30;
print "\nIPIP MODULE ONE $rcs_version\n";
#print "SHM is  $shm_id\n";
print "Instrument is $instrument\n";
#print "Basecamp is $basecamp\n";
print "Tarfile is $tarfile\n";
print "Release  is $version\n";
sleep 1;

$install_dir = "$basecamp/$instrument-$version" ;

if ( -d $install_dir && -w $install_dir ) { 
    print "Deleting previous release from $install_dir\n";
    system "/bin/rm -fr $install_dir";
    dielog "Could not delete directory $install_dir, please do it yourself\n" if $?;
# I am not too sure about the following
    if ( -e "$basecamp/$instrument" && -l "$basecamp/$instrument" ) { 
	system "/bin/rm $basecamp/$instrument";
	dielog "Could not delete link $basecamp/$instrument  please do it yourself\n" if $?;
    }
}


$tmpdir = "ipip-tmp$$";
$tmplog = "/tmp/ipip-tmp$$-setup.log";
warn "Could not delete previous ipip temporary directories. Not tragic.\n" if $?;
chdir $basecamp;
system "/bin/rm -fr ipip-tmp* >/dev/null 2>&1";
#
system "mkdir $tmpdir";
chdir "$tmpdir";
#print "directory should now be $basecamp/$tmpdir\n";
# location of initial tarfile must be univoquely determined
system "zcat $tarfile | tar xf -";
die "Could not untar  $tarfile\n" if $? !=0;
#
chop( $main_dir = `/bin/ls -d $instrument*`) ;
chdir "$main_dir"; 
#
if ( $? !=0 ) { 
    shmwrite($shm_id, "module1-failure",0,60) || die "$?";
    die "Could not change directory to $main_dir\n" ;
}
#
#print "directory now  $basecamp/$tmpdir/$main_dir\n";
#
# creating deployment directory
#
print "Creating deployment directory\n";
system "mkdir $install_dir";
dielog "Could not mkdir $install_dir \n" if ( $? != 0 ) ;


#
# 110705: It is very unlikely that a tmplog based on $$ id exist before 
# unlink $tmplog; 
#
#
#spawn a subprocess to monitor
#
$pid = fork();   die "Fork failed: $!" unless defined $pid;
if ( ! $pid  ) { 
    chdir "/tmp";
    $command = "/bin/ps -fu ".(getpwuid($>))[0] ." | grep ./setup | grep -v grep";
    sleep 2;
    while(1) { 
	@running = `$command`;
	if ($#running == -1) { 
	    exit;
	}
	print ".";
	sleep 20;
    }
}


print "Running setup $install_dir in $main_dir\n(this may take a while)\n";
print "Installation log available in $tmplog\n";
if ( -e "./setup" )  {
system "./setup $install_dir ". (($offline == 1)?"offline":"" ). " >  $tmplog  2>&1 ";
}
elsif ( -e "./setup.sh" ) { 
  system "./setup.sh $install_dir ". (($offline == 1)?"offline":"" ). " > $tmplog 2>&1 " ;
 }
else {
    print "*** setup or setup.sh file not found ****\n"; 
    dielog "Failed running setup in $ENV{PWD}\n";
    }

#
if ( $? !=0 ) { 
    print "*** SETUP EXECUTION FAILURE ****\n"; 
#    chdir "$basecamp";
#    system "cat $basecamp/$tmpdir/$main_dir/setup.log >> $tmplog";
#    system "/bin/rm -fr  $basecamp/$tmpdir";
    print "For further information see the file $tmplog\n";
    dielog "Failed running setup or setup.sh in $ENV{PWD}\n";
    }
# 110705 CG: Do not remove the logs, they might be necessary even if 
# the installation succedeed.
# unlink $tmplog;
print "\nSetup completed successfully\n";
#
if ( -e "$basecamp/$instrument" ) { 
    if ( -l "$basecamp/$instrument" ) { 
	unlink "$basecamp/$instrument";
    } else { 
	die "Sorry, but $basecamp/$instrument is not a link!!!\n";
    }
}

chdir "$basecamp";
system "/bin/ln -sf $instrument-$version $instrument";
#
if ( $? != 0 ) { 
    dielog "Could not link $instrument-$version to $instrument \n" ;
}

system "/bin/rm -fr  $basecamp/$tmpdir";
die "Could not cleanup temporary directory $basecamp/$tmpdir" if $? != 0;

