#! /bin/sh

#===============================================================================
#
#  $Id: configure,v 1.9 1998/09/17 18:09:51 miod Exp $
#
#  Configuration script for MikMod... Not autconf yet, but it gets nearer !
#
#===============================================================================

#
#========== Helper functions
#

yesno () {
	ans=""
	while test -z "$ans"
	do
		echo -e "\r$1 [y/n] \c" >&2
		read ans
		case $ans in
			y*|Y*)	ans=Y ;;
			n*|N*)	ans=N ;;
			*)
				echo "Invalid answer. Please answer Y or N" >&2
				ans=""
			;;
		esac
	done
#	echo >&2
	echo $ans
}

#
#========== 0. Initialize variables
#

system=""
ranlib=""
cc=""
cflags=""
mm_cflags=""
mm_ldflags=""
driver_af=""
driver_aix=""
driver_esd=""
driver_hp=""
driver_oss=""
driver_sgi=""
driver_sun=""
drivers=""
fnmatch=""
getopt=""
usleep=""
virtch="virtch.o"

#
#========== 1. Check the system
#

echo "MikMod'Unix 3.0.4 configuration"
echo

system=`uname`
case $system in
	# IRIX, IRIX64
	# - sgi driver
	# - no GNU getopt
	# - has fnmatch(3) and usleep(3)
	IRIX*)
		echo "Configuring for IRIX with SGI audio driver"
		system=SGI
		ranlib=true
		cc=cc
		cflags="-O"
		mm_ldflags="-laudio"
		mm_cflags="-DDRV_SGI"
		driver_sgi="SGI"
		drivers="drv_sgi.o"
		getopt="getopt.o getopt1.o "
	;;
	# AIX
	# - aix driver
	# - no GNU getopt
	# - has fnmatch(3) and usleep(3) but no prototype for usleep
	AIX)
		echo "Configuring for AIX with RS/x000 audio driver"
		system=AIX
		ranlib=ranlib
		cc=cc
		cflags="-O3"
		mm_cflags="-DAIX -DDRV_AIX"
		driver_aix="AIX"
		drivers="drv_aix.o"
		getopt="getopt.o getopt1.o "
	;;
	# HP-UX
	# - hp-ux driver
	# - no GNU getopt
	# - has fnmatch(3) and usleep(3)
	HP-UX)
		echo "Configuring for HP-UX with HP audio driver"
		system=HP
		ranlib=ranlib
		cc=cc
		cflags="-Ae +O3"
		mm_cflags="-DDRV_HP"
		driver_hp="HP"
		drivers="drv_hp.o"
		getopt="getopt.o getopt1.o "
	;;
	# Linux
	# - oss driver
	# - esd driver recommended
	# - has GNU getopt
	# - has fnmatch(3) and usleep(3)
	Linux)	# Ought to check what uname returns under FreeBSD
		echo "Configuring for $system"
		system=LINUX
		ranlib=ranlib
		cc=gcc
		cflags="-O3 -funroll-loops -ffast-math -finline-functions -fomit-frame-pointer -Wall"
		driver_esd="ESD"
		driver_oss="OSS"
		mm_cflags="$mm_cflags -DHAVE_GNU_GETOPT"
	;;
	# FreeBSD
	# - oss driver
	# - no GNU getopt
	# - has fnmatch(3) and usleep(3)
	FreeBSD)
		echo "Configuring for FreeBSD"
		system=FREEBSD
		ranlib=ranlib
		cc=gcc
		cflags="-O3 -funroll-loops -ffast-math -finline-functions -fomit-frame-pointer -Wall"
		driver_oss="OSS"
		getopt="getopt.o getopt1.o "
	;;
	# {Net,Open}BSD
	# - netbsd (sun-like) driver
	# - no GNU getopt
	# - has fnmatch(3) and usleep(3)
	NetBSD|OpenBSD)
		echo "Configuring for $system"
		system=NETBSD
		ranlib=ranlib
		cc=gcc
		cflags="-O3 -funroll-loops -ffast-math -finline-functions -fomit-frame-pointer -Wall"
		mm_cflags="-DDRV_SUN"
		driver_sun="SUN"
		drivers="drv_sun.o"
		getopt="getopt.o getopt1.o "
	;;
	# SunOS, Solaris
	# - sun driver
	# - no GNU getopt
	# - has fnmatch(3) and usleep(3) but no prototype for usleep
	SunOS) # Ought to support Solaris i386
		system=SUN
		ranlib=ranlib
		cc=cc
		cflags="-O -fast"
		mm_cflags="-DDRV_SUN"
		driver_sun="SUN"
		drivers="drv_sun.o"
		getopt="getopt.o getopt1.o "
		case `uname -r` in
			4*)
				echo "Configuring for SunOS with Sun audio driver"
				mm_cflags="$mm_cflags -DSUNOS"
			;;
			*)
				echo "Configuring for Solaris with Sun audio driver"
			;;
		esac
	;;
	# DEC OSF/1
	# - audiofile driver
	# - no GNU getopt
	# - don't know if it has fnmatch(3) and usleep(3)
	OSF1)
		echo "Configuring for OSF/1"
		system=ALPHA
		ranlib=ranlib
		cc=cc
		cflags="-O"
		mm_cflags="-DDRV_AF"
		driver_af="AF"
		getopt="getopt.o getopt1.o "
	;;
	# Other system
	*)
		echo "Gee ! I don't know your system. It seems that MikMod was never compiled on"
		echo "this platform. You'll have to tweak the configuration scripts and the"
		echo "proto Makefiles, and perhaps more. Anyway, please contact me at :"
		echo "miodrag@mygale.org"
		echo "There's no reason to keep your platform unsupported !"
		exit 0
	;;
