#!/bin/sh

# This configure script written by Brian Callahan <bcallah@openbsd.org>
# and released into the Public Domain.

Makefile() {
cat << EOF > Makefile
# This Makefile automatically generated by configure.

BINDIR =	$bindir
MANDIR =	$mandir

DMD =		$dmd
DFLAGS =	$dflags

PROG =	repology

all:
	\${DMD} \${DFLAGS} $oflag \${PROG}.d

install:
	install -c -s -m 755 \${PROG} \${DESTDIR}\${BINDIR}
	install -c -m 444 \${PROG}.1 \${DESTDIR}\${MANDIR}/man1

uninstall:
	rm -f \${BINDIR}/\${PROG} \${MANDIR}/man1/\${PROG}.1

test:
	@echo No tests.

clean:
	rm -f \${PROG} \${PROG}.o \${PROG}.core

distclean: clean
	rm -f Makefile
EOF
}

dmdcheck() {
  for compiler in "$DMD" ldc2 gdc dmd ; do
cat << EOF > conftest.d
void main(){}
EOF
  ($compiler --version | grep -q "LDC - the LLVM D compiler") > /dev/null 2>&1
  if [ $? -eq 0 ] ; then
    is="ldc"
  else
    ($compiler --version | grep -q "Free Software Foundation") > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
      is="gdc"
    else
      is="dmd"
    fi
  fi
  if [ $specifieddflags -eq 0 ] ; then
    case "$is" in
      "dmd")
        dflags="-O -release -inline"
        ;;
      "gdc")
        dflags="-O2 -pipe -frelease -finline"
        oflag="-o repology"
        ;;
      "ldc")
        dflags="-O -release"
        ;;
    esac
  fi
  if [ "x$is" = "xgdc" ] ; then
    $compiler $dflags -o conftest conftest.d > /dev/null 2>&1
  else
    $compiler $dflags conftest.d > /dev/null 2>&1
  fi
  if [ $? -eq 0 ] ; then
    rm -f conftest conftest.o conftest.d
    dmd="$compiler"
    return 0
  else
    rm -f conftest conftest.o conftest.d
  fi
  done
  return 1
}

# Option variables
if [ ! -z "$PREFIX" ] ; then
  prefix="$PREFIX"
else
  prefix="/usr/local"
fi

bindirset=0
mandirset=0
specifieddflags=0
bindir="$prefix/bin"
mandir="$prefix/share/man"

# Options
for opt
do
  case "$opt" in
    --prefix=*)
      prefix=${opt#*=}
      if [ $bindirset -eq 0 ] ; then
        bindir="$prefix/bin"
      fi
      if [ $mandirset -eq 0 ] ; then
        mandir="$prefix/share/man"
      fi
      ;;
    --bindir=*)
      bindir=${opt#*=}
      bindirset=1
      ;;
    --mandir=*)
      mandir=${opt#*=}
      mandirset=1
      ;;
    --dmd=*)
      DMD=${opt#*=}
      ;;
    --dflags=*)
      dflags=${opt#*=}
      specifieddflags=1
      ;;
    --help|-h)
      echo "Usage: configure [options]"
      echo ""
      echo "Options:"
      printf "  --help or -h            "
      echo "Display this help message"
      printf "  --prefix=PREFIX         "
      echo "Top level install directory is PREFIX [$prefix]"
      printf "  --bindir=BINDIR         "
      echo "Install executable to BINDIR [$bindir]"
      printf "  --mandir=MANDIR         "
      echo "Install manual pages to MANDIR [$mandir]"
      printf "  --dmd=DMD               "
      echo "Use specified D compiler [default=autodetect]"
      printf "  --dflags=DFLAGS         "
      echo "Use specified DFLAGS [default=autodetect]"
      exit 1
      ;;
    *)
      ;;
  esac
done

printf "checking for D compiler... "
dmdcheck
if [ $? -ne 0 ] ; then
  echo "not found"
  echo "error: Please install a D compiler and re-run configure"
  exit 1
else
  echo "$dmd"
fi

printf "creating Makefile... "
Makefile
echo "done"
