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

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

ifeq ($(UNAME),Darwin)
	CCOPT = -I.   -I$(INST_PREFIX)/include/hiredis -I/usr/local/include/hiredis -I/usr/include/hiredis -fno-strict-aliasing
	SOEXT := dylib
	OPT_SOLINK := -dynamiclib -Wl,-undefined -Wl,dynamic_lookup
else
	CCOPT = -I.   -I$(INST_PREFIX)/include/hiredis -I/usr/local/include/hiredis -I/usr/include/hiredis
	SOEXT := so
	OPT_SOLINK := -shared -dynamiclib
endif

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

INST_PREFIX = /usr/local

LDOPT =  -L$(INST_PREFIX)/lib -L/usr/local/lib

CFLAGS = -Wall -O2 $(CCOPT)

AZ_CCOPT = -Wall -O2 $(CCOPT) -I$(INST_PREFIX)/include/hiredis -I/usr/local/include/hiredis -I/usr/include/hiredis
AZPC_LIBS = hiredis
AZPC_OBJS = putil.o
AZ_LIB_EXT = $(INST_PREFIX)/lib/azprolog/ext
AZ_LIB_OBJ = $(INST_PREFIX)/lib/azprolog/obj

LDLIBS=$(LDOPT) -lhiredis

EXEC_TARGETS = redis.$(SOEXT) lib_redis.a redis azredis check

default: redis.$(SOEXT) lib_redis.a

redis: redis_userfile.c
	$(CC) $(CFLAGS) -c $<
	$(CC) -o $@ redis_userfile.o $(AZ_LIB_OBJ)/azpi.o $(AZ_LIB_OBJ)/curses.o -L$(AZ_LIB_EXT) -lazp -l_redis -lhiredis -lcurses -lm -ldl

azredis: redis.c putil.o
	azpc -p $< /i /e $@ /curses  /ccopt "$(AZ_CCOPT)"  /link_opt2 "$(AZPC_OBJS)" /lib $(AZPC_LIBS)
	rm -f *.o

redis.$(SOEXT): redis.c putil.c
	$(CC) $(CFLAGS) -fPIC -o redis.o -c redis.c
	$(CC) $(CFLAGS) -fPIC -o putil.o -c putil.c
	$(CC) $(OPT_SOLINK) -o $@ redis.o putil.o -lc $(LDLIBS)
	chmod 644 redis.$(SOEXT)
	rm -f redis.o putil.o

lib_redis.a: redis.c putil.c
	$(CC) $(CFLAGS) -o redis.o -c redis.c
	$(CC) $(CFLAGS) -o putil.o -c putil.c
	ar rv $@ redis.o
	ar rv $@ putil.o
	ranlib $@

redis.o: redis.c putil.h

putil.o: putil.c putil.h

check: check.c

example: example.c


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

install:
	cp -p redis.$(SOEXT) lib_redis.a $(AZ_LIB_EXT)

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