#!/bin/sh

# A basic configure script for LuaRocks.
# Not doing any fancy shell stuff here to keep good compatibility.

# Defaults

PREFIX="/usr/local"
SYSCONFDIR="$PREFIX/etc/luarocks"
ROCKS_TREE="$PREFIX"
LUA_SUFFIX=""
LUA_DIR="/usr"
LUA_BINDIR="/usr/bin"
LUA_INCDIR="/usr/include"
LUA_LIBDIR="/usr/lib"
LUA_VERSION="5.1"
MULTIARCH_SUBDIR=""

# ----------------------------------------------------------------------------
# FUNCTION DEFINITIONS
# ----------------------------------------------------------------------------

# Help

show_help() {
cat <<EOF
Configure LuaRocks.

--help                      This help.
--prefix=DIR                Prefix where LuaRocks should be installed.
                            Default is $PREFIX
--sysconfdir=DIR            Location where the config file should be installed.
                            Default is \$PREFIX/etc/luarocks

Where to install files installed by rocks, to make the accessible to Lua and
your \$PATH. Beware of clashes between files installed by LuaRocks and by your
system's package manager.

--rocks-tree=DIR            Root of the local tree of installed rocks.
                            Default is \$PREFIX

--lua-version=VERSION       Use specific Lua version: 5.1, 5.2, or 5.3
                            Default is "$LUA_VERSION"
--lua-suffix=SUFFIX         Versioning suffix to use in Lua filenames.
                            Default is "$LUA_SUFFIX" (lua$LUA_SUFFIX...)
--with-lua=PREFIX           Use Lua from given prefix.
                            Default is auto-detected (the parent directory of \$LUA_BINDIR).
--with-lua-bin=DIR          You can also specify Lua's bin dir.
                            Default is the directory of the auto-detected Lua interpreter,
                            or \$LUA_DIR/bin if --with-lua is used.
--with-lua-include=DIR      You can also specify Lua's includes dir.
                            Default is \$LUA_DIR/include
--with-lua-lib=DIR          You can also specify Lua's libraries dir.
                            Default is \$LUA_DIR/lib
--with-downloader=TOOL      Which tool to use as a downloader.
                            Valid options are: curl, wget.
                            Default is to auto-detect.
--with-md5-checker=TOOL     Which tool to use as a downloader.
                            Valid options are: md5sum, openssl
                            Default is to auto-detect.
--versioned-rocks-dir       Use a versioned rocks dir, such as
                            \$PREFIX/lib/luarocks/rocks-$LUA_VERSION/.
                            Default is to auto-detect the necessity.
--force-config              Use a single config location. Do not use the
                            \$LUAROCKS_CONFIG variable or the user's home
                            directory. Useful to avoid conflicts when LuaRocks
                            is embedded within an application.
EOF
}

# Helper functions

find_program() {
   path="$PATH"
   item="`echo "$path" | sed 's/\([^:]*\):.*/\1/'`"
   path="`echo "$path" | sed -n 's/[^:]*::*\(.*\)/\1/p'`"
   found="no"
   while [ -n "$item" ]
   do
      if [ -f "$item/$1" ]
      then
         found="yes"
         break
      fi
      item="`echo "$path" | sed 's/\([^:]*\):.*/\1/'`"
      path="`echo "$path" | sed -n 's/[^:]*::*\(.*\)/\1/p'`"
   done
   if [ "$found" = "yes" ]
   then
      echo "$item"
   else
      echo ""
   fi
}

die() {
   echo "$*"
   echo
   echo "configure failed."
   echo
   exit 1
}

find_helper() {
   explanation="$1"
   shift
   tried="$*"
   while [ -n "$1" ]
   do
      found=`find_program "$1"`
      if [ -n "$found" ]
      then
         echo "$1 found at $found"
         HELPER=$1
         return
      fi
      shift
   done
   echo "Could not find a $explanation. Tried: $tried."
   die "Make sure one of them is installed and available in your PATH."
}

case `echo -n x` in
-n*) echo_n_flag='';;
*)   echo_n_flag='-n';;
esac

echo_n() {
   echo $echo_n_flag "$*"
}

# ----------------------------------------------------------------------------
# MAIN PROGRAM
# ----------------------------------------------------------------------------

# Parse options

