#! /bin/sh


########################################################################
# go into source directory to make ...
cd src


########################################################################
# select make and cc programs
MAKE=${MAKE-make}

if [ "$GMAKE" = "" ]
then	# try to find gmake for use with gcc
	# (on some systems, Mac, I think, cc was gcc but make was not gmake
	#  which is a problem)
	if type gmake > /dev/null 2> /dev/null
	then	GMAKE=gmake
	else	if type gcc > /dev/null 2> /dev/null
		then	gdir=`type gcc | sed -e 's,.* ,,' -e 's,/[^/]*$,,'`
			if [ -x $gdir/make ]
			then	GMAKE=$gdir/make
			else	GMAKE=make
			fi
		else	GMAKE=make	# $MAKE ?
				# but make might be gmake while $MAKE is not
		fi
	fi
fi

# if make target is install, prevent using makefile.gcc as installation 
# target directories would be wrong
case "$1" in
*install*)	CC=true;;
esac

# if CC is explicitly set to gcc, shortcut to use it
case "$CC" in
gcc)	$GMAKE -f makefile.gcc $1
	exit;;
"")	CC=cc;;
esac


########################################################################
# for install targets, copy variables (root= etc) only if non-empty
copyvars () {
	case "$1" in
	?*)	shift;;
	esac
	makevars=
	for var in $*
	do	case $var in
		*=?*)	makevars="$makevars $var";;
		esac
	done
}

copyvars $*


########################################################################
# if make target is configure, redefine $MAKE to configure function
case "$1" in
configure)	MAKE=confmake
		GMAKE=$MAKE
		;;
esac

confmake () {
	while	case "$1" in
		-*)	true;;
		*)	false;;
		esac
	do	shift
	done
	rm -f makefile
	ln -s "$1" makefile
}


########################################################################
# select makefile suitable for operating system
case `uname` in
Linux*)	$MAKE -f makefile.linux $1 $makevars;;
Sun*)	if type $CC > /dev/null 2> /dev/null
	then	if $CC | grep "software package not installed" > /dev/null
		then	$GMAKE -f makefile.gcc $1 $makevars
		elif type $CC | grep "/ucb"
		then	$MAKE -f makefile.ucb $1 $makevars
		else	$MAKE -f makefile.sun $1 $makevars
		fi
	else	$GMAKE -f makefile.gcc $1 $makevars
	fi;;
HP*)	$MAKE -f makefile.hp $1 $makevars;;
*BSD*)	$GMAKE -f makefile.bsd $1 $makevars;;
CYG*)	if gcc -v 2>&1 | grep djgpp
	then	echo cross-compiling for djgpp on Cygwin system
		$MAKE -f makefile.dj $1 $makevars
	else	$MAKE -f makefile.cygwin $1 $makevars
	fi;;
Darwin*)
	if type gcc > /dev/null 2> /dev/null
	then	$MAKE -f makefile.osx $1 $makevars
	else	echo Mac compilation should have gcc available -
		echo keyboard mappings do not compile with cc
		false
	fi;;
Interix)
	$MAKE -f makefile.interix $1 $makevars;;
*)	echo trying to make in GNU mode
	$GMAKE -f makefile.gcc $1 $makevars;;
esac


########################################################################
# end
