all: build-server

# For requests where authentication is required.
LOGIN ?=
ifeq ($(LOGIN),)
LOGIN_OPT =
else
LOGIN_OPT = -l $(LOGIN)
endif

# No idea why, but I need that to run applications. Ignore that.
#LD_P ?= LD_PRELOAD=/usr/local/lib/libipc.so.0

OPTS ?= --progress

Q ?= @

SHOULD_UPDATE = ./bin/should-update

####################
### REQUEST EXAMPLES
####################

DOMAIN ?= example.com
build-write-zone-file: tools/write-zone-file.cr
	$(Q)-([ ! -f bin/write-zone-file ] || [ tools/write-zone-file.cr -nt bin/write-zone-file ]) && shards build write-zone-file $(OPTS)
zone-file: build-write-zone-file
	$(Q)./bin/write-zone-file $(DOMAIN)

build-write-template-zone-file: tools/write-template-zone-file.cr
	$(Q)-([ ! -f bin/write-template-zone-file ] || [ tools/write-template-zone-file.cr -nt bin/write-template-zone-file ]) && shards build write-template-zone-file $(OPTS)
zone-basic-template-file: build-write-template-zone-file
	$(Q)./bin/write-template-zone-file $(DOMAIN)

VERBOSITY ?= 4
run-client-verbosity:
	$(Q)$(LD_P) ./bin/dnsmanager-client admin maintenance verbosity $(VERBOSITY) $(LOGIN_OPT)

run-client-domain-add:
	$(Q)$(LD_P) ./bin/dnsmanager-client user domain add $(DOMAIN) $(LOGIN_OPT)

run-client-domain-del:
	$(Q)$(LD_P) ./bin/dnsmanager-client user domain del $(DOMAIN) $(LOGIN_OPT)

run-client-domain-list:
	$(Q)$(LD_P) ./bin/dnsmanager-client user domain list $(LOGIN_OPT)

run-client-zone-add:
	$(Q)$(LD_P) ./bin/dnsmanager-client user zone add $(DOMAIN).json $(LOGIN_OPT)

run-client-zone-get:
	$(Q)$(LD_P) ./bin/dnsmanager-client user zone get $(DOMAIN) $(LOGIN_OPT)

RRID ?= 1
NAME ?=
TTL ?= 3600
TARGET ?=
run-client-rr-add-a:
	$(Q)$(LD_P) ./bin/dnsmanager-client user rr add A $(DOMAIN) $(NAME) $(TTL) $(TARGET) $(LOGIN_OPT)

run-client-rr-update-a:
	$(Q)$(LD_P) ./bin/dnsmanager-client user rr update A $(DOMAIN) $(RRID) $(NAME) $(TTL) $(TARGET) $(LOGIN_OPT)

run-client-rr-del:
	$(Q)$(LD_P) ./bin/dnsmanager-client user rr del $(DOMAIN) $(RRID) $(LOGIN_OPT)

run-admin-generate-all-zonefiles:
	$(Q)$(LD_P) ./bin/dnsmanager-client admin genall $(LOGIN_OPT)

##################
### SETUP COMMANDS
##################

DBDIR=/tmp/DATA-dnsmanagerd
run-dnsmanagerd:
	$(Q)$(LD_P) ./bin/dnsmanagerd -v $(VERBOSITY) -r $(DBDIR)

PORT ?= 8082
ADDR ?=
run-token-handler:
	$(Q)$(LD_P) ./bin/token-handler $(PORT) $(ADDR)

build-server:
	$(Q)-$(SHOULD_UPDATE) bin/dnsmanagerd && shards build dnsmanagerd $(OPTS)

build-client:
	$(Q)-$(SHOULD_UPDATE) bin/dnsmanager-client && shards build dnsmanager-client $(OPTS)

build-token-handler:
	$(Q)shards build token-handler $(OPTS)

build: build-server build-client build-token-handler

print-messages:
	cat src/requests/*.cr | ./bin/get-messages.awk
print-message-numbers:
	make -s print-messages | grep -E "^[0-9]" | sort -n
print-messages-without-comments:
	make -s print-messages | grep -vE '^[[:blank:]]+#'
print-response-messages:
	cat src/responses/*.cr | ./bin/get-messages.awk
print-response-message-numbers:
	make -s print-response-messages | grep -E "^[0-9]" | sort -n
print-response-messages-without-comments:
	make -s print-response-messages | grep -vE '^[[:blank:]]+#'

MIGRATION_FILE_INIT         ?= /tmp/dnsmanager-migration-init.txt
MIGRATION_FILE_INTERMEDIARY ?= /tmp/dnsmanager-migration-intermediary.txt
MIGRATION_FILE_TARGET       ?= /tmp/dnsmanager-migration-target.txt
$(MIGRATION_FILE_TARGET):
	# Format entries: one per line.
	./bin/format.awk < $(MIGRATION_FILE_INIT) |\
		./bin/fix-last-element.awk |\
		# Remove 'activated' attribute. No longer relevant (never, actually). \
		sed 's/ \+activated \+=> \+0, \+//' |\
		sed 's/domain \+=> \+/"domain": /' |\
		sed 's/ \+login \+=> \+/"login": /' |\
		# Put double-quotes around logins composed of digits. \
		sed 's/"login": \([0-9]\+\)/"login": "\1"/' |\
		sed 's/", \+}/" }/' |\
		# Fix @ in logins. \
		sed 's/\\@/@/' |\
		# Remove obvious SQL hacks. \
		sed '/))))/d' |\
		sed '/: \+[0-9]\+,/d' |\
		# Remove all entries with accents. \
		grep -v '\\' > $(MIGRATION_FILE_INTERMEDIARY)
	./bin/migration-final.awk < $(MIGRATION_FILE_INTERMEDIARY) | sort -n > $(MIGRATION_FILE_TARGET)

MIGRATION_FILE_AUTHD        ?= /tmp/authd-migration-user-db.txt
$(MIGRATION_FILE_AUTHD):; cat $(MIGRATION_FILE_TARGET) | awk '{ print $$2 }' > $(MIGRATION_FILE_AUTHD)
migration-file-authd: $(MIGRATION_FILE_AUTHD)

run-migration-client:;	./bin/dnsmanager-client admin migration-script $(MIGRATION_FILE_TARGET) $(LOGIN)
migration-files: $(MIGRATION_FILE_TARGET) $(MIGRATION_FILE_AUTHD)
migration: migration-files run-migration-client

doc:
	crystal docs src/main.cr src/client.cr lib/authd/src/client.cr

HTTPD_ACCESS_LOGS ?= /tmp/access-dodb-docs.log
HTTPD_ADDR        ?= 127.0.0.1
HTTPD_PORT        ?= 9001
DIR               ?= docs
serve-doc:
	darkhttpd $(DIR) --addr $(HTTPD_ADDR) --port $(HTTPD_PORT) --log $(HTTPD_ACCESS_LOGS)

wipe-db:
	rm -r $(DBDIR)

release:
	make build-server OPTS="--release --progress --no-debug"