# Makefile
# ex.  > make INST_PREFIX=/usr
#

ifeq ($(OS),Windows_NT)
    UNAME := Windows
else
    UNAME := $(shell uname -s)
endif

NAME = clp
DATE_TAG:=`date +%Y%m%d`

INST_PREFIX = /usr/local

ifeq ($(UNAME),Darwin)
	CCOPT = -I.  -I$(INST_PREFIX)/include -fno-strict-aliasing
	SOEXT := dylib
	OPT_SOLINK := -dynamiclib -Wl,-undefined -Wl,dynamic_lookup
export DYLD_LIBRARY_PATH=../../../source:$(INST_PREFIX)/lib
else
	CCOPT = -I.  -I$(INST_PREFIX)/include
	SOEXT := so
	OPT_SOLINK := -shared -dynamiclib
export LD_LIBRARY_PATH=$(INST_PREFIX)/lib
endif

LDOPT =  -L$(INST_PREFIX)/lib
CFLAGS = -Wall -O2 $(CCOPT)

AZ_LIB_EXT = $(INST_PREFIX)/lib/azprolog/ext
AZ_LIB_OBJ = $(INST_PREFIX)/lib/azprolog/obj

AZPCFLAGS = /message /s_verbos /cc $(CC) /ccopt "$(CCOPT)" /link_opt "$(LDOPT)"

ifeq ($(AZ_DEBUG_MODE),true)
AZPC = azpc_d
else
AZPC = azpc
endif

EXEC_TARGETS = clp.$(SOEXT) lib_clp.a clp azclp


default: clp.$(SOEXT) lib_clp.a

clp: clp_userfile.c
	$(CC) $(CFLAGS) -c $<
	$(CC) -o $@ clp_userfile.o $(AZ_LIB_OBJ)/azpi.o $(AZ_LIB_OBJ)/curses.o -L$(AZ_LIB_EXT) -l_clp -lazp -lcurses -lm -ldl

azclp: clp.c
	$(AZPC) -p $< /i /e $@ /curses  /ccopt "$(CFLAGS)"
	rm -f *.o

clp.$(SOEXT): clp.c
	$(CC) $(CFLAGS) -fPIC -o clp.o -c $<
	$(CC) $(OPT_SOLINK) -o $@ clp.o
	chmod 644 $@

lib_clp.a: clp.c
	$(CC) $(CFLAGS) -o clp.o -c $<
	ar rv $@ clp.o
	ranlib $@

clp.c: clp.pl
	AZProlog=$(INST_PREFIX) $(INST_PREFIX)/bin/$(AZPC) -p $< /ncc $(AZPCFLAGS)

install:
	cp -p clp.$(SOEXT) lib_clp.a $(AZ_LIB_EXT)

clean:
	rm -f clp.c *~ .*~ *.o mkaz userfile.c $(EXEC_TARGETS)

tar:
	cd ..; tar -cvf $(NAME)-$(DATE_TAG).tar $(NAME)
	gzip ../$(NAME)-$(DATE_TAG).tar
