#
#  Some utility routines available for use by various scripts
#

sub fatal
{
	local($error_message) = @_;
	print STDERR "$argv0: $error_message: $!\n";
	exit(1);
}

# Returns 1 iff the files are identical
sub filesEqual { # file1, file2
	local($file1, $file2) = @_;
	local($return) = 1;

	# This means two non-existant files aren't the same!
	if ( (! -r $file1) || (! -r $file2) ) {
		$debug && print STDERR "One file doesn't exist\n";
		return 0;
	}

	open(FILE1, "<$file1") || die "[filesEqual] $!: $file1\n";
	open(FILE2, "<$file2") || die "[filesEqual] $!: $file2\n";

	do {
		$line1 = <FILE1>;
		$line2 = <FILE2>;
	} while ($line1 && ($line1 eq $line2));

	if ($line1 ne $line2) {
		$debug && print STDERR "Files (1) $file1 and (2) $file2 differ:\n";
		$debug && print STDERR "(1) $line1\n";
		$debug && print STDERR "(2) $line1\n";
		$return = 0;
	}

	close(FILE1);
	close(FILE2);
	return $return;
}

sub touchIfNotAbsent { # file
	local($file) = @_;
	local($now) = time;

	# This doesn't make $file if it doesn't exist!
	utime($now, $now, $file) || &fatal("utime failed for $file");
}

sub touchIfAbsent { # file
	local($file) = @_;

	if (! -r $file) {
		open (BOGUS, ">$file") || &fatal("Unable to create $file");
		close (BOGUS);
	}
}

sub moveFile { # source dest
	local($source, $dest) = @_;

	# NT mv doesn't work across file drives
	if ($winnt) {
		`cp $source $dest`;
		`rm $source`;
	} else {
		`mv $source $dest`;
	}
}

#
#  Replaces file1 with file2 if they differ, otherwise file2 is removed.
#  Useful for building an autogenerated file and comparing them to avoid
#  touching the resulting file (or a given touchFile) if nothing has changed.
#  This can avoid rebuilding autogenerated source files.
#
sub replaceIfDifferent { # file1 file2 touchFile
	local ($file1, $file2, $touchFile) = @_;

	if (! $touchFile ) {
		$touchFile = $file1;
	}

	if ( ! &filesEqual($file1, $file2) ) {
		&moveFile($file2, $file1);
	} else {
		unlink($file2) || &fatal("Unable to unlink $file2");
		&touchIfAbsent($touchFile);
		&touchIfNotAbsent($touchFile);
	}
}

#
# When running on WinNT, /tmp doesn't exist.  This function returns the appropriate
# directory to put temporary files
#
sub temporaryFileDirectory {
   local($dir);

   if (!$winnt) {
      return "/tmp";
   }

   $dir = $ENV{"TEMP"};
   if (!$dir || ! -d $dir) {
       $dir = "c:/temp";
       if ( ! -d $dir ) {
            &fatal("Cannot find temporary directory.  Set TEMP environemnt variable.");
       }
   }

   return $dir;
}

#
# Cache the install root for all systems
#
if ($ENV{OMNIMAKE_INSTALL_ROOT}) {
    $OMNIMAKE_INSTALL_ROOT = $ENV{OMNIMAKE_INSTALL_ROOT};
} else {
    $OMNIMAKE_INSTALL_ROOT = $ENV{OMNIMAKE_ROOT};
}

# Setup $argv0 for fatal

($argv0 = $0) =~ s@.*/(.*)$@\1@;

#
# Determine if we're on NT or Solaris.  Hack, hack...
#

if ( -d "C:/" ) {
    $winnt = 1;
}
elsif ( `uname` eq "SunOS\n" ) {
    $solaris = 1;
}


#
# Set the appropraite executable extension
#
if ($winnt) {
    $executableExtension = ".exe";
} else {
    $executableExtension = "";
}

# Setup some common command paths
if ($winnt) {
    $gnumake     = "$ENV{NEXT_ROOT}/NextDeveloper/Executables/make" . $executableExtension;
} 
elsif ($solaris) {
  $gnumake     = "/NextDeveloper/bin/make";
}
else {
  $gnumake     = "/bin/gnumake";
}

1;

