#!/bin/sh

. ../configure.inc

INSTALL_DIR=/usr/lib/ao/plugins-2/

HAVE_ROAR=false

while [ "$1" != '' ]
do
 case "$1" in
  --install-dir|--inst-dir)
   INSTALL_DIR="$2"
   shift;
  ;;
  --pkg-config)
   PKG_CONFIG="$2"
   shift;
  ;;
  --force-have-roar)
   HAVE_ROAR=true
  ;;
  --help|-h)
    cat << EOF
Usage: ./configure [OPTIONS]...

Options:
  --help               - Show this help
  --inst-dir DIR       - Install dir
  --force-have-roar    - Force to assume libroar is ok
  --pkg-config PKGCONF - Set filename for pkg-config
EOF
    exit 0
   ;;
  *)
    echo 'Unknown option. Try ./configure --help'
    exit 2
 esac
 shift;
done

on_error () {
 rm -f Makefile.conf
 exit 1;
}

check_cc;
check_pkg_config;

echo -n 'testing for libao... '
cat > tests.c << EOF
#include <ao/ao.h>
#include <ao/plugin.h>
int main (void) { return 0; }
EOF

if [ "$PKG_CONFIG" = '' ]
then
 AO_LIBS=''
 AO_CFLAGS=''
else
 AO_LIBS=`pkg-config --libs ao`
 AO_CFLAGS=`pkg-config --cflags ao`
fi
if [ "$AO_LIBS" = '' -a "$AO_CFLAGS" = '' ]
then
 AO_LIBS='-lao'
 AO_CFLAGS=''
fi

$CC $AO_LIBS $AO_CFLAGS -o tests tests.c 2> /dev/null
./tests 2> /dev/null

if [ "$?" = '0' ]
then
 echo yes
else
 echo no.
 on_error
fi

echo -n 'testing for libao to support mapping matrix... '
cat > tests.c << EOF
#include <ao/ao.h>
#include <ao/plugin.h>
int main (void) { ao_device dev = {.output_matrix = ""}; return 0; }
EOF

$CC $AO_LIBS $AO_CFLAGS -o tests tests.c 2> /dev/null
R="$?"
./tests 2> /dev/null

HAVE_MATRIX=false

if [ "$R" = '0' -a "$?" = '0' ]
then
 echo yes
 HAVE_MATRIX=true
else
 echo no.
fi

check_libroar;

rm -f tests tests.c

echo creating Makefile.conf...
{
 echo "CC=$CC"
 echo "INSTALL_DIR=$INSTALL_DIR"
 echo
 echo "AO_CFLAGS=$AO_CFLAGS"
 echo "AO_LIBS=$AO_LIBS"
 echo
 $HAVE_MATRIX && echo "HAVE_MATRIX=-DHAVE_MATRIX"
} > Makefile.conf

true; # needed so return value is correct

#ll
