#
# GRX Library Makefile for LINUX/console. Uses GNU Make.
#
.PHONY : libst libsh clean cleanall install uninstall setsuid \
	 install-bin uninstall-bin install-fonts uninstall-fonts \
	 install-info uninstall-info

GRXVLNX=y

include ../makedefs.grx

INCDIR= -I. -I./include -I../include
CCOPT += -DLFB_BY_NEAR_POINTER -pipe
ADDON_LIBS =

ifdef GRX_DEFAULT_FONT_PATH
CCOPT += -DGRX_DEFAULT_FONT_PATH=\"$(GRX_DEFAULT_FONT_PATH)\"
endif

ifeq ($(USE_SVGALIB_DRIVER),y)
CCOPT += -DSVGALIB_DRIVER
ADDON_LIBS += -lvga
endif

ifeq ($(USE_FRAMEBUFFER_DRIVER),y)
CCOPT += -DFRAMEBUFFER_DRIVER
endif

ifeq ($(USE_INOUTP_FRAMEDRIVERS),y)
CCOPT += -DINOUTP_FRAMEDRIVERS
endif

ifeq ($(SET_SUIDROOT),y)
EXECBITS = 4755
else
EXECBITS = 0755
endif

GRX20ST   = ../lib/$(GRX_LIB_SUBDIR)/libgrx20.a

GRX20SH = ../lib/$(GRX_LIB_SUBDIR)/libgrx20.so
GRX20SHli = $(GRX20SH).$(word 1,$(subst ., ,$(GRX_VERSION)))
GRX20SHna = $(GRX20SH).$(GRX_VERSION)
SHCCOPT = -fPIC -D__SHARED__ 

OX=.o
OP=

include ./stdobjs.mak

STD_O= $(STD_1) $(STD_2)  $(STD_3)  $(STD_4) \
       $(STD_5) $(STD_6)  $(STD_7)  $(STD_8) \
       $(STD_9) $(STD_10) $(STD_11) $(STD_12)

BGI_O= $(BGI_1) $(BGI_2)  $(BGI_3)  $(BGI_4) \
       $(BGI_5) $(BGI_6)

ADDON_O=

ifdef DEBUG
 ADDON_O += $(DBG_1)
endif

ifeq ($(HAVE_LIBTIFF),y)
  ADDON_O += $(TIF_1)
endif

ifeq ($(HAVE_LIBJPEG),y)
  ADDON_O += $(JPG_1)
else
  ADDON_O += $(NOJPG_1)
endif

ifeq ($(HAVE_LIBPNG),y)
  ADDON_O += $(PNG_1)
else
  ADDON_O += $(NOPNG_1)
endif

ifeq ($(INCLUDE_PRINTING_CODE),y)
  ADDON_O += $(PRN_1)
  INCDIR += -I../addons/print
ifeq ($(INCLUDE_BGI_SUPPORT),y)
  BGI_O += $(PRNBGI_1)
endif
endif

O=      $(STD_O)                \
	$(ADDON_O)              \
	fdrivers/ram24.o        \
	fdrivers/ram32l.o       \
	fdrivers/ram32h.o       \
	fdrivers/svga16.o       \
	fdrivers/svga24.o       \
	fdrivers/svga32h.o      \
	fdrivers/svga32l.o      \
	fdrivers/svga8.o        \
	mouse/lnxkeys.o         \
	misc/lnxmisc.o

ifeq ($(BUILD_X86_64),n)
  O += fdrivers/egavga1.o      \
	fdrivers/ega4.o         \
	fdrivers/svga4.o        \
	fdrivers/vga8x.o
endif

ifeq ($(INCLUDE_BGI_SUPPORT),y)
  O += $(BGI_O)
endif

ifeq ($(USE_SVGALIB_DRIVER),y)
  O += vdrivers/svgalib.o
endif

ifeq ($(USE_FRAMEBUFFER_DRIVER),y)
  O += vdrivers/vd_lnxfb.o
endif

