#!/bin/sh
#
# Builds acdc executable (with utf8 support, if possible). May get called by
# advbld, in which case CC is forced to the CC setting in advbld (which is
# by default cc, not gcc!).
#
CC="gcc"
defs=
while [ $# != 0 ]; do
  case "$1" in
    -x) set -x ;;
    -d|--d*) defs="$defs -DDEBUG" ;;
    -D) shift; defs="$defs -D$1" ;;
    -D*) defs="$defs $1" ;;
    -cc) shift; CC=$1 ;;
    *)
      cat <<EOT
Usage: $0 [-x] [-d] [-cc <cc>] [-D <symbol>]
  -x            show commands being executed
  -d            add -DDEBUG to the build command line
  -cc <cc>      define <cc> as the compiler (default is gcc)
  -D <symbol>   add -DNAME to the build command line
EOT
    exit 1 ;;
  esac
  shift
done
case "$(uname -m)" in
  *64*) arch=64 ;;
  *) arch=32 ;;
esac
[ "$CC" = gcc ] && CC='gcc -g -gdwarf-2 -g3'
list=$(ls -1 *.c | grep -v -e bad -e good)
[ ! -r ../lib$arch/libutf8proc.a ] && \
(
  echo Building utf8proc library...
  cd ../utf8proc/;
  make clean >/dev/null
  CFLAGS="-O2 -m$arch" LDFLAGS=-m$arch make libutf8proc.a >/dev/null
  cp libutf8proc.a utf8proc.h ../lib$arch/
)
if [ -r ../lib$arch/libutf8proc.a ]; then
  utf8=-DHAVE_UTF8
else
  utf8=
  echo "***WARNING** Building acdc with no UTF8 support"
fi
$CC $list $d $defs $utf8 -o acdc -I ../utf8proc -L ../lib$arch -lutf8proc
