#!/bin/sh

DESTDIR=
PREFIX=usr/local
debian=
plain_compatible=false

while [ $# -gt 0 ]
do
  case $1 in
    --help)
       cat 1>&2 <<EOF
Usage: $0 [options]

Options:
  --help         This message
  --debian       Configure to build debian package
  --compat[ible] Bibliography style 'plainnat' identical to standard
  --prefix==     Set installation prefix [default $PREFIX]
EOF
      exit 0
      ;;
   --prefix=*) PREFIX="`echo $2 | sed 's/.*=//'`" ; shift ; shift ;;
   --debian) debian=1 ; PREFIX=usr ; shift ;;
   --compat|--compatible) plain_compatible=true ; shift ;;
   *) echo "Usage: $0 -- help"; exit 1 ;;
  esac
done
      
######################################################################33
#
# search for lua

CF=
LF=

if [ -x "`which pkg-config 2>/dev/null`" ]; then

  CF="`pkg-config --cflags lua5.1`"
  LF="`pkg-config --libs   lua5.1`"

  if [ -z "$CF" ]; then
    echo "Don't know how to find include files for Lua 5.1"; exit 1
  fi

  if [ -z "$LF" ]; then
    echo "Don't know how to find libraries for Lua 5.1"; exit 1
  fi

fi

LUAI="/usr/include/lua5.1 /usr/local/include/lua5.1 /usr/include /usr/local/include"
LUAI="$LUAI /usr/local/lua5.1/include /usr/local/lua/include /usr/include/lua"

LIBS="/lib /usr/lib /usr/local/lib"

lua51=
lua40=
lua50=

if [ -z "$CF" ]; then
  for i in $LUAI; do
    if [ -r $i/lua.h ] && grep 'LUA_VERSION.* 5\.1' $i/lua.h > /dev/null; then
      CF="-I$i"
      break
    elif [ -r $i/lua.h ] && grep 'LUA_VERSION.* 5\.0' $i/lua.h > /dev/null; then
      lua50=true
    elif [ -r $i/lua.h ] && grep 'LUA_VERSION.* 4\.0' $i/lua.h > /dev/null; then
      lua40=true
    fi
  done
fi

if [ -z "$LF" ]; then
  L1=
  for i in $LIBS; do
    if [ -r $i/liblua5.1.so ]; then
      LF="-L$i"
      L1="-lliblua5.1"
      break
    fi
  done
  if [ -z "$L1" ]; then
    for i in $LIBS; do
      if [ -r $i/liblua.so ]; then
        if [ "x$LF" != "x-L$i" ]; then LF="$LF -L$i"; fi
        L1="-lliblua"
        break
      fi
    done
  fi
  if [ -n "$L1" ]; then
    LF="$LF $L1"
  else
    LF=
  fi
fi

if [ -z "$CF" ]; then
  echo "Don't know how to find include files for Lua 5.0"; exit 1
fi

if [ -z "$LF" ]; then
  echo "Don't know how to find libraries for Lua 5.0"
  if [ -n "$lua40" ]; then echo "(I did find an include file for Lua 4.0)"; fi
  if [ -n "$lua40" ]; then echo "(I did find an include file for Lua 4.0)"; fi
  if [ -n "$lua51" ]; then echo "(I did find an include file for Lua 5.1, but nbibtex requires Lua 5.0)"; fi
  exit 1
fi

echo "Lua: OK"

################################################################

if [ -z "$debian" ]; then
  setdest="DESTDIR=$DESTDIR"
else
  setdest=
fi

dollar='$'

cat <<EOF > Makefile
$setdest
SHARE=$dollar(DESTDIR)/$PREFIX/share/nbibtex
BIN=$dollar(DESTDIR)/$PREFIX/bin
MAN1=$dollar(DESTDIR)/$PREFIX/share/man/man1
EOF

cat <<EOF >> Makefile
CFLAGS=$CF -DSHARE='"$dollar(SHARE)"'
LDFLAGS=$LF

EOF

cat Makefile.in >> Makefile

cat <<EOF - bibtex.lua.in > bibtex.lua
local config = { nbs = "$SHARE" }
EOF

if [ $plain_compatible = false ]; then
  compatibility='Two of these styles\
.RB ( abbrvnat \
and\
.BR unsrtnat )\
are completely compatible with the corresponding \\*(OB styles;\
the third style \
.RB ( plainnat ) \
has been changed to more closely follow   \
the Chicago Manual of Style. \
A compatible version is available as style \
.BR plainnatc .'
else
  compatibility='These styles \
.RB ( plainnat , \
.BR abbrvnat , \
and \
.BR unsrtnat ) \
are completely compatible with the corresponding \\*(OB styles. \
An improved version of the "plain" style, \
which more closely follows the  Chicago Manual of Style, \
is available as style \
.BR plainnatx .' 
fi


sed -e "s@<NBSDIR>@$DESTDIR/$PREFIX/share/nbibtex@g" \
    -e "s@<compatibility>@$compatibility@" \
  < nbibtex.1.in > nbibtex.1


cat <<EOF - natbib.nbs.in > natbib.nbs
local plain_compatible = $plain_compatible
EOF