esac

# Check for fnmatch() and usleep()
echo
echo "Checking for missing Posix.2 functions..."
if test ! -f /usr/include/fnmatch.h
then
	echo "I don't see <fnmatch.h> on your system. It seems your Unix lacks the fnmatch(3)"
	ecoh "function."
	result=`yesno "Do you need to use the included fnmatch(3) ?"`
	if test "$result" = "Y"
	then
		fnmatch="fnmatch.o "
		mm_cflags="$mm_cflags -DNEED_FNMATCH"
	fi
fi

if test -z "`grep usleep /usr/include/unistd.h`" -a \
   -z "`grep usleep /usr/include/sys/unistd.h`"
then
	mm_cflags="$mm_cflags -DNEED_USLEEP"
	case $system in
		AIX) ;;
		SUN) ;;
		*)
			echo "I don't see an usleep(3) prototype in <unistd.h>. It seems your Unix lacks the"
			echo "usleep(3) function."
			result=`yesno "Do you need to use the included usleep(3) ?"`
			if test "$result" = "Y"
			then
				usleep="usleep.o "
			fi
		;;
	esac
fi

#
#========== 2. Ask the user for his/her choices
#

### Debug version
echo
echo "Debugging..."
debug=`yesno "Do you want a debug version ?"`
if test "$debug" = "Y"
then
	mm_cflags="$mm_cflags -DMIKMOD_DEBUG -g"
	mm_ldflags="$mm_ldflags -g"
	efence=`yesno "Do you want to link with Electric Fence for debugging ?"`
	if test "$efence" = "Y"
	then
		mm_ldflags="$mm_ldflags -lefence"
	fi
fi

### Drivers
echo
echo "Drivers..."

# AF driver
echo "Do you want to include the Digital AudioFile driver ?"
if test -z "$driver_af"
then
	echo "(this is not recommended for this platform)"
else
	echo "(this is recommended for your platform if you have the AudioFile library)"
fi
driver_af=`yesno "Include AudioFile driver ?"`
if test "$driver_af" = "Y"
then
	mm_ldflags="$mm_ldflags -lAF"
	mm_cflags="$mm_cflags -DDRV_AF"
	drivers="$drivers drv_AF.o"
fi

# AIX driver only under AIX

# ESD driver
echo "Do you want to include the Enlightment sound daemon (EsounD) driver ?"
if test -z "$driver_esd"
then
	echo "(this is not recommended for this platform)"
else
	echo "(this is recommended for your platform if you have the EsounD library)"
