#########################################################################
#
# Copyright 2019 by Sean Conner.  All Rights Reserved.
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, see <http://www.gnu.org/licenses/>.
#
# Comments, questions and criticisms can be sent to: [email protected]
#
########################################################################

CC      = c99 -Wall -Wextra -Wwrite-strings
CFLAGS  = -g -fPIC -Os
LDFLAGS = -g -shared

INSTALL         = /usr/bin/install
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA    = $(INSTALL) -m 644

prefix          = /usr/local
libdir          = $(prefix)/lib
datarootdir     = $(prefix)/share
dataroot        = $(datarootdir)

LUA             ?= lua
LUA_VERSION     := $(shell $(LUA) -e "print(_VERSION:match '^Lua (.*)')")
LUADIR          ?= $(dataroot)/lua/$(LUA_VERSION)
LIBDIR          ?= $(libdir)/lua/$(LUA_VERSION)

ifneq ($(LUA_INCDIR),)
 override CFLAGS += -I$(LUA_INCDIR)
endif

PEG             = peg

# ===================================================

PHONY: install uninstall clean luacheck

%.so : %.c
       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)

%.i : %.peg
       $(PEG) -o $@ $<

install: mod_blog/html.so mod_blog/tumbler.so
       $(INSTALL) -d $(DESTDIR)$(LUADIR)/org/conman/app/GLV-1/handlers/blog
       $(INSTALL) -d $(DESTDIR)$(LUADIR)/org/conman/app/port70/handlers/blog
       $(INSTALL) -d $(DESTDIR)$(LIBDIR)/org/conman/app/port70/handlers/blog
       $(INSTALL) -d $(DESTDIR)$(LIBDIR)/org/conman/app/mod_blog

       $(INSTALL_DATA)    GLV-1/handlers/*.lua       $(DESTDIR)$(LUADIR)/org/conman/app/GLV-1/handlers/
       $(INSTALL_DATA)    GLV-1/handlers/blog/*.lua  $(DESTDIR)$(LUADIR)/org/conman/app/GLV-1/handlers/blog/
       $(INSTALL_DATA)    port70/handlers/*.lua      $(DESTDIR)$(LUADIR)/org/conman/app/port70/handlers/
       $(INSTALL_DATA)    port70/handlers/blog/*.lua $(DESTDIR)$(LUADIR)/org/conman/app/port70/handlers/blog/
       $(INSTALL_DATA)    *.lua                      $(DESTDIR)$(LUADIR)/org/conman/app/
       $(INSTALL_PROGRAM) mod_blog/*.so              $(DESTDIR)$(LIBDIR)/org/conman/app/mod_blog/

uninstall:
       $(RM) -r $(DESTDIR)$(LUADIR)/org/conman/app
       $(RM) -r $(DESTDIR)$(LIBDIR)/org/conman/app

clean:
       $(RM) -r $(shell find . -name '*~')
       $(RM) -r $(shell find . -name '*.i')
       $(RM) -r $(shell find . -name '*.so')

luacheck:
       luacheck $(shell find . -name '*.lua')

mod_blog/html.so : override CFLAGS += -Wno-unused-label -Wno-unused-function
mod_blog/html.so : mod_blog/html.c mod_blog/html.i

mod_blog/tumbler.so : override CFLAGS += -I/home/spc/source/boston/src -D_GNU_SOURCE
mod_blog/tumbler.so : mod_blog/tumbler.c /home/spc/source/boston/src/wbtum.c /home/spc/source/boston/src/timeutil.c
       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)