language: c

env:
  global:
    - _COMPILE="libtool --mode=compile --tag=CC gcc"
    - _CFLAGS="-O2 -Wall -DLUA_COMPAT_ALL -DLUA_USE_LINUX"
    - _INSTALL="libtool --mode=install install -p"
    - _LINK="libtool --mode=link --tag=CC gcc"
    - _LIBS="-lm -Wl,-E -ldl -lreadline"

    - prefix=/usr/local
    - bindir=$prefix/bin
    - incdir=$prefix/include
    - libdir=$prefix/lib
  matrix:
    - LUA=lua5.3
    - LUA=lua5.2
    - LUA=lua5.1
    - LUA=luajit


before_install:
  # Put back the links for libyaml, which are missing on recent Travis VMs
  - test -f /usr/lib/libyaml.so ||
    sudo find /usr/lib -name 'libyaml*' -exec ln -s {} /usr/lib \;
  - sudo apt-get install help2man

  # Fetch Lua sources.
  - cd $TRAVIS_BUILD_DIR
  - 'if test lua5.3 = "$LUA"; then
      curl http://www.lua.org/work/lua-5.3.0-rc2.tar.gz | tar xz;
      cd lua-5.3.0;
    fi'
  - 'if test lua5.2 = "$LUA"; then
      curl http://www.lua.org/ftp/lua-5.2.3.tar.gz | tar xz;
      cd lua-5.2.3;
    fi'
  - 'if test lua5.1 = "$LUA"; then
      curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz;
      cd lua-5.1.5;
    fi'

  # Unpack, compile and install Lua.
  - 'if test luajit = "$LUA"; then
      curl http://luajit.org/download/LuaJIT-2.0.3.tar.gz | tar xz;
      cd LuaJIT-2.0.3;
      make && sudo make install;
      for header in lua.h luaconf.h lualib.h lauxlib.h luajit.h lua.hpp; do
        if test -f /usr/local/include/luajit-2.0/$header; then
          sudo ln -s /usr/local/include/luajit-2.0/$header /usr/local/include/$header;
        fi;
      done;
    else
      for src in src/*.c; do
        test src/lua.c = "$src" || test src/luac.c = "$src" || eval $_COMPILE $_CFLAGS -c $src;
      done;
      eval $_LINK -o lib$LUA.la -version-info 0:0:0 -rpath $libdir *.lo;
      sudo mkdir -p $libdir;
      eval sudo $_INSTALL lib$LUA.la $libdir/lib$LUA.la;

      eval $_COMPILE $_CFLAGS -c src/lua.c;
      eval $_LINK -static -o $LUA lua.lo lib$LUA.la $_LIBS;
      sudo mkdir -p $bindir;
      eval sudo $_INSTALL $LUA $bindir/$LUA;

      sudo mkdir -p $incdir;
      for header in lua.h luaconf.h lualib.h lauxlib.h lua.hpp; do
        if test -f src/$header; then
          eval sudo $_INSTALL src/$header $incdir/$header;
        fi;
      done;
    fi'

  # Fetch LuaRocks.
  - cd $TRAVIS_BUILD_DIR
  - 'git clone https://github.com/keplerproject/luarocks.git luarocks-2.2.0'
  - cd luarocks-2.2.0
  - git checkout v2.2.0

  # Compile and install luarocks.
  - if test luajit = "$LUA"; then
      ./configure --lua-suffix=jit;
    else
      ./configure;
    fi
  - 'make build && sudo make install'

  # Tidy up file droppings.
  - cd $TRAVIS_BUILD_DIR
  - rm -rf lua-5.3.0 lua-5.2.3 lua-5.1.5 LuaJIT-2.0.3 luarocks-2.2.0


install:
  # Use Lua 5.3 compatible rocks, where available.
  - 'for rock in @EXTRA_ROCKS@""; do
       if test -z "$rock"; then break; fi;
       if luarocks list | grep "^$rock$" >/dev/null; then continue; fi;
       sudo luarocks install --server=http://rocks.moonscript.org/manifests/gvvaughan $rock;
    done'

  # Fudge timestamps on release branches.
  - 'if test -f configure; then
      test -f aclocal.m4 && touch aclocal.m4;
      sleep 1; touch Makefile.in;
      sleep 1; test -f config.h.in && touch config.h.in;
      sleep 1; touch configure;
    fi'

  # Build from rockspec.
  - export ROCKSPEC=@PACKAGE@-@VERSION@-1.rockspec
  - 'test -f "$ROCKSPEC" || ROCKSPEC=@PACKAGE@-git-1.rockspec'
  - sudo luarocks make $ROCKSPEC LUA="$LUA"

  # Clean up files created by root
  - sudo git clean -dfx
  - sudo rm -rf slingshot /tmp/ldoc


script:
  - test -f configure || ./bootstrap --verbose
  - test -f Makefile || ./configure --disable-silent-rules LUA="$LUA"
  - make
  - make check V=1


# Run sanity checks on CI server, ignoring buggy automakes.
after_success:
  - '{ _assign="=";
       if grep local-checks-to-skip build-aux/sanity-cfg.mk >/dev/null; then
         _assign="+=";
       fi;
       printf "local-checks-to-skip %s sc_vulnerable_makefile_CVE-2012-3386\n" "$_assign";
     } >> build-aux/sanity-cfg.mk'
  - 'make syntax-check || : this will usually fail on the release branch'
