SRCDIR=.

SUBDIRS = util test

.PHONY: all
ifeq ($(OSTYPE),cygwin)
all: libcatgets.a cygcatgets1.dll gencat
else
all: libcatgets.a libcatgets.so.1.0 gencat
endif

# Though we have an infrastructure to work on other architectures, we
# have implemented only IA32:

INCLUDES = -I bits/i386 -I util/include

SHELL = bash

OSTYPE = $(shell echo $$OSTYPE)

ifeq ($(OSTYPE),cygwin)
  ICONV_LIBOPT = -liconv
  INTL_LIBOPT = -lintl
else
  ICONV_LIBOPT =
  INTL_LIBOPT =
endif

OBJS = catgets.o open_catalog.o gencat.o

include $(SRCDIR)/Makefile.common

CATGETS_OBJS = catgets.o open_catalog.o

libcatgets.a: $(CATGETS_OBJS)
	rm --force $@
	ar rc $@ $(CATGETS_OBJS)

ifeq ($(OSTYPE),cygwin)
cygcatgets1.dll: $(CATGETS_OBJS)
	$(CCLD) -shared $(CATGETS_OBJS) -o $@ -Wl,--enable-auto-image-base -Xlinker --out-implib -Xlinker libcatgets.dll.a
else
libcatgets.so.1.0: $(CATGETS_OBJS)
	$(CCLD) -shared -o $@ -Wl,-soname,libcatgets.so.1 -lc $(CATGETS_OBJS)
endif

GENCAT_OBJS = \
  gencat.o \
  util/obstack.o \
  util/getline.o \
  util/error.o \
  util/getopt/getopt.o \
  util/getopt/getopt1.o \
  util/argp/argp.o \
  util/strchrnul.o

gencat:%:%.o $(GENCAT_OBJS)
	$(CCLD) -g -o $@ $^ -L. -lcatgets $(ICONV_LIBOPT) $(INTL_LIBOPT) $(LADD)

util/%.o:
	$(MAKE) -C $(dir $@) $(notdir $@)

include Makefile.depend

local-clean:
	rm --force gencat

ifeq ($(OSTYPE),cygwin)
install-so:
	mkdir -p $(DESTDIR)/usr/bin
	install -s -m 755 cygcatgets1.dll  $(DESTDIR)/usr/bin/
	mkdir -p $(DESTDIR)/usr/lib
	install -m 644 libcatgets.dll.a $(DESTDIR)/usr/lib/
else
install-so:
	mkdir -p $(DESTDIR)/usr/lib
	install -m 644 libcatgets.so.1.0 $(DESTDIR)/usr/lib/
	cd $(DESTDIR)/usr/lib/ && ln -s libcatgets.so.1.0 libcatgets.so.1
	cd $(DESTDIR)/usr/lib/ && ln -s libcatgets.so.1.0 libcatgets.so
endif

install: gencat nl_types.h libcatgets.a install-so
	mkdir -p $(DESTDIR)/usr/bin
	install -s -m 755 gencat $(DESTDIR)/usr/bin/
	mkdir -p $(DESTDIR)/usr/include
	install -m 644 nl_types.h $(DESTDIR)/usr/include/
	mkdir -p $(DESTDIR)/usr/lib
	install -m 644 libcatgets.a $(DESTDIR)/usr/lib/
