# Your system. Expands to 'GNU/Linux', 'Cygwin' or 'Msys'
SYSTEM = $(shell uname)
ifneq (, $(findstring Darwin, $(SYSTEM)))
  SYSTEM=Linux
  SUBSYSTEM=Darwin
else
  SYSTEM = $(shell uname -o)
endif

# Module name
MODULE = readline
VERSION = 1.0

# Objects
OBJECTS := lreadline.o

# Libraries
LIBRARIES := readline history

# Lua scripts
LUAS :=
 
# Documentation
DOCUMENTS :=

# ====================================================== 
# You probably do not need to change anything below 

# Lua Settings
# - Lua version to use
LV:=5.2
LX:=52

# - Lua development headers and libraries
ifneq (, $(findstring Msys, $(SYSTEM)))
  LUA=/c/usr/local
  LUABIN=$(LUA)/bin/lua$(LX).exe
  LUADOC = ldoc
else
  LUADOC = luadoc
  ifneq (, $(findstring Linux, $(SYSTEM)))
    ifneq (, $(findstring Darwin, $(SUBSYSTEM)))
      LUA=/usr/local
      LUABIN=$(LUA)/bin/lua$(LX)
    else
      LUA = /usr
      LUABIN=$(LUA)/bin/lua
    endif
  else
    LUA=/usr/local
  endif
endif

# Distribution stuff
DISTNAME=$(MODULE)-$(VERSION)
ifneq (, $(findstring Msys, $(SYSTEM)))
  EXPORTDIR=m:/myexports
  DISTARCH=$(DISTNAME).zip
else
  EXPORTDIR=$(HOME)/exports
  DISTARCH=$(DISTNAME).tar.gz
endif

# Installation Directories
ifneq (, $(findstring Msys, $(SYSTEM)))
  INSTALL_ROOT=c:/usr/local
else
  INSTALL_ROOT=/usr/local
endif
INSTALL_SHARE=$(INSTALL_ROOT)/share/lua/$(LV)
INSTALL_LIB=$(INSTALL_ROOT)/lib/lua/$(LV)
INSTALL_DOC=$(INSTALL_ROOT)/share/doc

# Installation tools
ifneq (, $(findstring Linux, $(SYSTEM)))
  INSTALL = install -p
else
  INSTALL = install.exe -p
endif
INSTALL_EXEC = $(INSTALL) -m 0755 
INSTALL_DATA = $(INSTALL) -m 0644
MKDIR = mkdir -p
RM = rm -rf
RMTEMP = find . -name "*~" | xargs $(RM)
ifneq (,$(findstring Msys, $(SYSTEM)))
  GIT =  c:/msysgit/msysgit/bin/git.exe
else
  GIT =  git
endif
GZIP = gzip

# Compat-5.1 location
COMPAT=compat-5.1r5

# Lua specifics
DEFCOMPAT=-DCOMPAT_H
ifneq (, $(findstring Linux, $(SYSTEM)))
  ifneq (, $(findstring Darwin, $(SUBSYSTEM)))
    LUAINC=-I$(LUA)/include/lua/$(LV)
    LUALIB=-L$(LUA)/lib
  else
    LUAINC=-I$(LUA)/include/lua5.1
    LUALIB=-L$(LUA)/lib_64
  endif
else
  ifneq (, $(findstring Msys, $(SYSTEM)))
    LUAINC=-I$(LUA)/include/
    LUALIB=-L$(LUA)/lib
  else
    LUAINC=-I$(LUA)/include
    LUALIB=-L$(LUA)/lib
  endif
endif

# Platform specific differences
ifneq (, $(findstring Linux, $(SYSTEM)))
  ifneq (, $(findstring Darwin, $(SUBSYSTEM)))
    # Linux: need not to link againt Lua libraries
    SOEXT := so
    PIC :=-fPIC
    LIBDIR+=-L/opt/local/lib
    INCDIR+=-I/opt/local/include 
    LDFLAGS=$(OPT) -bundle -undefined dynamic_lookup $(LUALIB) $(LIBDIR)
  else
    # Linux: need not to link againt Lua libraries
    SOEXT := so
    LIBS+=
    PIC :=-fPIC
    LDFLAGS=$(OPT) -shared $(LUALIB)$(LIBDIR)
  endif 
else
  # Cygwin/Mingw: need to link against Lua libraries
  ifneq (, $(findstring Msys, %(SYSTEM)))
    SOEXT := dll
    LIBS+=-llua$(LX)
    LIBDIR+=-L/c/usr/lib
    INCDIR+=-I/c/usr/include
    PIC :=
  else
    SOEXT := dll
    LIBS+=-llua$(LX)
    LIBDIR+=-L/mingw64/lib
    INCDIR+=-I/usr/include -I/mingw64/include
    PIC :=
  endif
  CFLAGS=-mwin32
  LDFLAGS=$(OPT) -mwin32 -shared $(LUALIB) $(LIBDIR)
endif

# Set default build type
BUILD ?= ./Release

# Compiler and Linker
DEF=$(DEFCOMPAT) -DVERSION='"$(VERSION)"'
CC=gcc
LD=gcc
ifneq (, $(findstring Release, $(BUILD)))
  OPT := -O2
else
  OPT := -g
endif
WARN := -pedantic -Wall
CFLAGS+=$(LUAINC) $(INCDIR) -I$(COMPAT) $(DEF) $(WARN) $(OPT) $(PIC)