while [ -n "$1" ]
do
   value="`echo $1 | sed 's/[^=]*.\(.*\)/\1/'`"
   key="`echo $1 | sed 's/=.*//'`"
   if `echo "$value" | grep "~" >/dev/null 2>/dev/null`
   then
      echo
      echo '*WARNING*: the "~" sign is not expanded in flags.'
      echo 'If you mean the home directory, use $HOME instead.'
      echo
   fi
   case "$key" in
   --help)
      show_help
      exit 0
      ;;
   --prefix)
      [ -n "$value" ] || die "Missing value in flag $key."
      PREFIX="$value"
      PREFIX_SET=yes
      ;;
   --sysconfdir)
      [ -n "$value" ] || die "Missing value in flag $key."
      SYSCONFDIR="$value"
      SYSCONFDIR_SET=yes
      ;;
   --rocks-tree)
      [ -n "$value" ] || die "Missing value in flag $key."
      ROCKS_TREE="$value"
      ROCKS_TREE_SET=yes
      ;;
   --force-config)
      FORCE_CONFIG=yes
      ;;
   --versioned-rocks-dir)
      VERSIONED_ROCKS_DIR=yes
      ;;
   --lua-suffix)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_SUFFIX="$value"
      LUA_SUFFIX_SET=yes
      ;;
   --lua-version|--with-lua-version)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_VERSION="$value"
      [ "$LUA_VERSION" = "5.1" -o "$LUA_VERSION" = "5.2" -o "$LUA_VERSION" = "5.3" ] || die "Invalid Lua version in flag $key."
      LUA_VERSION_SET=yes
      ;;
   --with-lua)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_DIR="$value"
      LUA_DIR_SET=yes
      ;;
   --with-lua-bin)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_BINDIR="$value"
      LUA_BINDIR_SET=yes
      ;;
   --with-lua-include)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_INCDIR="$value"
      LUA_INCDIR_SET=yes
      ;;
   --with-lua-lib)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_LIBDIR="$value"
      LUA_LIBDIR_SET=yes
      ;;
   --with-downloader)
      [ -n "$value" ] || die "Missing value in flag $key."
      case "$value" in
      wget|curl) LUAROCKS_DOWNLOADER="$value" ;;
      *) die "Invalid option: $value. See --help." ;;
      esac
      LUAROCKS_DOWNLOADER_SET=yes
      ;;
   --with-md5-checker)
      [ -n "$value" ] || die "Missing value in flag $key."
      case "$value" in
      md5sum|openssl|md5) LUAROCKS_MD5CHECKER="$value" ;;
      *) die "Invalid option: $value. See --help." ;;
      esac
      LUAROCKS_MD5CHECKER_SET=yes
      ;;
   *)
      die "Error: Unknown flag: $1"
      ;;
   esac
   shift
done

if [ "$PREFIX_SET" = "yes" -a ! "$SYSCONFDIR_SET" = "yes" ]
then
   if [ "$PREFIX" = "/usr" ]
   then SYSCONFDIR=/etc/luarocks
   else SYSCONFDIR=$PREFIX/etc/luarocks
   fi
fi


if [ "$PREFIX_SET" = "yes" -a ! "$ROCKS_TREE_SET" = "yes" ]
then
   ROCKS_TREE=$PREFIX
fi

detect_lua_version() {
   detected_lua=`$1 -e 'print(_VERSION:sub(5))' 2> /dev/null`
   if [ "$detected_lua" = "5.1" -o "$detected_lua" = "5.2" -o "$detected_lua" = "5.3" ]
   then
      echo "Lua version detected: $detected_lua"
      if [ "$LUA_VERSION_SET" != "yes" ]
      then
         LUA_VERSION=$detected_lua
      elif [ "$LUA_VERSION" != "$detected_lua" ]
      then
         die "This clashes with the value of --lua-version. Please check your configuration."
      fi
   fi
}

search_interpreter() {
   suffix="$1"
   if [ "$LUA_BINDIR_SET" = "yes" ]
   then
      find_lua="$LUA_BINDIR"
   elif [ "$LUA_DIR_SET" = "yes" ]
   then
      if [ -f "$LUA_DIR/bin/lua$suffix" ]
      then
         find_lua="$LUA_DIR/bin"
      fi
   else
      find_lua=`find_program lua$suffix`
   fi
   if [ -n "$find_lua" -a -x "$find_lua/lua$suffix" ]
   then
      echo "Lua interpreter found: $find_lua/lua$suffix..."
      LUA_SUFFIX=$suffix
      detect_lua_version "$find_lua/lua$LUA_SUFFIX"
      return 0
   fi
   return 1
}