ifeq ($(USE_DIRECT_MOUSE_DRIVER),y)
  O += mouse/lnxinpu2.o
else
  O += mouse/lnxinput.o
endif

LO = $(subst $(OX),.lo,$(O))

UTILP = ../bin/bin2c            \
	../bin/fnt2c            \
	../bin/lfbinfo

UTILPS= ../bin/modetest 

all: libst libsh $(UTILP) $(UTILPS)

libst: $(GRX20ST)

$(GRX20ST): $(LINUX_i386_CONSOLE) $(O)
	-rm -f $(GRX20ST)
	$(AR) -r $(GRX20ST) $(O)
	$(RANLIB) $(GRX20ST)

ifeq ($(INCLUDE_SHARED_SUPPORT),y)
libsh: $(GRX20SHna)
else
libsh:
endif

$(GRX20SHna): $(LINUX_i386_CONSOLE) $(LO)
	-rm -f $(GRX20SHna)
	$(CC) $(LDOPT) -shared -Wl,-soname,$(notdir $(GRX20SHli)) -o $(GRX20SHna) $(LO)
	ln -sf $(notdir $(GRX20SHna)) $(GRX20SHli)

$(UTILP): ../bin/% : utilprog/%.o $(GRX20ST)
	$(CC) $(LDOPT) -o $@ utilprog/$*.o $(GRX20ST)
	$(STRIP) $@

$(UTILPS): ../bin/% : utilprog/%.o $(GRX20ST)
	$(CC) $(LDOPT) -o $@ utilprog/$*.o $(GRX20ST) $(ADDON_LIBS) -lm
	$(STRIP) $@
	chmod $(EXECBITS) $@

$(O): $(LINUX_i386_CONSOLE)

$(LO): $(LINUX_i386_CONSOLE)

