Makefile

# Copyright (c) 2024, SWGY, Inc. <ron@sw.gy>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


.SUFFIXES: .msc .png

# Define constants and variables
APPNAME=swtx
VERSION=0.1.2
SRCDIST=$(APPNAME)-$(VERSION)

DEPLOY_USER=youfoundme

SBCL=/usr/local/bin/sbcl
SBCL_FLAGS=--dynamic-space-size 1024 --disable-ldb --noinform

MSCGEN=/usr/local/bin/mscgen

LOWDOWN_FLAGS = -thtml --html-no-skiphtml --html-no-escapehtml

# Define phony targets
.PHONY: \
	bundle \
	deploy \
	ensure-packages \
	clean \
	tix.sw.gy/dist \
	dist-clean \
	install

all: bundle

bundle: $(SRCDIST).tar.gz

deploy: $(SRCDIST).tar.gz
.if !defined(HOSTNAME) || $(HOSTNAME) == ""
	@echo "HOSTNAME must be specified on the command line to deploy"
	@exit 1
.endif
	scp $(SRCDIST).tar.gz $(DEPLOY_USER)@$(HOSTNAME):
	ssh $(DEPLOY_USER)@$(HOSTNAME) "mv -f cl-swiggy-tix cl-swiggy-tix-1 || true"
	ssh $(DEPLOY_USER)@$(HOSTNAME) "tar -zxvf $(SRCDIST).tar.gz"
	ssh $(DEPLOY_USER)@$(HOSTNAME) "cd cl-swiggy-tix ; make install"
	banner success

dist/:
	mkdir dist

tix.sw.gy/dist:
	$(MAKE) -C tix.sw.gy dist

# Make sure the required tools are available
check-tools:
	@command -v lowdown >/dev/null 2>&1 || \
{ echo "lowdown command not found. Please pkg_add lowdown." >&2; exit 1; }

# General rule for .md to .html
.md.html: check-tools tix.sw.gy/header tix.sw.gy/footer
	lowdown $< $(LOWDOWN_FLAGS) | cat tix.sw.gy/header - tix.sw.gy/footer > $@
	PAGE_TITLE=`grep -m 1 '^# ' $< | sed "s/# //"` ; \
	sed -i "s/$(HEAD_TITLE_MARKER)/$$PAGE_TITLE/" $@

