#!/bin/sh
# Just a simple handwritten configure
rm -f config.mk

host=''
while [ "$#" -gt 0 ]; do
  case "$1" in
  --host=*) host="`echo "$1" | sed -e 's/--host=//'`";;
  esac
  shift
done

printf 'Checking for a C compiler... '
if [ "x$CC" = "x" -a "x$host" != "x" ]; then
  if which "${host}-cc" > /dev/null 2> /dev/null; then CC="${host}-cc"
  elif which "${host}-gcc" > /dev/null 2> /dev/null; then CC="${host}-gcc"
  elif which "${host}-clang" > /dev/null 2> /dev/null; then CC="${host}-clang"
  fi
fi
if [ "x$CC" = "x" ]; then
  if which cc > /dev/null 2> /dev/null; then CC=cc
  elif which gcc > /dev/null 2> /dev/null; then CC=gcc
  elif which clang > /dev/null 2> /dev/null; then CC=clang
  fi
fi
if [ "x$CC" = "x" ]; then echo "None found"; exit 1; fi
echo "$CC"
echo "CC=$CC" >> config.mk

printf 'Checking if we need -liconv for iconv... '
echo '#include <iconv.h>' > iconvtest.c
echo 'int main(){iconv_t x=iconv_open("a","b");return 0;}' >> iconvtest.c
if ! "$CC" iconvtest.c -o iconvtest > /dev/null 2> /dev/null; then
  echo 'LIBS+=-liconv' >> config.mk
  echo yes
else
  echo no
fi
rm -f iconvtest iconvtest.c

printf 'Checking for gtk+-3.0... '
libs="`pkg-config --libs gtk+-3.0 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "GTK_LIBS=${libs}" >> config.mk
  echo "GTK_CFLAGS=`pkg-config --cflags gtk+-3.0`" >> config.mk
  echo yes
else
  echo no
  printf 'Checking for gtk+-2.0... '
  libs="`pkg-config --libs gtk+-2.0 2> /dev/null`"
  if [ "x$libs" != "x" ]; then
    echo "GTK_LIBS=${libs}" >> config.mk
    echo "GTK_CFLAGS=`pkg-config --cflags gtk+-2.0`" >> config.mk
    echo yes
  else
    echo no
  fi
fi

printf 'Checking for libavcodec... '
libs="`pkg-config --libs libavcodec 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "AVCODEC_LIBS=${libs}" >> config.mk
  echo "AVCODEC_CFLAGS=`pkg-config --cflags libavcodec`" >> config.mk
  echo yes
else
  echo no
fi

printf 'Checking for libswscale... '
libs="`pkg-config --libs libswscale 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "SWSCALE_LIBS=${libs}" >> config.mk
  echo "SWSCALE_CFLAGS=`pkg-config --cflags libswscale`" >> config.mk
  echo yes
else
  echo no
fi

printf 'Checking for libavutil... '
libs="`pkg-config --libs libavutil 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "AVUTIL_LIBS=${libs}" >> config.mk
  echo "AVUTIL_CFLAGS=`pkg-config --cflags libavutil`" >> config.mk
  echo yes
else
  echo no
fi

# TODO: Figure out how to mix sound sources so that this doesn't sound horrible with more than one person on mic, having it disabled by default until then
if [ "x${ENABLE_MIC}" != "x" ]; then
printf 'Checking for libswresample... '
libs="`pkg-config --libs libswresample 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "SWRESAMPLE_LIBS=${libs}" >> config.mk
  echo "SWRESAMPLE_CFLAGS=`pkg-config --cflags libswresample`" >> config.mk
  echo yes
else
  echo no
fi

printf 'Checking for libao... '
libs="`pkg-config --libs ao 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "AO_LIBS=${libs}" >> config.mk
  echo "AO_CFLAGS=`pkg-config --cflags ao`" >> config.mk
  echo yes
else
  echo no
fi
fi

printf 'Checking for ncurses... '
libs="`ncursesw5-config --libs 2> /dev/null || ncurses5-config --libs 2> /dev/null`"
if [ "x$libs" != "x" ]; then
  echo "CURSES_LIBS=${libs}" >> config.mk
  echo "CURSES_CFLAGS=`ncursesw5-config --cflags 2> /dev/null || ncurses5-config --cflags 2> /dev/null`" >> config.mk
  echo yes
else
  echo no
fi

printf 'Checking for readline... '
echo '#include <stdio.h>' > readlinetest.c
echo '#include <readline/readline.h>' >> readlinetest.c
echo 'int main(){rl_initialize();return 0;}' >> readlinetest.c
if "$CC" readlinetest.c -lreadline ${libs} -o readlinetest > /dev/null 2> /dev/null; then
  echo "READLINE_LIBS=-lreadline" >> config.mk
  echo yes
else
  echo no
fi
rm -f readlinetest.c readlinetest

echo Done