if [ "$LUA_SUFFIX_SET" != "yes" ]
then
   if [ "$LUA_VERSION_SET" = "yes" -a "$LUA_VERSION" = "5.1" ]
   then
      suffixes="5.1 51 -5.1 -51"
   elif [ "$LUA_VERSION_SET" = "yes" -a "$LUA_VERSION" = "5.2" ]
   then
      suffixes="5.2 52 -5.2 -52"
   elif [ "$LUA_VERSION_SET" = "yes" -a "$LUA_VERSION" = "5.3" ]
   then
      suffixes="5.3 53 -5.3 -53"
   else
      suffixes="5.3 53 -5.3 -53 5.2 52 -5.2 -52 5.1 51 -5.1 -51"
   fi
   lua_interp_found=no
   for suffix in "" `echo $suffixes`
   do
      search_interpreter "$suffix" && {
         lua_interp_found=yes
         break
      }
   done
fi

if [ "$LUA_DIR_SET" != "yes" ]
then
   if [ -z "$find_lua" ]
   then
      echo_n "Looking for Lua... "
      find_lua=`find_program lua$LUA_SUFFIX`
   fi

   if [ -n "$find_lua" ]
   then
      LUA_DIR=`dirname $find_lua`
      LUA_BINDIR="$find_lua"
      echo "lua$LUA_SUFFIX found in \$PATH: $find_lua"
   else
      echo "lua$LUA_SUFFIX not found in \$PATH."
      die "You may want to use the flags --with-lua, --with-lua-bin and/or --lua-suffix. See --help."
   fi
fi

if [ "$LUA_INCDIR_SET" != "yes" ]
then
   LUA_INCDIR="$LUA_DIR/include"
fi

if [ "$LUA_LIBDIR_SET" != "yes" ]
then
   LUA_LIBDIR="$LUA_DIR/lib"
fi

if [ "$LUA_DIR_SET" = "yes" -a "$LUA_BINDIR_SET" != "yes" ]
then
   LUA_BINDIR="$LUA_DIR/bin"
fi

if [ "$lua_interp_found" != "yes" ]
then
   echo_n "Checking Lua interpreter... "
   if [ -x "$LUA_BINDIR/lua$LUA_SUFFIX" ]
   then
      echo "lua$LUA_SUFFIX found in $LUA_BINDIR"
   else
      echo "lua$LUA_SUFFIX not found (looked in $LUA_BINDIR)"
      die "You may want to use the flag --with-lua or --with-lua-bin. See --help."
   fi
fi

echo_n "Checking Lua includes... "
lua_h="$LUA_INCDIR/lua.h"
if [ -f "$lua_h" ]
then
   echo "lua.h found in $lua_h"
else
   v_dir="$LUA_INCDIR/lua/$LUA_VERSION"
   lua_h="$v_dir/lua.h"
   if [ -f "$lua_h" ]
   then
      echo "lua.h found in $lua_h"
      LUA_INCDIR="$v_dir"
   else
      d_dir="$LUA_INCDIR/lua$LUA_VERSION"
      lua_h="$d_dir/lua.h"
      if [ -f "$lua_h" ]
      then
         echo "lua.h found in $lua_h (Debian/Ubuntu)"
         LUA_INCDIR="$d_dir"
      else
         echo "lua.h not found (looked in $LUA_INCDIR, $v_dir, $d_dir)"
         die "You may want to use the flag --with-lua or --with-lua-include. See --help."
      fi
   fi
fi

if [ "$LUAROCKS_DOWNLOADER_SET" != "yes" ]
then
   find_helper "downloader helper program" curl wget fetch
   LUAROCKS_DOWNLOADER=$HELPER
fi

if [ "$LUAROCKS_MD5CHECKER_SET" != "yes" ]
then
   find_helper "MD5 checksum calculator" md5sum openssl md5
   LUAROCKS_MD5CHECKER=$HELPER
fi

echo_n "Configuring for system... "
if uname -s
then
   LUAROCKS_UNAME_S=`uname -s`
