# Copyright (C) 2010-2017 by Hoefer Alfred 

USE_BULDIN_APP = y
USE_BULDIN_ALL_APP = y

TOP = ../..

MAKEFLAGS += --no-print-directory

ifndef MAKEOKFLAG

DEBUG = 
#DEBUG_STACK = y
STATIC_ALDLIB = y
RPATH_ALDLIB = y
USE_PNG = y
USE_JPEG = y
USE_JPEG_TURBO = 
# USE_ASM = c
USE_ACL = 

USE_PTHREAD = y

USE_FREETYPE = 
USE_XLIBRARY = y

include $(TOP)/Makefile.tool

MAKEOKFLAG = y
endif #MAKEOKFLAG

include Mobj

ifeq ($(BUILD_OS), Win32)
 W32res_rc = resW32_ALDfile.rc
 W32res_o = resW32_ALDfile.o
 XLIBRARY = $(W32res_o) -lgdi32
 LDFLAGS += -mwindows
 USE_BULDIN_ALL_APP =
else
 XLIBRARY += -lXext
endif

include $(TOP)/Makefile.LastBuilt

ifndef MAKE_ALL
SUBDIRS = $(TOP)/lib
endif

ifdef USE_BULDIN_APP
  BIAPP = buildin_app/
  include buildin_app/Mobj

  SUBDIRS += buildin_app
  EXTRACF += -DUSE_BULDIN_APP

  ifdef USE_BULDIN_ALL_APP
    EXTRACF += -DUSE_BULDIN_ALL_APP
  endif

  export USE_BULDIN_APP USE_BULDIN_ALL_APP
endif

OBJL = $(addprefix $(loc_obj_PHAT)/,$(OBJS))

vpath %.o $(loc_obj_PHAT)

all: subdirs $(W32res_o) $(OBJS) aldfile$(EXE)

.PHONY: subdirs $(SUBDIRS)

subdirs: $(SUBDIRS)

$(SUBDIRS):
	@$(MAKE) -C $@

$(W32res_o): $(W32res_rc)
	$(WINDRES) -i $(W32res_rc) -o $(W32res_o)

.c.o:
	@echo "	CC $<"
	@${RM} $(lib_obj_PHAT)/$@.o
	@$(CC) $(CFLAGS) $(EXTRACF) $(INCL) -c $< -o $(loc_obj_PHAT)/$@

# Test new lib
$(OBJS): Makefile $(DEPLIB) $(HDEPS)


aldfile$(EXE): aldfile.c $(OBJS) Makefile $(DEPLIB) $(HDEPS)
	@echo "	CC "$(patsubst %$(EXE),%,$@).c
	@${RM} $(lib_obj_PHAT)/$(patsubst %$(EXE),%,$@).o
	@$(CC) $(CFLAGS) $(EXTRACF) $(INCL) -c $(patsubst %$(EXE),%,$@).c -o $(loc_obj_PHAT)/$(patsubst %$(EXE),%,$@).o

	@echo "	LD "$(patsubst %$(EXE),%,$@).c
	@$(CC) $(LDFLAGS) -O2 $(PATHLIBS) $(RPATH) -o $@ $(loc_obj_PHAT)/$(patsubst %$(EXE),%,$@).o $(OBJL) $(LIBRARY) $(XLIBRARY)
ifdef STRIP_BIN
	@echo "	STRIP $@"
	@$(STRIP) -s --remove-section=.note --remove-section=.comment $@
endif

ifdef USE_BULDIN_APP
ifneq ($(BUILD_OS), Win32) 
	@${RM} aldfind$(EXE)
	@${RM} aldfilebox$(EXE)
	@$(LN) aldfile$(EXE) aldfind$(EXE)
ifdef USE_BULDIN_ALL_APP
	@$(LN) aldfile$(EXE) aldfilebox$(EXE)
endif
endif
endif
	@$(UPDATE_BUILDversion)

install:
ifeq ($(BUILD_OS), Win32) 
	${INSTALL} -p -D -m 0755 aldfile$(EXE) $(DESTDIR)$(bindir)/aldfile$(EXE)
else
	${INSTALL} -p -D -m 0755 aldfile$(EXE) $(DESTDIR)$(bindir)/aldfile
	${INSTALL} -p -D -m 0644 aldfile.desktop $(DESTDIR)$(datadir)/applications/aldfile.desktop
	${INSTALL} -p -D -m 0644 $(TOP)/icons/16x16/apps/aldfile.png $(DESTDIR)$(datadir)/icons/16x16/apps/aldfile.png
	${INSTALL} -p -D -m 0644 $(TOP)/icons/32x32/apps/aldfile.png $(DESTDIR)$(datadir)/icons/32x32/apps/aldfile.png
ifdef USE_BULDIN_APP
	${RM} $(DESTDIR)$(bindir)/aldfind
	cd $(DESTDIR)$(bindir); ${LN} aldfile aldfind
	${INSTALL} -p -D -m 0644 aldfind.desktop $(DESTDIR)$(datadir)/applications/aldfind.desktop
	${INSTALL} -p -D -m 0644 $(TOP)/icons/16x16/apps/aldfind.png $(DESTDIR)$(datadir)/icons/16x16/apps/aldfind.png
	${INSTALL} -p -D -m 0644 $(TOP)/icons/32x32/apps/aldfind.png $(DESTDIR)$(datadir)/icons/32x32/apps/aldfind.png
ifdef USE_BULDIN_ALL_APP
	${RM} $(DESTDIR)$(bindir)/aldfilebox
	cd $(DESTDIR)$(bindir); ${LN} aldfile aldfilebox
endif
endif
endif

uninstall:
ifeq ($(BUILD_OS), Win32) 
	${RM} $(DESTDIR)$(bindir)/aldfile$(EXE)
else
	${RM} $(DESTDIR)$(bindir)/aldfile
	${RM} $(DESTDIR)$(bindir)/aldfilebox
	${RM} $(DESTDIR)$(bindir)/aldfind
	${RM} $(DESTDIR)$(datadir)/applications/aldfile.desktop
	${RM} $(DESTDIR)$(datadir)/applications/aldfind.desktop
	${RM} $(DESTDIR)$(datadir)/icons/16x16/apps/aldfile.png
	${RM} $(DESTDIR)$(datadir)/icons/32x32/apps/aldfile.png
	${RM} $(DESTDIR)$(datadir)/icons/16x16/apps/aldfind.png
	${RM} $(DESTDIR)$(datadir)/icons/32x32/apps/aldfind.png
endif

loc_clean:
	${RM} *.o $(loc_obj_PHAT)/*.o

clean:
	${RM} *.o $(loc_obj_PHAT)/*.o
	${RM} -r $(loc_obj_PHAT)
	${RM} aldfile$(EXE)
	@${RM} aldfind$(EXE)
	@${RM} aldfilebox$(EXE)
ifndef MAKE_ALL
	$(MAKE) -C $(TOP)/lib  -j clean
endif
