#!/usr/local/bin/perl -w
#

if (!(exists $ENV{'ROOTSYS'})) {
    die print "Your ROOTSYS environment variable is not set. Did you install Root?\n";
}
#if (!(exists $ENV{'ALICE_ROOT'})) {
#    die print "Your ALICE_ROOT environment variable is not set. Did you download AliRoot?\n";
#}
if (!(exists $ENV{'DATE_ROOT'})) {
    print "Your DATE_ROOT environment variable is not set. Did you install DATE?\n";
    print "If not, you may not run SPDmood online. Set 'WITH_DATE = 0' in src/Makefile\n";
}

if (exists $ENV{'SPDMOOD_SITE'}) {
    if (-d "$ENV{'SPDMOOD_SITE'}/src" && -d "$ENV{'SPDMOOD_SITE'}/bin") {
	print "Building the code...\n";
	$switch = "";
	if (-e "$ENV{'ROOTSYS'}/lib/libRoot.a") {
	    $switch = "all";
	}
	$command = "cd $ENV{'SPDMOOD_SITE'}/src; make clean; make $switch; make install;";
	print "$command\n";
	system($command);
    }
    else {
	die print "Something is wrong with your SPDMOOD_SITE directory. src and/or bin dir missing!\n";
    }

    print "Building done. Checking your environment...\n";

    $status = 0;
    $path = $ENV{'PATH'};
    $pattern="$ENV{'SPDMOOD_SITE'}/bin";
    if (!($path=~m/\Q$pattern\E/)) {
	print "Your SPDMOOD_SITE/bin is not in your PATH!\n";
	$status++;
    }
    $libpath = $ENV{'LD_LIBRARY_PATH'};
    $pattern="$ENV{'SPDMOOD_SITE'}/lib";
    if (!($libpath=~m/\Q$pattern\E/)) {
	print "Your SPDMOOD_SITE/lib is not in your LD_LIBRARY_PATH!\n";
	$status++;
    }
    print "Finished check. \n";
    if ($status>0) {
	print "See above comments for further actions.\n";
    }
}
else {
    die print "You need to set the environment variable SPDMOOD_SITE to the relevant directory!\n";
}