else
   die "Could not determine operating system. 'uname -s' failed."
fi
echo_n "Configuring for architecture... "
if uname -m
then
   LUAROCKS_UNAME_M=`uname -m`
else
   die "Could not determine processor architecture. 'uname -m' failed."
fi

for v in 5.1 5.2 5.3; do
  if [ "$v" != "$LUA_VERSION" ]; then
    if [ -e "$PREFIX/share/lua/$v/luarocks/site_config.lua" ]; then
      LUA_OTHER_VERSION="$v"
      break
    fi
  fi
done

LUAROCKS_ROCKS_SUBDIR=/lib/luarocks/rocks
if [ "$VERSIONED_ROCKS_DIR" = "yes" ]
then
   LUAROCKS_ROCKS_SUBDIR=$LUAROCKS_ROCKS_SUBDIR-$LUA_VERSION
   echo "Using versioned rocks dir: $PREFIX$LUAROCKS_ROCKS_SUBDIR"
elif [ -e "$PREFIX/share/lua/$LUA_VERSION/luarocks/site_config.lua" ]
then
   echo "Existing installation detected."
   LUAROCKS_ROCKS_SUBDIR=`grep "LUAROCKS_ROCKS_SUBDIR" "$PREFIX/share/lua/$LUA_VERSION/luarocks/site_config.lua" | sed 's,.*=\[\[\(.*\)\]\],\1,'`
   echo "Using previously configured rocks dir: $PREFIX$LUAROCKS_ROCKS_SUBDIR"
elif [ -n "$LUA_OTHER_VERSION" ]
then
   echo "Existing installation detected for other Lua version ($LUA_OTHER_VERSION)."
   LUAROCKS_ROCKS_SUBDIR=$LUAROCKS_ROCKS_SUBDIR-$LUA_VERSION
   echo "Using versioned rocks dir: $PREFIX$LUAROCKS_ROCKS_SUBDIR"
else
   echo "Using unversioned rocks dir: $PREFIX$LUAROCKS_ROCKS_SUBDIR"
fi

if [ "$LUAROCKS_UNAME_S" = Linux ]
then
   GCC_ARCH=`gcc -print-multiarch 2>/dev/null`
   if [ -n "$GCC_ARCH" -a -d "/usr/lib/$GCC_ARCH" ]
   then
      MULTIARCH_SUBDIR="lib/$GCC_ARCH"
   elif [ -d "/usr/lib64" ]
   then
      # Useful for Fedora systems
      MULTIARCH_SUBDIR="lib64"
   fi
fi

if [ -f config.unix ]; then
   rm -f config.unix
fi

# Write config

echo "Writing configuration..."
echo

rm -f built
cat <<EOF > config.unix
# This file was automatically generated by the configure script.
# Run "./configure --help" for details.

LUA_VERSION=$LUA_VERSION
PREFIX=$PREFIX
SYSCONFDIR=$SYSCONFDIR
ROCKS_TREE=$ROCKS_TREE
LUA_SUFFIX=$LUA_SUFFIX
LUA_DIR=$LUA_DIR
LUA_DIR_SET=$LUA_DIR_SET
LUA_INCDIR=$LUA_INCDIR
LUA_LIBDIR=$LUA_LIBDIR
LUA_BINDIR=$LUA_BINDIR
FORCE_CONFIG=$FORCE_CONFIG
LUAROCKS_UNAME_M=$LUAROCKS_UNAME_M
LUAROCKS_UNAME_S=$LUAROCKS_UNAME_S
LUAROCKS_DOWNLOADER=$LUAROCKS_DOWNLOADER
LUAROCKS_MD5CHECKER=$LUAROCKS_MD5CHECKER
LUAROCKS_ROCKS_SUBDIR=$LUAROCKS_ROCKS_SUBDIR
MULTIARCH_SUBDIR=$MULTIARCH_SUBDIR

EOF

echo "Installation prefix: $PREFIX"
echo "LuaRocks configuration directory: $SYSCONFDIR"
echo "Using Lua from: $LUA_DIR"

make clean > /dev/null 2> /dev/null

echo
echo "Done configuring."
echo "- Type 'make build' and 'make install':"
echo "  to install to $PREFIX as usual."
echo "- Type 'make bootstrap':"
echo "  to install LuaRocks in $PREFIX as a rock."
echo