#
# LocalVariables.pl
#
# This file should contain (all) site-specific machine-specific information 
# and/or routines
#
# Note variable $^O is predefined with the system type
#
# Version 0.4 DGC  2002/10/08 Include GzipIt (only coded for linux)
# Version 0.3 DGC  2002/05/17 Include backup and computer name
# Version 0.2 DGC  2002/04/10 First real version, only nicely integrated
#                              with StoreTestResults for now
#

return 1 ;

sub InitialiseLocalVariables {
#
# Note: forward slashes work fine also on Windows, and are strongly recommended...
#
# --- These variables refer to your windows test system ---
# This is your local sctvar directory. Few scripts need it
    $sctvar="d:/sctvar" ;

# This is your local archive directory. One subdirectory will be made per module
    $archive="/home/atlas/f1bdata/sct/sctvar_archive" ;

# This is your local backup directory. One subdirectory will be made per 
# backing up computer (since run number sequences differ for two different
# DAQ systems)

    $backup="n:/atlas/sct/sctvar_backup" ;

# Probably the following shouldn't need to change...

    $rawpsdir="$sctvar/ps" ;
    $rawresultdir="$sctvar/results" ;
    $rawdatadir="$sctvar/data" ;

# Computer name: following method works on Windows NT
# My convention is to use the computername in lower case - change if
# you wish different (the \L makes it lower case)

    if ($^O eq "MSWin32") {
	$ComputerName="\L$ENV{COMPUTERNAME}" ;
    }
    elsif ($^O eq "linux" || $^O eq "hpux") {
	$ComputerName=$ENV{HOST} ;
    }
    else {
# Fill your own in here if you're on a weird system...!
	$ComputerName="unknown" ;
    }

}

sub StripWebKey {
# This searches for the last occurrence of /, = or ? in a
# URL string, and returns the trailing characters
# You may need to modify if your WEB_KEY ends in a different 
# character
    my ($input,$l,$lasth) ;
    $input=$_[0] ;
    $lasth=-1 ;
    $l=rindex($input,'/') ;
    $lasth=$l if $l>$lasth ;
    $l=rindex($input,'=') ;
    $lasth=$l if $l>$lasth ;
    $l=rindex($input,'?') ;
    $lasth=$l if $l>$lasth  ;
    if ($lasth>-1) {
	return substr($input,$lasth+1) ;
    }
    return $input ;
}

sub CopyFile {
    my ($from,$to) ;
# This implementation should work on Windows and Linux/HPUX
# using system commands. The fallback is slow but also works, perhaps
# more generally (if such is needed).
    $from=$_[0] ;
    $to=$_[1] ;
    if (!-r $from) {
	print "CopyFile error: input file $from doesn't exist\n" ;
	return ;
    }
    if ($^O eq "MSWin32") {
	$from=~s/\//\\/g ;
	$to=~s/\//\\/g ;
	system "copy $from $to" ;
    }
    elsif ($^O eq "linux" || $^O eq "hpux") {
	system "cp $from $to" ;
    }
    else {
	open(CF_INPUT,$from) ;
	open(CF_OUTPUT,">$to") ;
	while (<CF_INPUT>) {
	    print CF_OUTPUT ;
	}
	close(CF_OUTPUT) ;
	close(CF_INPUT) ;
    }
}

sub GZipIt {
#
# Current version is unix-only
# but it should behave reasonably (no gzip) on other systems
#
    my $gzipped, $psfname, $psfext;
    my $Directory=$_[0] ;
    my $plot=$_[1] ;

    if ($^O eq "MSWin32") {
	if (-e "$plot.gz" && !-e "$plot") {
	    $plot="$plot.gz" ;
	}
	return $plot ;
    }
    elsif ($^O eq "linux" || $^O eq "hpux") {
	if($plot =~ /\.ps/ && !($plot =~ /\.gz/)) {
#       print "Generating name of zipped plot\n" ;
	    ($psfname,$psfext)=split(/\./,$plot) ;
	    $gzipped = "$psfname\.ps\.gz" ;
	    
	    if(! -e "$Directory/$gzipped") {
		print "Compressing $plot\n";
		system("/usr/bin/gzip $Directory/$plot");
	    }
	    
	    $plot = $gzipped;
	}
	return $plot ;
    }
    else {
	return $plot ;
    }
}
