#!/usr/bin/env bmake -f
### Makefile  -*- Make -*-
## Set up Iroca.

### Ivan Shmakov, 2022

## To the extent possible under law, the author(s) have dedicated
## all copyright and related and neighboring rights to this software
## to the public domain worldwide.  This software is distributed
## without any warranty.

## You should have received a copy of the CC0 Public Domain Dedication
## along with this software.  If not, see
## <http://creativecommons.org/publicdomain/zero/1.0/>.

### Commentary:

## Note that this is a makefile for NetBSD Make (AKA bmake or pmake.)
## It is not intended to be compatible with other make implementations,
## such as GNU Make.

## In the simplest of cases, the usage may be as follows:

## $ bmake pg-setup tests 

### History:

## 0.4  2022-12-14 23:45Z
##      (LH_CONF): New variable.
##      (MQ_CONF): Likewise.
##      (lh-start): Use $(START_STOP_DAEMON) --chdir=. option,
##      as we rely on relative filenames throughout.
##      (mq-start): Likewise.
##      (pg-mkdb): Fixed the use of the :@@@ modifier.
##      (tests): Use :Q variable modifier.

## 0.3  2022-12-14 07:05:47Z
##      (sfn.xvg6FXe3BGdhHIUufCU5oEjzLWb76IW7dLU1agVuRRA.mk)
##      (default): Depend on checks (was: pg-ck.)
##      (checks): New non-file target depending on pg-ck,
##      mq-ck, lh-ck.
##      (MOSQUITTO): New variable.
##      (LIGHTTPD): Likewise.
##      (CURL): Likewise.
##      (CURL_FLAGS): Likewise.
##      (MOSQUITTO_PUB): Likewise.
##      (START_STOP_DAEMON): Likewise.
##      (LH_PID): Likewise.
##      (LH_URI): Likewise.
##      (MQ_PID): Likewise.
##      (MQ_SOCK): Likewise.
##      (lh-ck): New non-file target.
##      (lh-start): Likewise.
##      (lh-stop): Likewise.
##      (mq-ck): Likewise.
##      (mq-start): Likewise.
##      (mq-stop): Likewise.
##      (pg-ck): Check if the process is still running first.
##      (pg-mkdb): Also depend on sql/logger.sql sql/report.sql,
##      and pass them to $(PSQL) -f.
##      (pg-mkdb): Use :Q variable modifier whenever possible.
##      ($(PG_PID)): Likewise.
##      ($(PG_DIR)): Likewise.
##      (tests): Simplified.

## 0.2  2022-11-25 17:30:50Z
##      (sfn._jx003OeN6HUAnU5q2EMaYXfmVTqLg36UqSIcLTxFWg.mk)
##      Added a short usage example to Commentary:.
##      (PERL): New variable.
##      (TESTS): Likewise.
##      (tests): New target.
##      (.PHONY): Add tests.
##      (PGDATABASE): Export variable.
##      (PGUSER): Likewise.
##      (PGHOST): Likewise.
##      (pg-mkdb): Fixed: use -d option to $(PSQL) (was: -D.)
##      Fixed: do not try to ensure the database is running by
##      executing an empty command (was: exitting successfully if so.)
##      Also create $(PGUSER) role and, under that role, execute
##      sql/create.sql, on which we now depend as well.
##      ($(PG_DIR)): Also initialize pg_ident.conf with a sole unix
##      map allowing $(USER) to connect as both $(PGSUPERUSER) and
##      $(PGUSER).  Use local map=unix in pg_hba.conf (was: trust.)

## 0.1  2022-11-23 08:55:15Z
##      (sfn.RaNqOh7pX10CBNADa8NW1I5mCQW8GPk929X-LCkLcbo.mk)
##      Initial revision.

### Code:

default: checks

.PHONY: default checks

checks: pg-ck mq-ck lh-ck

## These daemon names must be absolute filenames; see --exec= below
MOSQUITTO   ?= /usr/sbin/mosquitto
LIGHTTPD    ?= /usr/sbin/lighttpd

CURL    ?= curl
CURL_FLAGS      ?= --noproxy \* -sIki 
MOSQUITTO_PUB   ?= mosquitto_pub
PERL    ?= perl
PSQL    ?= psql
START_STOP_DAEMON   ?= /usr/sbin/start-stop-daemon

LH_CONF ?= etc/lighttpd.conf
LH_PID  ?= .lighttpd.pid
# LH_SOCK ?= .lighttpd.socket
LH_URI  ?= http://ip6-localhost:3180/

