#
# Makefile rules (Linux X11 version):
#
#DEBUG		= -g -DDEBUG
CFLAGS		= -O2 -Wall 
RM		= rm -rf
MKDIR		= mkdir -p

LIBRARY		= $(PACKAGE).a
DISTNAME	= $(PACKAGE)-$(VERSION)

INCDIR		= include/
SRCDIR		= src/
PATHLIB32	= lib/
OBJ32DIR	= $(SRCDIR)obj32/
LIBRARIES	= $(PATHLIB32)$(LIBRARY)

ifeq ($(IS_64BIT),true)
	PATHLIB64	= lib64/
	OBJ64DIR	= $(SRCDIR)obj64/
	LIBRARIES+=$(PATHLIB64)$(LIBRARY)
endif

#
# Librerie
#
LIB_HEADERS 	= finestre.h tastiera.h colori.h 

LIB_SORGENTI	= WX11_errors.c WX11_signals.c			\
		WX11_init.c WX11_colors.c WX11_finestre.c 	\
		WX11_font.c WX11_print.c WX11_cmd.c		\
		WX11_events.c WX11_alert.c WX11_menu.c		\
		WX11_input.c WX11_listcmd.c WX11_scroll.c	\
		WX11_timer.c WX11_exit.c WX11_border.c		\
		WX11_listbox.c WX11_combobox.c WX11_checkbox.c	\
		WX11_files.c WX11_viewfile.c 			\
		WX11_gc.c					\
		WX11_imgbmp.c WX11_imgxpm.c WX11_imgjpeg.c WX11_imgpng.c	\
		WX11_utils.c					\
		WX11_sql.c					\
		WX11_debug.c	

LIB_OGGETTI	= $(LIB_SORGENTI:.c=.o)

# Delete the default suffixes
.SUFFIXES:


all: $(LIBRARIES) sub1 sub2
$(PATHLIB64)$(LIBRARY) : $(addprefix $(OBJ64DIR),$(LIB_OGGETTI)) 
	if test ! -d $(PATHLIB64);  then $(MKDIR) $(PATHLIB64); fi
	$(AR) r $(PATHLIB64)$(LIBRARY) $(addprefix $(OBJ64DIR),$(LIB_OGGETTI))
	$(TOUCH) examples/*.c
	$(TOUCH) test/*.c

$(PATHLIB32)$(LIBRARY) : $(addprefix $(OBJ32DIR),$(LIB_OGGETTI)) 
	if test ! -d $(PATHLIB32);  then $(MKDIR) $(PATHLIB32); fi
	$(AR) r $(PATHLIB32)$(LIBRARY) $(addprefix $(OBJ32DIR),$(LIB_OGGETTI))
	$(TOUCH) examples/*.c
	$(TOUCH) test/*.c

$(OBJ64DIR)%.o : $(SRCDIR)%.c $(addprefix $(INCDIR),$(LIB_HEADERS))
	if test ! -d $(OBJ64DIR);  then $(MKDIR) $(OBJ64DIR); fi
	$(CC) -c $(DEBUG) $(CFLAGS) -m64 -I$(X11_INC_DIR) -Iinclude -o $@ $<

$(OBJ32DIR)%.o : $(SRCDIR)%.c $(addprefix $(INCDIR),$(LIB_HEADERS))
	if test ! -d $(OBJ32DIR);  then $(MKDIR) $(OBJ32DIR); fi
	$(CC) -c $(DEBUG) $(CFLAGS) -m32 -I$(X11_INC_DIR) -Iinclude -o $@ $<

sub1:
	cd examples && $(MAKE)
	cd ..

sub2:
	cd test && $(MAKE)
	cd ..

tidy:
	$(RM) $(SRCDIR)*~ ./*~ core

clean:	tidy
	cd examples && $(MAKE) clean
	cd ..
	cd test && $(MAKE) clean
	cd ..

dist:	
	$(RM) $(DISTNAME)
	$(MKDIR) $(DISTNAME)
	$(MKDIR) $(DISTNAME)/rules
	$(MKDIR) $(DISTNAME)/include
	$(MKDIR) $(DISTNAME)/src
	$(MKDIR) $(DISTNAME)/examples
	$(MKDIR) $(DISTNAME)/test
	
	cp README INSTALL AUTHORS LICENSE $(DISTNAME)
	cp configure PACKAGE VERSION $(DISTNAME)
	cp ./rules/* $(DISTNAME)/rules
	cp $(addprefix $(INCDIR),$(LIB_HEADERS)) $(DISTNAME)/include
	cp $(addprefix $(SRCDIR),$(LIB_SORGENTI)) $(DISTNAME)/src
	cp -R ./icons $(DISTNAME)/
	cp ./examples/*.c $(DISTNAME)/examples
	cp ./test/*.c ./test/*.jpg $(DISTNAME)/test

	tar -czf $(DISTNAME).tar.gz $(DISTNAME)

	$(RM) $(DISTNAME)

install:
	if test -e $(PATHLIB32)$(LIBRARY); then \
		$(MKDIR) $(MAX_LIB32_DIR); \
		cp $(PATHLIB32)$(LIBRARY) $(MAX_LIB32_DIR); \
	fi
	if test -e $(PATHLIB64)$(LIBRARY);  then  \
		$(MKDIR) $(MAX_LIB64_DIR); \
		cp $(PATHLIB64)$(LIBRARY) $(MAX_LIB64_DIR); \
	fi
	if test -d $(INCDIR); then \
		$(MKDIR) $(MAX_INC_DIR); \
		cp $(addprefix $(INCDIR),$(LIB_HEADERS)) $(MAX_INC_DIR); \
		chmod 644 $(MAX_INC_DIR)/*; \
	fi

uninstall:
	if test -d $(MAX_LIB32_DIR);  then \
		$(RM) $(MAX_LIB32_DIR); \
	fi
	if test -d $(MAX_LIB64_DIR);  then \
		$(RM) $(MAX_LIB64_DIR); \
	fi
	$(RM) $(MAX_INC_DIR);
	cd examples && $(MAKE) clean
	cd ..
	cd test && $(MAKE) clean
	cd ..

