LIBS = -lslang -lsupp -lintl -liconv -lgpm
LIBTCL = -ltcl8.4

CC = gcc.exe
CPP = gcc.exe -E
CFLAGS = -g -O2
CPPFLAGS = -D_GNU_SOURCE -I/usr/include/slang  

SHLIBFLAGS= -Wl,--version-script,newt.0.52.ver
VERSION = 0.52.10
TAG = r$(subst .,-,$(VERSION))
SONAME = 0.52

PYTHONVERS = python*
WHIPTCLSO = 

PROGS = test whiptail testgrid testtree showchars showkey
TESTOBJS = test.o testgrid.o testtree.o showchars.o showkey.o
NDIALOGOBJS = whiptail.o dialogboxes.o
WHIPTCLOBJS = shared/whiptcl.o shared/dialogboxes.o
LIBNEWT = libnewt.a
LIBNEWTSH = libnewt.so.$(VERSION)
LIBNEWTSONAME = libnewt.so.$(SONAME)
LIBOBJS = newt.o button.o form.o checkbox.o entry.o label.o listbox.o \
          scrollbar.o textbox.o scale.o grid.o windows.o buttonbar.o \
	  checkboxtree.o

SHCFLAGS =

prefix = /dev/env/DJDIR
includedir = ${prefix}/include
exec_prefix = ${prefix}
libdir = ${exec_prefix}/lib
bindir = ${exec_prefix}/bin
datadir = ${prefix}/share
mandir = ${prefix}/share/man
man1dir = $(mandir)/man1

#--------------------------------------

SOURCES = $(subst .o,.c,$(TESTOBJS) $(NDIALOGOBJS) $(LIBOBJS))

SHAREDDIR = shared
SHAREDOBJS = $(patsubst %,$(SHAREDDIR)/%, $(LIBOBJS))

ifeq (.depend,$(wildcard .depend))
TARGET=$(PROGS)
else
TARGET=depend $(PROGS)
endif

all:	$(TARGET)

test:	test.o $(LIBNEWT)
	$(CC) -g -o test test.o $(LIBNEWT) $(LIBS)

testgrid:	testgrid.o $(LIBNEWT)
	$(CC) -g -o testgrid testgrid.o $(LIBNEWT) $(LIBS)

testtree:	testtree.o $(LIBNEWT)
	$(CC) -g -o testtree testtree.o $(LIBNEWT) $(LIBS)

showchars:	showchars.o $(LIBNEWT)
	$(CC) -g -o showchars showchars.o $(LIBNEWT) $(LIBS)

showkey:	showkey.o $(LIBNEWT)
	$(CC) -g -o showkey showkey.o $(LIBNEWT) $(LIBS)

_snackmodule.so:   snackmodule.c
	for ver in $(PYTHONVERS) ; do \
	    	mkdir -p $$ver ;\
	        $(CC) $(CFLAGS) -I/usr/include/$$ver $(SHCFLAGS) -c -o $$ver/snackmodule.o snackmodule.c ;\
		$(CC) $(SHCFLAGS) -o $$ver/_snackmodule.so $$ver/snackmodule.o -L .  -lnewt -lslang ;\
	done
	touch $@

whiptail: $(NDIALOGOBJS) $(LIBNEWT)
	$(CC) -g -o whiptail $(NDIALOGOBJS) -L . -lnewt $(LIBS) -lpopt

whiptcl.so: $(WHIPTCLOBJS) $(LIBNEWTSH)
	$(CC) $(SHCFLAGS) -o whiptcl.so $(WHIPTCLOBJS) -L . -lnewt  $(LIBTCL) -lslang -lpopt -lm

$(LIBNEWT): $(LIBOBJS)
	ar rv $@ $^

newt.o $(SHAREDDIR)/newt.o: newt.c Makefile

veryclean: clean
	rm -f .depend

clean:
	rm -f $(PROGS) *.o $(LIBNEWT) core $(LIBNEWTSH)  \
		$(SHAREDDIR)/*.o *.so*

depend:
	$(CPP) $(CFLAGS) $(CPPFLAGS) -M $(SOURCES) > .depend

$(SHAREDDIR):
	mkdir -p $(SHAREDDIR)

sharedlib: $(LIBNEWTSH)

$(LIBNEWTSH): $(SHAREDDIR) $(SHAREDOBJS)
	$(CC) -o $(LIBNEWTSH) $(SHLIBFLAGS) -Wl,-soname,$(LIBNEWTSONAME) $(SHAREDOBJS) $(LIBS)
	ln -fs $(LIBNEWTSONAME) libnewt.so
	ln -fs $(LIBNEWTSH) $(LIBNEWTSONAME)

$(SHAREDDIR)/%.o : %.c
	$(CC) $(SHCFLAGS) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

install: $(LIBNEWT) install-sh whiptail
	[ -d $(instroot)/$(bindir) ] || install -m 755 -d $(instroot)/$(bindir)
	[ -d $(instroot)/$(libdir) ] || install -m 755 -d $(instroot)/$(libdir)
	[ -d $(instroot)/$(includedir) ] || install -m 755 -d $(instroot)/$(includedir)
	[ -d $(instroot)/$(man1dir) ] || install -m 755 -d $(instroot)/$(man1dir)
	install -m 644 newt.h $(instroot)/$(includedir)
	install -m 644 $(LIBNEWT) $(instroot)/$(libdir)
	install -m 755 whiptail $(instroot)/$(bindir)
	install -m 644 whiptail.1 $(instroot)/$(man1dir)
	make -C po datadir=$(instroot)/$(datadir) install

install-sh: sharedlib $(WHIPTCLSO) _snackmodule.so
	[ -d $(instroot)/$(libdir) ] || install -m 755 -d $(instroot)/$(libdir)
	install -m 755 $(LIBNEWTSH) $(instroot)/$(libdir)
	ln -sf $(LIBNEWTSONAME) $(instroot)/$(libdir)/libnewt.so
	ln -sf $(LIBNEWTSH) $(instroot)/$(libdir)/$(LIBNEWTSONAME)
	[ -n "$(WHIPTCLSO)" ] && install -m 755 whiptcl.so $(instroot)/$(libdir) || :
	for ver in $(PYTHONVERS) ; do \
	   [ -d $(instroot)/$(libdir)/$$ver/site-packages ] || install -m 755 -d $(instroot)/$(libdir)/$$ver/site-packages ;\
	   install -m 755 $$ver/_snackmodule.so $(instroot)/$(libdir)/$$ver/site-packages ;\
	   install -m 644 snack.py $(instroot)/$(libdir)/$$ver/site-packages ;\
	done

Makefile: newt.spec
	echo "You need to rerun ./configure before continuing"
	@exit 1

create-archive: Makefile
	@rm -rf /tmp/newt-$(VERSION)
	@git archive --prefix=newt-$(VERSION)/ $(TAG) | tar x -C /tmp
	@cd /tmp/newt-$(VERSION) && ./autogen.sh && rm -rf autom4te.cache
	@cd /tmp; tar czSpf newt-$(VERSION).tar.gz newt-$(VERSION)
	@rm -rf /tmp/newt-$(VERSION)
	@cp /tmp/newt-$(VERSION).tar.gz .
	@rm -f /tmp/newt-$(VERSION).tar.gz
	@echo " "
	@echo "The final archive is ./newt-$(VERSION).tar.gz."

tag-archive: Makefile
	@git tag $(TAG)

archive: tag-archive create-archive

ifeq (.depend,$(wildcard .depend))
include .depend
endif
