#!/bin/csh -f
     
echo ""
echo "  This script compiles the SimWalk2 source code into an executable."
unalias g77
unalias rm
echo ""
echo " " >>&! simwalk2.tmp
if !( -w simwalk2.tmp ) then
	echo "  This script has FAILED because you do not have write permission"
	echo "  in this directory."
	echo ""
	exit 2
endif
rm simwalk2.tmp >& /dev/null
if !( -r simwalk2.f ) then
	echo "  This script has FAILED because the main source code file,"
	echo "  simwalk2.f, cannot be read from the current directory."
	echo ""
	exit 2
else
	if ( -r mendel.f ) then
		set file2 = mendel
	else if ( -r nomendel.f ) then
		set file2 = nomendel
	else
		echo "  This script has FAILED because neither the file"
		echo "  mendel.f nor nomendel.f can be read from the current"
		echo "  directory. One of these two files must be available."
		echo ""
		exit 2
	endif
endif
set rewrite = 0
if ( -w simwalk2 ) then
	if !( -w simwalk2.old ) then
		mv simwalk2       simwalk2.old    >& /dev/null
		set rewrite = 1
	else if !( -w simwalk2.older ) then
		mv simwalk2.old   simwalk2.older  >& /dev/null
		mv simwalk2       simwalk2.old    >& /dev/null
		set rewrite = 2
	else
		mv simwalk2.older simwalk2.oldest >& /dev/null
		mv simwalk2.old   simwalk2.older  >& /dev/null
		mv simwalk2       simwalk2.old    >& /dev/null
		set rewrite = 3
	endif
endif
if ( -e simwalk2 ) then
	echo "  This script has FAILED because there is currently a file"
	echo "  called simwalk2 that cannot be overwritten."
	echo "  Once this file has been moved, please rerun this script."
	exit 3
endif
echo "  Using the command: g77 -o simwalk2 -O simwalk2.f $file2.f"
echo ""
echo ""
g77 -o simwalk2 -O simwalk2.f $file2.f
rm $file2.o   >& /dev/null
rm simwalk2.o >& /dev/null
unset file2
if !( -x simwalk2 ) then
	echo ""
	echo ""
	echo "  This script has FAILED; for an unknown reason, the compiling"
	echo "  and linking using g77 did not result in an executable."
	echo ""
	if ( $rewrite != 0 ) then
		mv simwalk2.old    simwalk2       >& /dev/null
		echo "  The OLD version of SimWalk2 should still be available."
		echo ""
		if ( $rewrite != 1 ) then
			mv simwalk2.older  simwalk2.old   >& /dev/null
			if ( $rewrite != 2 ) then
				mv simwalk2.oldest simwalk2.older >& /dev/null
			endif
		endif
	endif
	unset rewrite
	exit 2
else
	echo ""
	echo ""
	echo "  This script appears to have been SUCCESSFUL."
	echo "  Once the data files are in place,"
	echo "  to execute the program SimWalk2"
	echo "  simply issue the command simwalk2 ."
	echo ""
	if ( $rewrite != 0 ) then
		echo "  The previous version of SimWalk2"
		echo "  has been moved to simwalk2.old"
		echo "  Of course you may delete all old versions"
		echo "  once the new version has been seen to work."
		echo ""
	endif
	unset rewrite
endif
echo ""
exit 0