$(LINUX_i386_CONSOLE):
	if [ ! -r $(LINUX_i386_CONSOLE) ]; then \
	rm -f $(SYSTEM_TAG_PREFIX).* $(O) $(LO) utilprog/*.o; fi
	touch $(LINUX_i386_CONSOLE)

clean:
	rm -f $(O) $(O:.o=.s) $(LO) $(LINUX_i386_CONSOLE)
	rm -f utilprog/*.o

cleanall: clean
ifeq ($(INCLUDE_SHARED_SUPPORT),y)
	rm -f $(GRX20SHna) $(GRX20SHli)
endif
	rm -f $(GRX20ST)
	rm -f $(UTILP) $(UTILPS)

install: libst libsh
	install -m 0755 -d $(DESTDIR)$(libdir)
	install -m 0755 -d $(DESTDIR)$(includedir)
	install -m 0644 $(GRX20ST) $(DESTDIR)$(libdir)
ifeq ($(INCLUDE_SHARED_SUPPORT),y)
	install -m 0755 $(GRX20SHna) $(DESTDIR)$(libdir)
	ln -sf $(notdir $(GRX20SHna)) $(DESTDIR)$(libdir)/$(notdir $(GRX20SHli))
	ln -sf $(notdir $(GRX20SHli)) $(DESTDIR)$(libdir)/$(notdir $(GRX20SH))
endif
	install -m 0644 ../include/grx20.h $(DESTDIR)$(includedir)
	install -m 0644 ../include/grxkeys.h $(DESTDIR)$(includedir)
	install -m 0644 ../include/libbcc.h $(DESTDIR)$(includedir)
ifeq ($(INCLUDE_PRINTING_CODE),y)
	install -m 0644 ../addons/print/grxprint.h $(DESTDIR)$(includedir)
endif
ifeq ($(INCLUDE_GPC_SUPPORT),y)
	install -m 0755 -d $(DESTDIR)$(unitsdir)
	install -m 0644 ../pascal/grx.pas $(DESTDIR)$(unitsdir)
	install -m 0644 ../pascal/bgi/graph.pas $(DESTDIR)$(unitsdir)
endif

uninstall:
	rm -f $(DESTDIR)$(libdir)/$(notdir $(GRX20ST))
ifeq ($(INCLUDE_SHARED_SUPPORT),y)
	rm -f $(DESTDIR)$(libdir)/$(notdir $(GRX20SH))
	rm -f $(DESTDIR)$(libdir)/$(notdir $(GRX20SHli))
	rm -f $(DESTDIR)$(libdir)/$(notdir $(GRX20SHna))
endif
	rm -f $(DESTDIR)$(includedir)/grx20.h
	rm -f $(DESTDIR)$(includedir)/grxkeys.h
	rm -f $(DESTDIR)$(includedir)/libbcc.h
ifeq ($(INCLUDE_PRINTING_CODE),y)
	rm -f $(DESTDIR)$(includedir)/grxprint.h
endif
ifeq ($(INCLUDE_GPC_SUPPORT),y)
	rm -f $(DESTDIR)$(unitsdir)/grx.pas
	rm -f $(DESTDIR)$(unitsdir)/graph.pas
endif

setsuid: $(UTILPS)
ifeq ($(SET_SUIDROOT),y)
	chown root ../bin/modetest
	chmod $(EXECBITS) ../bin/modetest
else
	@echo "Nothing to do, SET_SUIDROOT is not set to 'y' in makedefs.grx"
endif

install-info:
	install -m 0755 -d $(DESTDIR)$(infodir)
	install -m 0644 ../doc/grx*um.inf $(DESTDIR)$(infodir)/grx.info
	for info_dir_file in $(DESTDIR)$(infodir)/dir /etc/info-dir; do \
	  if [ -w $info_dir_file ]; then \
	    install-info --dir-file=$info_dir_file $(DESTDIR)$(infodir)/grx.info; \
	  fi; \
	done

uninstall-info:
	for info_dir_file in $(DESTDIR)$(infodir)/dir /etc/info-dir; do \
	  if [ -w $info_dir_file ]; then \
	    install-info --dir-file=$info_dir_file -r grx; \
	  fi; \
	done
	rm -f $(DESTDIR)$(infodir)/grx.info

install-bin: $(UTILP) $(UTILPS)
	install -m 0755 -d $(DESTDIR)$(bindir)
	install -m 0755 ../bin/bin2c $(DESTDIR)$(bindir)
	install -m 0755 ../bin/fnt2c $(DESTDIR)$(bindir)
	install -m 0755 ../bin/lfbinfo $(DESTDIR)$(bindir)
	install -m $(EXECBITS) ../bin/modetest $(DESTDIR)$(bindir)

uninstall-bin:
	rm -f $(DESTDIR)$(bindir)/bin2c
	rm -f $(DESTDIR)$(bindir)/fnt2c
	rm -f $(DESTDIR)$(bindir)/lfbinfo
	rm -f $(DESTDIR)$(bindir)/modetest

ifdef GRX_DEFAULT_FONT_PATH
install-fonts:
	install -m 0755 -d $(DESTDIR)$(GRX_DEFAULT_FONT_PATH)
	install -m 0644 ../fonts/* $(DESTDIR)$(GRX_DEFAULT_FONT_PATH)
uninstall-fonts:
	rm -f $(GRX_DEFAULT_FONT_PATH)/*
endif

dep:
	$(CC) -MM $(CCOPT) $(INCDIR) $(O:.o=.c) \
	| sed 's#^.*: \(.*\)\.c#\1.o: \1.c#g' >depend.lnx

$(O): %.o : %.c
	$(CC) -c $(CCOPT) $(INCDIR) $*.c -o $*.o

$(LO): %.lo : %.c
	$(CC) -c $(CCOPT) $(SHCCOPT) $(INCDIR) $*.c -o $*.lo 

.c.s:
	$(CC) -S $(CCOPT)  $(INCDIR) $*.c -o $*.s

.c.o:
	$(CC) -c $(CCOPT) $(INCDIR) $*.c -o $*.o

include depend.lnx
