DOD_CERTS_VERSION = 5.1

all: cert-0.crt

rel3_dodroot_2048.cac:
	rm -f __TMP__.zip
	wget -O __TMP__.zip http://iasecontent.disa.mil/pki-pke/Certificates_PKCS7_v$(DOD_CERTS_VERSION)_DoD.zip
	unzip __TMP__.zip '*/*.der.p7b'
	mv */*.der.p7b "$@"
	rm -rf Certificates_P* __TMP__.zip

cert-%.crt: rel3_dodroot_2048.cac
	idx=0; \
	( \
		openssl pkcs7 -in rel3_dodroot_2048.cac -inform DER -print_certs -text; \
	) | while IFS='' read -r line; do \
		if [ -z "$${line}" ]; then \
			continue; \
		fi; \
		echo "$${line}" >> "cert-$${idx}.crt"; \
		if [ "$${line}" == "-----END CERTIFICATE-----" ]; then \
			idx=$$[$$idx + 1]; \
		fi; \
	done

clean:
	rm -f cert-*.crt
	rm -f rel3_dodroot_2048.cac.new

distclean: clean
	rm -f rel3_dodroot_2048.cac
