#---*- Makefile -*-------------------------------------------------------
#$Author: saulius $
#$Date: 2018-05-10 11:42:20 +0000 (Thu, 10 May 2018) $
#$Revision: 136 $
#$URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/makefiles/PDB-download-store-and-process/Makelocal-cif-downloads $
#------------------------------------------------------------------------

include ${CIF_DEPEND_FILE}

${CIF_DEPEND_FILE}: ${INPUT_LISTS}
	echo "#" $$(date +"%F %T %Z") > $@
	awk '!/^#/{print "${DOWNLOAD_DIR}/"substr($$0,0,2)"/"$$0".cif"}' $^ \
	| sed "s/'//g; s/[()]//g" \
	| sort -u \
	| awk '{print "${CIF_DOWNLOAD_LOG}:", $$0}' >> $@

.PHONY: downloads cleancifdownloads distcleancifdownloads

all: downloads

downloads: ${CIF_DOWNLOAD_LOG}

${CIF_DOWNLOAD_LOG}: ${INPUT_LISTS}
	echo "Downloads completed:" > $@
	date +"%F %T %Z" >> $@

outputs/downloads/%.cif:
	@mkdir -p $(dir $@)
	curl -k -sSL ${DOWNLOAD_URL}/$(notdir $@) > $@

cleancifdownloads:
	rm -f ${CIF_DEPEND_FILE}
	rm -f ${CIF_DOWNLOAD_LOG}

distcleancifdownloads: cleancifdownloads
	find outputs/downloads/ -name '*.cif' -print0 \
	| xargs -0 --no-run-if-empty rm -v

clean: cleancifdownloads

distclean cleanAll: distcleancifdownloads
