#!/bin/sh

#
# defaults
#
prefix=/usr/local		# where to install midish
readline=no			# do we have readline
alsa=no				# do we want alsa support ?
sndio=no			# do we want sndio support ?
rmidish=yes			# do we want to build rmidish ?
progs=midish			# programs to build
unset vars			# variables definitions passed as-is
unset cflags			# compilation flags
unset ldflags			# linking flags
unset bindir			# path where to install binaries
unset datadir			# path where to install doc and examples
unset mandir			# path where to install man pages
unset defs			# no extra #defines
unset ldadd			# no extra libraries (-l options)
unset readline_lib		# path to readline library
unset readline_include		# path to readline header files

#
# guess if we want alsa devices support
#
if [ `uname` = Linux ]; then
	alsa=yes
fi

#
# guess if we want sndio devices support
#
if [ `uname` = OpenBSD ]; then
	sndio=yes
fi

#
# guess readline(3) parameters
#
if [ -e /usr/include/readline/readline.h ]; then
	readline=yes
elif [ -e /usr/local/include/readline/readline.h ]; then
	readline=yes
	readline_include=/usr/local/include
	readline_lib=/usr/local/lib
fi

#
# display help screeen
#
help() {
cat << END
Usage: configure [options]
--prefix=DIR			set install prefix to DIR [$prefix]
--bindir=DIR			install executables in DIR [\$prefix/bin]
--datadir=DIR			install read-only data in DIR [\$prefix/share]
--mandir=DIR			install man pages in DIR [\$prefix/man]
--readline-lib=DIR		path to readline library
--readline-include=DIR		path to readline header files
--enable-alsa			enable alsa sequencer backend [$alsa]
--disable-alsa			disable alsa sequencer backend
--enable-sndio			enable libsndio backend [$sndio]
--disable-sndio			disable libsndio backend
--enable-rmidish		build rmidish binary [$rmidish]
--disable-rmidish		don't build rmidish
END
}

# shell word separator (none)
IFS=''

# sed-quoted new-line
nl='\
'

for i; do
	case "$i" in
	--prefix=*)
		prefix="${i#--prefix=}"
		shift;;
	--bindir=*)
		bindir="${i#--bindir=}"
		shift;;
	--datadir=*)
		datadir="${i#--datadir=}"
		shift;;
	--mandir=*)
		mandir="${i#--mandir=}"
		shift;;
	--readline-lib=*)
		readline=yes
		readline_lib="${i#--readline-lib=}"
		shift;;
	--readline-include=*)
		readline=yes
		readline_include="${i#--readline-include=}"
		shift;;
	--enable-alsa)
		alsa=yes
		shift;;
	--disable-alsa)
		alsa=no
		shift;;
	--enable-sndio)
		sndio=yes
		shift;;
	--disable-sndio)
		sndio=no
		shift;;
	--enable-rmidish)
		rmidish=yes
		shift;;
	--disable-rmidish)
		rmidish=no
		shift;;
	CC=*|CFLAGS=*|LDFLAGS=*)
		vars="${vars+$vars$nl}$i"
		shift;;
	*)
		help
		exit 1
		;;
	esac
done

bindir="${bindir:-$prefix/bin}"
datadir="${datadir:-$prefix/share}"
mandir="${mandir:-$prefix/man}"

#
# add parameters for ALSA support
#
if [ $alsa = yes ]; then
	defs="$defs -DUSE_ALSA"
	ldadd="$ldadd -lasound"
fi

#
# add parameters for libsndio support
#
if [ $sndio = yes ]; then
	defs="$defs -DUSE_SNDIO"
	ldadd="$ldadd -lsndio"
fi

#
# add rmidish front-end if readline is present
#
if [ $rmidish = yes ]; then
	if [ $readline != yes ]; then
		echo "Can't build rmidish without readline library"
		exit 1
	fi
	progs="$progs rmidish"
fi

#
# if variables defined, then insert a comment
#
vars=${vars+"\\
# \\
# the following were passed as arguments to configure script\\
# \\
$vars\\
"}

echo "configure: creating Makefile"
sed \
-e "s:@bindir@:$bindir:" \
-e "s:@datadir@:$datadir:" \
-e "s:@mandir@:$mandir:" \
-e "s:@readline_lib@:${readline_lib+-L}$readline_lib:" \
-e "s:@readline_include@:${readline_include+-I}$readline_include:" \
-e "s:@defs@:$defs:" \
-e "s:@ldadd@:$ldadd:" \
-e "s:@progs@:$progs:" \
-e "s:@vars@:${vars}:" \
< Makefile.in >Makefile

echo
echo "bindir................... $bindir"
echo "datadir.................. $datadir"
echo "mandir................... $mandir"
echo "alsa..................... $alsa"
echo "sndio.................... $sndio"
echo "rmidish.................. $rmidish"
echo "readline-lib............. ${readline_lib-default}"
echo "readline-include......... ${readline_include-default}"
echo
echo "Do \"make && make install\" to compile and install midish"
echo