MQ_CONF ?= etc/mosquitto.conf
MQ_PID  ?= .mosquitto.pid
MQ_SOCK ?= .mosquitto.socket

PG_BIN  ?= /usr/lib/postgresql/15/bin
PG_CTL  ?= '$(PG_BIN)'/pg_ctl
PG_DIR  ?= $(HOME)/.postgresql/iroca
PG_INI  ?= '$(PG_BIN)'/initdb -E UTF-8
PG_LOG  ?= $(PG_DIR:H)/.$(PG_DIR:T).log
PG_PID  ?= $(PG_DIR)/postmaster.pid

PG_INIFLAGS ?= --no-instructions

PGDATABASE  ?= iroca
PGSUPERUSER ?= toor
PGUSER      ?= $(USER)
PGHOST      ?= $(PG_DIR)
.export PGDATABASE PGUSER PGHOST

TESTS    = test/43-pq-schema.perl

.PHONY: lh-ck lh-start lh-stop

lh-ck:
	$(START_STOP_DAEMON) \
	    --pidfile=$(LH_PID:Q) --exec=$(LIGHTTPD:Q) --status
	# $(CURL) $(CURL_FLAGS) --unix-socket $(LH_SOCK:Q) -- http://-/
	$(CURL) $(CURL_FLAGS) -- $(LH_URI:Q)

lh-start:
	$(START_STOP_DAEMON) --chdir=. \
	    --pidfile=$(LH_PID:Q) --exec=$(LIGHTTPD:Q) --start \
	    -- $(LIGHTTPD) -f $(LH_CONF:Q)

lh-stop:
	$(START_STOP_DAEMON) \
	    --pidfile=$(LH_PID:Q) --exec=$(LIGHTTPD:Q) --stop

.PHONY: mq-ck mq-start mq-stop

mq-ck:
	$(START_STOP_DAEMON) \
	    --pidfile=$(MQ_PID:Q) --exec=$(MOSQUITTO:Q) --status
	$(MOSQUITTO_PUB) --unix $(MQ_SOCK:Q) -t Test -n

mq-start:
	$(START_STOP_DAEMON) --chdir=. \
	    --pidfile=$(MQ_PID:Q) --exec=$(MOSQUITTO:Q) --start \
	    -- $(MOSQUITTO) -d -c $(MQ_CONF:Q)

mq-stop:
	$(START_STOP_DAEMON) \
	    --pidfile=$(MQ_PID:Q) --exec=$(MOSQUITTO:Q) --stop
	$(START_STOP_DAEMON) --pidfile=$(MQ_PID:Q) --stop

.PHONY: pg-ck pg-schema pg-mkdb pg-start pg-initdb

pg-ck:
	$(START_STOP_DAEMON) --pidfile=$(PG_PID:Q) --status
	$(PSQL) -c ""

pg-setup: pg-mkdb

pg-mkdb: pg-start sql/create.sql sql/logger.sql sql/report.sql
	$(PSQL) -U $(PGSUPERUSER:Q) -d postgres \
	    -c 'CREATE ROLE "$(PGUSER)" WITH LOGIN;' \
	    -c 'CREATE DATABASE "$(PGDATABASE)" \
	            WITH OWNER "$(PGUSER)";'
	$(PSQL) $(.ALLSRC:[2..-1]:@f@-f $(f:Q)@)

pg-start: $(PG_PID)

$(PG_PID): pg-initdb
	$(PG_CTL) -D $(PG_DIR:Q) -l $(PG_LOG:Q) start

pg-initdb: $(PG_DIR)

$(PG_DIR):
	$(PG_INI) -D $(PG_DIR:Q) -U $(PGSUPERUSER:Q) $(PG_INIFLAGS)
	printf "%7s %7s %7s\\n \
	    unix $(USER:Q) $(PGSUPERUSER:Q) \
	    unix $(USER:Q) $(PGUSER:Q) \
	    > $(PG_DIR:Q)/pg_ident.conf
	printf %s\\n 'local all all peer map=unix' \
	    > $(PG_DIR:Q)/pg_hba.conf
	printf %s\\n \
	    "listen_addresses = ''" \
	    "unix_socket_directories = '$(PGHOST)'" \
	    >> $(PG_DIR:Q)/postgresql.conf

tests: src/run-tests.perl $(TESTS)
	$(PERL) -- $(.ALLSRC:[@]:@f@$(f:Q)@)

### Makefile ends here