$(SRCDIST).tar.gz: tix.sw.gy/dist dist/ tix.sw.gy/ src/*.lisp Makefile cl-swiggy-tix.asd \
									files/httpd.conf.swtx quicklisp.lisp $(WEBSITE_HTML_FILES)
	mkdir -p dist/lib
	cp -R lib/* dist/lib
	mkdir -p dist/src
	cp src/*.lisp dist/src/
	mkdir -p dist/tix.sw.gy
	cp -R tix.sw.gy/dist/* dist/tix.sw.gy
	cp quicklisp.lisp dist/
	cp cl-swiggy-tix.asd dist/
	cp files/httpd.conf.swtx dist/
	cp Makefile dist/
	tar -s '/dist/cl-swiggy-tix/' -czvf $(SRCDIST).tar.gz dist/


# Define targets
dist-clean:
	rm -f files/httpd.conf.swtx
	rm -f $(SRCDIST).tar.gz
	rm -Rf dist/
	$(MAKE) -C tix.sw.gy dist-clean

clean: dist-clean
	rm -f src/swtx
	$(MAKE) -C tix.sw.gy clean

src/swtx: 
	doas -u swtx $(SBCL) $(SBCL_FLAGS) --non-interactive \
		--eval "(ql:quickload 'cl-swiggy-tix)" \
		--eval "(asdf:make :cl-swiggy-tix)"

files/httpd.conf.swtx: files/httpd.conf.swtx.template
.if !defined(EXT_IP) || $(EXT_IP) == ""
	@echo "EXT_IP must be defined for this task."
	@exit 1
.elif !defined(HTTP_HOST) || $(HTTP_HOST) == ""
	@echo "HTTP_HOST must be defined for this task."
	@exit 1
.endif
	sed -e 's/__HOSTNAME__/$(HTTP_HOST)/g' \
        -e 's/__EXT_IP__/$(EXT_IP)/g' \
        files/httpd.conf.swtx.template > files/httpd.conf.swtx


.msc.png:
	$(MSCGEN) -T png -o $@ $<

/home/swtx/quicklisp:
	doas -u swtx $(SBCL) --load quicklisp.lisp \
		--eval "(quicklisp-quickstart:install)" \
		--eval "(ql:add-to-init-file)"

ensure-packages:
	@pkg_info -e 'sbcl->=2.4.7-threads' || (echo "Installing sbcl..."; doas -u root pkg_add sbcl)
	@pkg_info -e 'zstd-*-' || (echo "Installing zstd..."; doas -u root pkg_add zstd)
	@pkg_info -e 'lmdb-*-' || (echo "Installing zstd..."; doas -u root pkg_add lmdb)
	@pkg_info -e 'fcgi-*-' || (echo "Installing zstd..."; doas -u root pkg_add fcgi)


install: ensure-packages /home/swtx/quicklisp quicklisp.lisp httpd.conf.swtx tix.sw.gy
	doas -u root cp httpd.conf.swtx /etc/httpd.conf.swtx
	doas -u root mkdir -p /usr/local/share/cl-swiggy-tix
	doas -u root mkdir -p /usr/local/share/clack
	doas -u root mkdir -p /usr/local/share/cl-fastcgi
	doas -u root mkdir -p /usr/local/share/sly-asdf
	doas -u root chown `whoami` /usr/local/share/cl-swiggy-tix
	doas -u root chown `whoami` /usr/local/share/clack
	doas -u root chown `whoami` /usr/local/share/cl-fastcgi
	doas -u root chown `whoami` /usr/local/share/sly-asdf
	cp -R src/ cl-swiggy-tix.asd /usr/local/share/cl-swiggy-tix/
	cp -R lib/clack/*            /usr/local/share/clack/
	cp -R lib/cl-fastcgi/*       /usr/local/share/cl-fastcgi/
	cp -R lib/sly-asdf/*         /usr/local/share/sly-asdf/
	doas -u swtx ln -sfh /usr/local/share/cl-swiggy-tix /home/swtx/common-lisp/cl-swiggy-tix
	doas -u swtx ln -sfh /usr/local/share/clack         /home/swtx/common-lisp/clack
	doas -u swtx ln -sfh /usr/local/share/cl-fastcgi    /home/swtx/common-lisp/cl-fastcgi
	doas -u swtx ln -sfh /usr/local/share/sly-asdf      /home/swtx/common-lisp/sly-asdf
	doas -u root mkdir -p /var/www/htdocs/tix.sw.gy
	doas -u root chown -R swtx:swtx-dev /var/www/htdocs/tix.sw.gy
	doas -u swtx chmod g+w /var/www/htdocs/tix.sw.gy
	doas -u swtx cp -R tix.sw.gy/ /var/www/htdocs/
	doas -u swtx chgrp -R swtx-dev /var/www/htdocs/tix.sw.gy/
	doas -u root rcctl reload httpd

run-on-host:
.if !defined(SLY_IP) || $(SLY_IP) == ""
	@echo "SLY_IP must be defined for this task."
	@exit 1
.endif
	doas -u swtx nohup /usr/local/bin/sbcl --dynamic-space-size 1024 \
			--lose-on-corruption \
			--non-interactive \
			--eval "(ql:quickload 'cl-swiggy-tix)" \
			--eval "(slynk:create-server :port 4005 :interface \"$(SLY_IP)\")" \
			--eval "(setf swtx:*disable-random-codes* T)" \
			--eval "(swtx:serve :use-thread nil)" &

	doas -u swtx tail -f /home/swtx/nohup.out


uninstall:
	doas -u root rm -Rf /usr/local/share/cl-swiggy-tix
	doas -u root rm /etc/httpd.conf.swtx
	rm -Rf /var/www/htdocs/tix.sw.gy
	echo "You may want to disable httpd and update /etc/httpd.conf"