fi
driver_esd=`yesno "Include EsounD driver ?"`
if test "$driver_esd" = "Y"
then
	mm_ldflags="$mm_ldflags -lesd"
	mm_cflags="$mm_cflags -DDRV_ESD"
	drivers="$drivers drv_esd.o"
fi

# HP-UX driver only under HP-UX

# OSS driver
echo "Do you want to include the Open Sound System (OSS) driver ?"
if test -z "$driver_oss"
then
	echo "(this is not recommended for this platform)"
else
	echo "(this is recommended for your platform)"
fi
driver_oss=`yesno "Include OSS driver ?"`
if test "$driver_oss" = "Y"
then
	mm_cflags="$mm_cflags -DDRV_OSS"
	drivers="$drivers drv_oss.o"
fi

# SGI driver only under SGI

# SUN driver only under SUN / OpenBSD
if test "$system" = "SUN"
then
	headphone=`yesno "Do you want to use the headphone in the Sun audio driver ?"`
	if test "$headphone" = "Y"
	then
		mm_cflags="$mm_cflags -DUSE_HEADPHONE"
	fi
fi

### Interface
echo
echo "Interface..."

# Ncurses
echo "For the player, ncurses 4.0 or later is recommended."
ncurses=`yesno "Do you have ncurses installed (/usr/include/ncurses.h and -lncurses) ?"`
if test "$ncurses" = "Y"
then
	mm_cflags="$mm_cflags -DHAVE_NCURSES"
	mm_ldflags="$mm_ldflags -lncurses"
else
	mm_ldflags="$mm_ldflags -lcurses"
fi

### Options
echo
echo "Options..."

# Cpu time snagger
case "$system" in
	LINUX|FREEBSD|NETBSD|OPENBSD)
		echo "The Cpu time snagger feature allows MikMod to get more CPU time and even"
		echo "realtime priority with the switches -renice and -realtime"
		if test "$system" = "NETBSD"
		then
			echo "(realtime priority is not supported under `uname`)"
		fi
		snagger=`yesno "Do you want the Cpu time snagger feature ?"`
		if test "$snagger" = "Y"
		then
			mm_cflags="$mm_cflags -DCPUTIME_SNAGGER"
		fi
	;;
	*) ;;
esac

# High quality mixer
echo "The High quality mixer will improve the quality of your sounds, but requires"
echo "a huge amount of power ! That's why it is not recommended."
virtch2=`yesno "Do you want the high quality mixer ?"`
if test "$virtch2" = "Y"
then
	virtch="virtch2.o"
	mm_cflags="$mm_cflags -DVIRTCH2"
fi

#
#========== 3. Generate Makefiles
#

echo

# Search for imposed variables
if test "$CC"
then
	cc=$CC
fi
if test "$CFLAGS"
then
	cflags=$CFLAGS
fi

extraobj="${getopt}${fnmatch}${usleep}"
if test "$extraobj"
then
	extralib="extra.a"
	mm_ldflags="$mm_ldflags ..\/extra\/$extralib"
else
	extralib="FORCE"
fi

# some @#!$%!# systems lack mktemp
###tmpfile=`mktemp /tmp/.mikmod.XXXXXX`
tmpfile="/tmp/.mikmod.$$"
echo "s/@CC@/$cc/" > $tmpfile
echo "s/@CFLAGS@/$cflags/" >> $tmpfile
echo "s/@RANLIB@/${ranlib}/" >> $tmpfile
echo "s/@DRIVERS@/$drivers/" >> $tmpfile
echo "s/@MM_CFLAGS@/${mm_cflags}/" >> $tmpfile
echo "s/@MM_LDFLAGS@/${mm_ldflags}/" >> $tmpfile
echo "s/@VIRTCH@/${virtch}/" >> $tmpfile
echo "s/@EXTRAOBJ@/${extraobj}/" >> $tmpfile
echo "s/@EXTRALIB@/${extralib}/" >> $tmpfile

for directory in extra mmio playercode usercode
do
	echo "Generating Makefile in $directory"
	sed -f $tmpfile $directory/Makefile.in > $directory/Makefile
done

rm -f $tmpfile

#
#========== 4. Print an option summary
#
echo
echo "Configuration is complete. MikMod is ready to compile."
echo "Just enter 'make' at the command prompt..."
echo
