#
# Country Codes Makefile
#

#
# Modify this, if you need to and if you know what you are doing.
#

prefix ?= /usr/local
bindir ?= ${prefix}/bin

mandir ?= ${prefix}/man/man1

# For system that doesn't support mkdir -p.
# If you have Linux, don't bother.

MKDIRECTORY = mkdir -p

# Mode for user binaries
BINMODE=755

# Mode for the log directory
LOGDIRMODE=700

# Compiler to use
CC=gcc

# Compiler warnings
WARNINGS= -pedantic -Wall

# Compiler flags
CCOPTS = -O2 -fomit-frame-pointer

# The makefile standards document I read says that I have to put it here...
SHELL = /bin/sh

# Clear and set suffixes 
.SUFFIXES:
.SUFFIXES: .c .o

# Install program
INSTALL ?= install

# Miscellaneous
COCO_VERSION = 1.0.6
RELEASEDATE  = 2017-02-26

# Location of sed
SEDBIN  = sed

# sed command
SEDCMDS = "s/@version@/$(COCO_VERSION)/g;s/@releasedate@/$(RELEASEDATE)/g"

ISO3166OBJ = iso3166.o common.o

PROGRAM = iso3166

all: $(PROGRAM)

$(PROGRAM): $(ISO3166OBJ)
	$(CC) $(CCOPTS) $(ISO3166OBJ) -o $@

clean:
	$(RM) $(ISO3166OBJ) core defines.h $(PROGRAM)

strip:
	strip $(PROGRAM)

install:
	$(MKDIRECTORY) $(DESTDIR)/${bindir} $(DESTDIR)/${mandir}/man1
	$(INSTALL) -m $(BINMODE) $(PROGRAM) $(DESTDIR)/${bindir}
	@echo "Installing man page..."
	$(INSTALL) -m 644 iso3166.1 $(DESTDIR)/${mandir}/man1

update-man:
	@$(SEDBIN) $(SEDCMDS) iso3166.1.in > iso3166.1

uninstall:
	$(RM) ${bindir}/$(PROGRAM)
	$(RM) ${mandir}/iso3166.1

.c.o:
	$(CC) $(CCOPTS) $(WARNINGS) -c $<

$(ISO3166OBJ): common.h defines.h protos.h tables.h

defines.h:
	@echo "Creating file \"defines.h\""
	@echo "/*"                                                           >> defines.h
	@echo "   Global #defines for Country Codes"                         >> defines.h
	@echo "   Copyright © 1999, 2000 Diego Javier Grigna <diego@grigna.com>" >> defines.h
	@echo "             © 2017 Johnny A. Solbu <johnny@solbu.net>" >> defines.h
	@echo ""                                                             >> defines.h
	@echo "   File automatically created by the Makefile."               >> defines.h
	@echo "   Do not edit by hand, modify the Makefile instead."         >> defines.h
	@echo "*/"                                                           >> defines.h
	@echo ""                                                             >> defines.h
	@echo "#define COCO_VERSION    \"$(COCO_VERSION)\""                  >> defines.h
	@echo ""                                                             >> defines.h

TARFILE = countrycodes-$(COCO_VERSION).tar.gz

release:
	git archive --prefix=countrycodes-$(COCO_VERSION)/ HEAD -o ../$(TARFILE)

upload-sf:
	rsync -avh --progress ../$(TARFILE) solbu@frs.sourceforge.net:/home/frs/project/countrycodes/$(COCO_VERSION)/
