#! /usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1996,2019 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# sccsid = "@(#)52   1.10   src/rsct/pgs/cmds/hagsSetup.pkg.pl, gsctrl, rsct_rady, rady2035a 3/9/16 05:40:32"

package hagsSetup;

$now = -1;

sub main'TSprint {
    # to match TraceStream output format
    local($hdr) = shift(@_);
    local($rightNow) = time;
    if ($rightNow != $now) {
	# let's only do this "heavy processing" when we need to; 
	# at most once a second.
	($TSPsec,$TSPmin,$TSPhour,$TSPmday,$TSPmon,$TSPyear,
		$TSPwday,$TSPyday,$TSPisdst) = localtime($rightNow);
    	$TSPmonth = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$TSPmon];
	$now = $rightNow;
    }
    print "$TSPmonth $TSPmday $TSPhour:$TSPmin:$TSPsec $hdr : @_";
}

# taken from SequenceNum.[Ch].

$NaN = 65535;  # Not a Number
$kHalfSequence = 32767;

sub CompareSequenceNumbers {
	local ($a) = $_[0];
	local ($b) = $_[1];
	# NaN is the smallest SequenceNumber
	if ($a == $b)   { return  0; } # equal
	if ($NaN == $a) { return -1; } # a < b
	if ($NaN == $b) { return  1; } # a > b
	local($c) = ($a + $NaN -1 - $b) % $NaN;
	return ($kHalfSequence > $c) ? 1 : -1;
}

#######################################################################
#  hagsGetInstance - maintain hags instance numbers
#
#  PGSD_TID_DIR is the directory to look in.
#
#######################################################################

sub main'GetInstance {
	($TidDir, $daemon) = @_;

	if (! -d $TidDir) {
		mkdir( $TidDir, 0750 ) || die "Can't make $TidDir: $!";
	}
	chdir( $TidDir ) || die "Can't cd to $TidDir: $!";

	$highest = $NaN;

	# Note that 'glob' does not work on NAS box, so use 'ls -1'
	@incfiles = `LC_ALL=C ls -1 $TidDir/$daemon\.in* 2> /dev/null`;
	if(defined($incfiles)) {
	    foreach ( @incfiles ) {
		chomp($_);
		push(@OldFilenames, $_);
		s/^$TidDir\/$daemon\.in//o; # strip off hagsd.in
		if (0 < &CompareSequenceNumbers( $_, $highest )) {
			# new highest sequence number; save it!
			$highest = $_;
		}
	    }
	}
	
	&'TSprint(TRACE_FYI, "largest incarnation number is: $highest\n" );
	
	if ($NaN <= ++$highest) {
		$highest = 0;
	}
	$NewIncarnationFile = "$daemon.in$highest";
	
	&'TSprint(TRACE_FYI, "NewIncar is $highest, filename is $NewIncarnationFile\n" );
	
	if ( ! open(NEWINCAR, ">$NewIncarnationFile" ) ) {
	        die "Can't open $NewIncarnationFile for output: $!\n";
			$rc = $! + 0;
	        exit $rc;
	}
	close(NEWINCAR);
	
	chmod 0, $NewIncarnationFile;
	
	if ($#OldFilenames >= $[) {
		unlink @OldFilenames;
	}
	
	$highest;
}

sub main'MoveToRunDir {
	local ($RunDir, $NodeNumber, $incarnation) = @_;

	if (! -d $RunDir) {
		&'TSprint(TRACE_FYI, "making run directory $RunDir\n" );
		mkdir( $RunDir, 0750 ) || die "Can't make $RunDir: $!";
	}
	&'TSprint(TRACE_FYI, "changing to run directory $RunDir\n" );
	chdir( $RunDir ) || die "Can't cd to $RunDir: $!";
	# sub RemoveCoreFile
	if ( -e "core") {
		$prevLife = $incarnation - 1;
		if (0 > $prevLife) {
			$prevLife = $NaN - 1;
		}
		$newCoreName = "core_$NodeNumber.$prevLife";
		&'TSprint(TRACE_FYI, "renaming core to $newCoreName\n" );
		rename("core", $newCoreName);
        } else {
		# Note that 'glob' does not work on NAS box, so use 'ls -1'
                $coreFile = `LC_ALL=C ls -1 "core\.*\.*" 2> /dev/null` ;
	        if ( defined($coreFile) && -e "$coreFile") {
	        	$prevLife = $incarnation - 1;
	        	if (0 > $prevLife) {
	        		$prevLife = $NaN - 1;
	        	}
	        	$newCoreName = "core_$NodeNumber.$prevLife";
	        	&'TSprint(TRACE_FYI, "renaming core to $newCoreName\n" );
	        	rename("$coreFile", $newCoreName);
                }
        }
}