[require]
packages=

[target]
programs=transgui

[prerules]
ifneq ($(findstring $(OS_TARGET),win32,win64),)
PROG_VER=$(shell type VERSION.txt)
else
PROG_VER=$(shell cat VERSION.txt)
endif
ifeq ($(LAZARUS_DIR),)
  # Searching Lazarus dir
  LAZARUS_DIR=$(strip $(dir $(realpath $(firstword $(strip $(wildcard $(addsuffix /lazbuild$(SRCEXEEXT),$(SEARCHPATH))))))))
  lazarus_ok=$(strip $(wildcard $(LAZARUS_DIR)/lazbuild))
  ifeq ($(lazarus_ok),)
    LAZARUS_DIR=
    # Trying home dir
    lazarus_ok=$(strip $(wildcard $(HOME)/lazarus/lazbuild))
    ifneq ($(lazarus_ok),)
      LAZARUS_DIR=$(HOME)/lazarus
    endif
  endif
  ifeq ($(LAZARUS_DIR),)
    $(error Lazarus directory was not found. Use LAZARUS_DIR=<dir> switch.)
  endif
endif

$(info Using Lazarus dir: $(LAZARUS_DIR))

LAZRES=$(LAZARUS_DIR)/tools/lazres$(SRCEXEEXT)

# Widgetset
LCL_WIDGETSET=gtk2
ifneq ($(findstring $(OS_TARGET),win32,win64),)
LCL_WIDGETSET=win32
endif
ifneq ($(findstring $(OS_TARGET),darwin),)
LCL_WIDGETSET=cocoa
endif

ifeq ($(DEBUG),)
  COMP_OPT=-O3 -g- -CX -XX -Xs -Scgi -l -vewnhibq
else
  COMP_OPT=-O- -g -gl -gs -dCALLSTACK -Scgi -l -vewnhibq
endif

[compiler]
unitdir=synapse/source/lib
options=-MObjFPC -dLCL -dLCL$(LCL_WIDGETSET) $(COMP_OPT)
options_darwin=-k-macosx_version_min -k10.5 -XR/Developer/SDKs/MacOSX10.5.sdk/

#Lazarus dirs
unitdir=$(LAZARUS_DIR)/lcl/units/$(FULL_TARGET)
unitdir=$(LAZARUS_DIR)/lcl/units/$(FULL_TARGET)/$(LCL_WIDGETSET)
unitdir=$(LAZARUS_DIR)/components/lazutils/lib/$(FULL_TARGET)

[rules]
#.PHONY: extraclean

transgui$(EXEEXT): $(patsubst %.lfm,%.lrs,$(wildcard *.lfm)) $(wildcard *.lfm) $(wildcard *.pas)

%.lrs: %.lfm; $(LAZRES) $@ $<

have_lazres=$(strip $(wildcard $(LAZRES)))
ifeq ($(have_lazres),)
check_lazres:
	$(MAKE) -C $(LAZARUS_DIR)/tools lazres$(SRCEXEEXT)
else
check_lazres:
endif

all: check_lazres fpc_all

extraclean:
	-$(DEL) $(addprefix $(UNITTARGETDIRPREFIX), *$(OEXT) *$(PPUEXT) *$(RSTEXT) *$(ASMEXT) *$(STATICLIBEXT) *$(SHAREDLIBEXT) *$(PPLEXT) *.or *.res)

clean: extraclean fpc_clean

zipdist: all
	-$(DEL) -r ./Release/dist
	-$(MKDIRPROG) ./Release
	$(MKDIRPROG) ./Release/dist
	$(MKDIRPROG) ./Release/dist/lang
	-strip ./transgui$(EXEEXT)
	$(CPPROG) ./transgui$(EXEEXT) ./Release/dist
	$(CPPROG) ./readme.txt ./Release/dist
	$(CPPROG) ./README.md ./Release/dist
	$(CPPROG) ./history.txt ./Release/dist
	$(CPPROG) ./LICENSE ./Release/dist
	$(CPPROG) ./transgui.png ./Release/dist
	$(CPPROG) ./lang/transgui.* ./Release/dist/lang
	-$(DEL) ./Release/transgui-$(PROG_VER)-$(FULL_TARGET).zip
	$(MAKE) -C ./Release/dist -f ../../Makefile int_zip ZIP_FILE=transgui-$(PROG_VER)-$(FULL_TARGET).zip
	-$(DEL) -r ./Release/dist

int_zip:
	$(ZIPPROG) -9 -r ../$(ZIP_FILE) .
