#!/bin/sh

#
# display help screeen
#
help() {
cat << END
Usage: configure [options]
--prefix=DIR			set arch independent install prefix to DIR [$prefix]
--exec-prefix=DIR		set arch dependent install prefix to DIR [\$prefix]
--bindir=DIR			install executables in DIR [\$exec_prefix/bin]
--datadir=DIR			install read-only data in DIR [\$prefix/share]
--includedir=DIR		install header files in DIR [\$prefix/include]
--libdir=DIR			install libraries in DIR [\$exec_prefix/lib]
--pkgconfdir=DIR		install pkg-config file in DIR [\$libdir/pkgconfig]
--mandir=DIR			install man pages in DIR [\$prefix/man]
--precision=NUMBER		default sndiod device bit-depth [$precision]
--privsep-user=USER		non-privileged user for sndio daemon [$user]
--enable-alsa			enable alsa audio & midi backends [$alsa]
--disable-alsa			disable alsa audio & midi backends
--enable-sun			enable sun audio backend [$sun]
--disable-sun			disable sun audio backend
--enable-rmidi			enable character device midi backend [$rmidi]
--disable-rmidi			disable character device midi backend
--enable-umidi			enable usb-midi backend [$umidi]
--disable-umidi			disable usb-midi backend
--with-libbsd			use the libbsd rather than bsd-compat/*
--without-libbsd		don't use libbsd
--default-dev=DEV		set default device [$dev]
END
}

#
# defaults
#
version=1.9.0				# package version (used by pkg-config)
prefix=/usr/local			# where to install sndio
so="libsndio.so.\${MAJ}.\${MIN}"	# shared libs to build
alsa=no					# do we want alsa support ?
sun=no					# do we want sun support ?
oss=no					# do we want oss support ?
rmidi=no				# do we want support for raw char dev ?
umidi=no				# do we want support for umidi ?
precision=16				# sndiod default device bit-depth
user=_sndio				# non-privileged user for sndio daemon
libbsd=no				# use libbsd?
so_ldflags=				# extra linker flags for shared libs
unset vars				# variables passed as arguments
unset exec_prefix			# prefix for arch. independent files
unset bindir				# path where to install binaries
unset datadir				# path where to install doc
unset mandir				# path where to install man pages
unset includedir			# path where to install header file
unset libdir				# path where to install library
unset pkgconfdir			# path where to install pkg-config file
unset defs				# no extra #defines
unset ldadd				# no extra libraries (-l options)
unset dev

#
# guess OS-specific parameters
#
case `uname` in
	Linux)
		alsa=yes
		ldadd="-lrt"
		user=sndiod
		so_link="libsndio.so libsndio.so.\${MAJ} libsndio.so.\${MAJ}.0"
		so_ldflags="-Wl,-soname=libsndio.so.\${MAJ}"
		defs='-D_GNU_SOURCE -DHAVE_SOCK_CLOEXEC -DHAVE_CLOCK_GETTIME'
		;;
	NetBSD)
		sun=no
		rmidi=yes
		user=_sndio
		so_link="libsndio.so"
		defs='-DHAVE_ARC4RANDOM -DHAVE_GETPEEREID -DHAVE_ISSETUGID \\\
		-DHAVE_STRLCAT -DHAVE_STRLCPY \\\
		-DHAVE_SOCK_CLOEXEC -DHAVE_CLOCK_GETTIME'
		;;
	OpenBSD)
		sun=yes
		rmidi=yes
		user=_sndiop
		defs='-DHAVE_ARC4RANDOM -DHAVE_GETPEEREID -DHAVE_ISSETUGID \\\
		-DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRTONUM \\\
		-DHAVE_SOCK_CLOEXEC -DHAVE_CLOCK_GETTIME'
		;;
	DragonFly|FreeBSD)
		oss=yes
		umidi=yes
		user=_sndio
		so_ldflags="-Wl,-soname=libsndio.so.\${MAJ}.\${MIN}"
		so_link="libsndio.so"
		defs='-DHAVE_ARC4RANDOM -DHAVE_GETPEEREID -DHAVE_ISSETUGID \\\
		-DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRTONUM \\\
		-DHAVE_SOCK_CLOEXEC -DHAVE_CLOCK_GETTIME'
		;;
	Darwin)
		rmidi=no
		so="libsndio.\${MAJ}.\${MIN}.dylib"
		so_link="libsndio.dylib"
		defs='-DHAVE_ARC4RANDOM -DHAVE_GETPEEREID -DHAVE_ISSETUGID \\\
		-DHAVE_STRLCAT -DHAVE_STRLCPY'
esac

# shell word separator (none)
IFS=''

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

for i; do
	case "$i" in
	--prefix=*)
		prefix="${i#--prefix=}"
		shift;;
	--exec-prefix=*)
		exec_prefix="${i#--exec-prefix=}"
		shift;;
	--bindir=*)
		bindir="${i#--bindir=}"
		shift;;
	--datadir=*)
		datadir="${i#--datadir=}"
		shift;;
	--includedir=*)
		includedir="${i#--includedir=}"
		shift;;
	--libdir=*)
		libdir="${i#--libdir=}"
		shift;;
	--pkgconfdir=*)
		pkgconfdir="${i#--pkgconfdir=}"
		shift;;
	--mandir=*)
		mandir="${i#--mandir=}"
		shift;;
	--enable-alsa)
		alsa=yes
		shift;;
	--disable-alsa)
		alsa=no
		shift;;
	--enable-oss)
		oss=yes
		shift;;
	--disable-oss)
		oss=no
		shift;;
	--enable-sun)
		sun=yes
		shift;;
	--disable-sun)
		sun=no
		shift;;
	--enable-rmidi)
		rmidi=yes
		shift;;
	--disable-rmidi)
		rmidi=no
		shift;;
	--enable-umidi)
		umidi=yes
		shift;;
	--disable-umidi)
		umidi=no
		shift;;
	--privsep-user=*)
		user="${i#--privsep-user=}"
		shift;;
	--precision=*)
		precision="${i#--precision=}"
		if [ "$precision" != 16 -a "$precision" != 24 ]; then
			echo "$i: only 16 and 24 are supported" >&2
			exit 1
		fi
		shift;;
	--with-libbsd)
		libbsd=yes
		shift;;
	--without-libbsd)
		libbsd=no
		shift;;
	--default-dev=*)
		dev="${i#--default-dev=}"
		shift;;
	CC=*|CFLAGS=*|LDFLAGS=*)
		vars="$vars$i$nl"
		shift;;
	*)
		help
		exit 1
		;;
	esac
done

#
# if $xxxdir is not specified, define it to $prefix/xxx
#
exec_prefix="${exec_prefix:-$prefix}"
bindir="${bindir:-$exec_prefix/bin}"
datadir="${datadir:-$prefix/share}"
includedir="${includedir:-$prefix/include}"
libdir="${libdir:-$exec_prefix/lib}"
pkgconfdir="${pkgconfdir:-$prefix/lib/pkgconfig}"
mandir="${mandir:-$prefix/share/man}"

#
# umidi implies rmidi
#
if [ $umidi = yes ]; then
	rmidi=yes
fi

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

#
# if using OSS, add corresponding parameters
#
if [ $oss = yes ]; then
	defs="$defs -DUSE_OSS"
fi

#
# if using Sun API, add corresponding parameters
#
if [ $sun = yes ]; then
	defs="$defs -DUSE_SUN -DUSE_SUN_MIXER"
fi

#
# if using raw character devices for midi, add corresponding parameters
#
if [ $rmidi = yes ]; then
	defs="$defs -DUSE_RMIDI"
fi

#
# if using usb-midi raw devices for midi, add corresponding parameters
#
if [ $umidi = yes ]; then
	defs="$defs -DUSE_UMIDI"
fi

#
# if using libbsd, add corresponding parameters
#
if [ $libbsd = yes ]; then
	# no arc4random as libbsd has a questionable implementation
	defs="$defs -DUSE_LIBBSD -DHAVE_STRLCPY -DHAVE_STRLCAT -DHAVE_STRTONUM"
	ldadd="$ldadd -lbsd"
fi

if [ -n "$dev" ]; then
	defs="$defs -DDEFAULT_DEV='\"$dev\"'"
fi


for f in Makefile aucat/Makefile midicat/Makefile sndiod/Makefile \
libsndio/Makefile \
sndioctl/Makefile \
examples/Makefile \
contrib/init.d.sndiod \
contrib/sndiod.service
do
	sed \
	-e "s:@bindir@:$bindir:" \
	-e "s:@datadir@:$datadir:" \
	-e "s:@includedir@:$includedir:" \
	-e "s:@libdir@:$libdir:" \
	-e "s:@pkgconfdir@:$pkgconfdir:" \
	-e "s:@mandir@:$mandir:" \
	-e "s:@version@:$version:" \
	-e "s:@defs@:$defs:" \
	-e "s:@ldadd@:$ldadd:" \
	-e "s:@so@:$so:" \
	-e "s:@so_link@:$so_link:" \
	-e "s:@so_ldflags@:$so_ldflags:" \
	-e "s:@vars@:${vars}:" \
	-e "s:@precision@:$precision:" \
	-e "s:@user@:$user:" \
	-e "s:@devs@:$devs:" \
	< $f.in > $f
done

#
# Generate sndio.pc. Substitute path prefixes with ${prefix} or
# ${exec_prefix} variable names, so that they can be overriden by the
# user
#
cat <<EOF >libsndio/sndio.pc
prefix=${prefix}
exec_prefix=`echo $exec_prefix | sed -e "s:^${prefix}:\\${prefix}:"`
libdir=`echo $libdir | sed -e "s:^${exec_prefix}:\\${exec_prefix}:"`
includedir=`echo $includedir | sed -e "s:^${prefix}:\\${prefix}:"`

Name: sndio
Description: sndio library
Version: ${version}
Requires:
Libs: -L\${libdir} -lsndio
Cflags: -I\${includedir}
EOF

chmod +x contrib/init.d.sndiod

cat <<EOF

version.................. $version
bindir................... $bindir
datadir.................. $datadir
includedir............... $includedir
libdir................... $libdir
pkgconfdir............... $pkgconfdir
mandir................... $mandir
user..................... $user
libbsd................... $libbsd
precision................ $precision
alsa..................... $alsa
oss...................... $oss
sun...................... $sun
rmidi.................... $rmidi
umidi.................... $umidi

Do "make && make install" to compile and install sndio

Installation requires the "$libdir" directory to be part of
the system library search path. The "$user" user must exist and
its primary group must have access to audio and MIDI hardware.

EOF
