#--*- Makefile -*--

# Look up information associated with each ISSN.

# The output is a JSON augmented with comments, with the extensions
# '.jsons'. Comments start with the has symbol ('#') on the first
# position in the line and extend till the end of the line. To get rid
# of comments for JSON parsers, use the "grep -v '^#'" filter.

ISSN_API_URI ?= https://portal.issn.org

ISSN_QUERY_URI ?= ${ISSN_API_URI}/resource/ISSN/

# The following ISSNs failed to look up on https://portal.issn.org

FAILING_ISSNS = 

INPUT_DIR ?= inputs

OUTPUT_DIR ?= outputs

INPUT_ISSN_LIST ?= $(wildcard ${INPUT_DIR}/*.lst)

define CHECK_ID_LISTS
BEGIN{ \
    if(ARGC<2) { \
        print "awk: no identifier list files in the " \
              "\"${INPUT_DIR}/\" directory" \
            > "/dev/stderr"; \
        exit(1) \
    } \
}
endef

INPUT_ISSN_IDS := $(filter-out ${FAILING_ISSNS}, \
	$(shell awk '${CHECK_ID_LISTS} !/^\#/' ${INPUT_ISSN_LIST} | sort -u) \
)

OUTPUT_ISSN_JSONS = \
    ${INPUT_ISSN_IDS:%=${OUTPUT_DIR}/issn/%.jsons}

OUTPUT_ISSN_XML = \
    ${INPUT_ISSN_IDS:%=${OUTPUT_DIR}/issn/%.xml}

OUTPUT_ISSN_HTML = \
    ${INPUT_ISSN_IDS:%=${OUTPUT_DIR}/issn/%.html}

OUTPUT_ISSN_LISTS = \
    ${INPUT_ISSN_IDS:%=${OUTPUT_DIR}/issn/%.lst}

OUTPUT_ALL_ISSNS = ${OUTPUT_DIR}/issn.lst

SLEEP ?= sleep 1

.PHONY: all lookup

all: lookup

lookup: ${OUTPUT_ISSN_JSONS} ${OUTPUT_ISSN_XML} ${OUTPUT_ISSN_HTML} \
	${OUTPUT_ISSN_LISTS} \
	${OUTPUT_ALL_ISSNS}

#------------------------------------------------------------------------------

${OUTPUT_DIR}/issn/%.jsons:
	date +"# %F %T %Z" > $@
	curl -sSL '${ISSN_QUERY_URI}$*?format=json' \
	| jq . \
	>> $@
	${SLEEP}

${OUTPUT_DIR}/issn/%.xml:
	curl -sSL '${ISSN_QUERY_URI}$*?format=xml' \
	| tidy -i -q -xml \
	> $@
	date +"<!-- %F %T %Z -->" >> $@
	${SLEEP}

${OUTPUT_DIR}/issn/%.html:
	curl -sSL '${ISSN_QUERY_URI}$*' > $@
	date +"<!-- %F %T %Z -->" >> $@
	${SLEEP}

${OUTPUT_DIR}/issn/%.lst: ${OUTPUT_DIR}/issn/%.jsons
	date +"# %F %T %Z" > $@
	grep -v '^#' $^ \
	| jq -r ".\"@graph\"[] | select(.issn != null) \
		| {issn,url: (if .url|type == \"array\" \
			then .url[] \
			else .url end)} \
		| .issn + \"\t\" + .url" \
	| grep -vP '^\s*$$' \
	>> $@

${OUTPUT_ALL_ISSNS}: ${OUTPUT_ISSN_LISTS}
	touch $@
	mv $@ $(dir $@)tmp-$(notdir $@)
	date +"# %F %T %Z" > $@
	find $(sort $(dir $^)) -name '*.lst' \
	| xargs cat \
	| grep -hv '^#' $(dir $@)tmp-$(notdir $@) - \
	| sort -u \
	>> $@
	rm $(dir $@)tmp-$(notdir $@)

#------------------------------------------------------------------------------

.PHONY: clean distclean clean-query distclean-query

clean-query:
	rm -f ${OUTPUT_ISSN_JSONS}
	rm -f ${OUTPUT_ISSN_XML}
	rm -f ${OUTPUT_ISSN_HTML}
	rm -f ${OUTPUT_ISSN_LISTS}

distclean-query:
	rm -f ${OUTPUT_ALL_ISSNS}

clean: clean-query
distclean: distclean-query
