MAKE=make
bindir=$(prefix)/bin
# remove -L/lib/ if you are using MacOS
LIBS = -L/lib/ -lcurses -lmenu
GCC = gcc
CFLAGS = -pedantic -Wall
OPT_FLAGS = -s
DEBUG_FLAGS = -g -DDEBUG -O0
MODULE= -DMODULE
ALL_CFLAGS = $(CFLAGS) $(OPT_FLAGS) 
SRC=misc.c fildir.c stack.c dialogs.c menu.c subshell.c iface.c ofm.c
OBJ=$(SRC:.c=.o)

ifeq ($(with-editor),yes)
  OBJ_EDITOR=ofe/ui.o ofe/file.o ofe/ofe.o
else
  with-editor=no
endif

all: $(OBJ)
	if [ "$(with-editor)" = "yes" ]; then \
	  cd ofe && $(MAKE) bindir="$(bindir)" LIBS="$(LIBS)" GCC="$(GCC)" \
	  CFLAGS="$(CFLAGS)" OPT_FLAGS="$(OPT_FLAGS)" DEBUG_FLAGS="$(DEBUG_FLAGS)" \
	  ALL_CFLAGS="$(CFLAGS) $(OPT_FLAGS) $(MODULE)" \
	  module && cd ..; \
	fi; \
	$(GCC) $(LIBS) $(ALL_CFLAGS) -o ofm $(OBJ) $(OBJ_EDITOR)

debug: ALL_CFLAGS = $(CFLAGS) $(DEBUG_FLAGS)
debug: $(OBJ)
	$(GCC) $(LIBS) $(ALL_CFLAGS) -o ofm $(OBJ)	

misc.o: misc.c
	$(GCC) $(ALL_CFLAGS) -c misc.c

fildir.o: fildir.c
	$(GCC) $(ALL_CFLAGS) -c fildir.c	

stack.o: stack.c
	$(GCC) $(ALL_CFLAGS) -c stack.c	

dialogs.o: dialogs.c
	$(GCC) $(ALL_CFLAGS) -c dialogs.c

menu.o: menu.c
	$(GCC) $(ALL_CFLAGS) -c menu.c

subshell.o: subshell.c
	$(GCC) $(ALL_CFLAGS) -c subshell.c

iface.o: iface.c
	$(GCC) $(ALL_CFLAGS) -c iface.c

ofm.o: ofm.c
	$(GCC) $(ALL_CFLAGS) -c ofm.c

clean:
	cd ofe && $(MAKE) clean &&cd ..; \
	rm -f *.o core ofm *.da *.bb *.bbg;

install:
	cp ofm $(bindir)/ofm

editor:
	cd ofe && $(MAKE) bindir="$(bindir)" LIBS="$(LIBS)" GCC="$(GCC)" \
	 CFLAGS="$(CFLAGS)" OPT_FLAGS="$(OPT_FLAGS)" DEBUG_FLAGS="$(DEBUG_FLAGS)" \
	 ALL_CFLAGS="$(CFLAGS) $(OPT_FLAGS)"; \
	 cd ../; $(MAKE) editor-install

editor-install:
	cp ofe/ofe $(bindir)/ofe
