#!/bin/sh
set -e
LC_ALL=C
export LC_ALL
echo
echo Generating certificates.
rm -f cert.tmp
mangle_unprintable_command="`../intl/mangle-unprintable`"
(
echo '/* Automatically generated by certs/gen */'
echo
echo 'static_const struct {'
echo '	int len;'
echo '	const char *data;'
echo '} certificates[] = {'
total=`wc -l links.crt|sed 's/ .*//'`
line=0
while read x; do
	line=`expr $line + 1`
	if echo "$x" | grep ^----- >/dev/null; then
		if echo "$x" | grep ^-----END >/dev/null; then
			"`which echo`" -ne `expr $line \* 100 / $total || true`%'\r' 1>&2
			base64 -d <cert.tmp >cert.bin
			echo -n "{ `wc -c cert.bin|sed 's/ .*//'`, \""
#			hexdump -v <cert.bin  -e '/1 "-%o"'|sed 's/-/\\/g'
			sed 's/\([\"]\)/\\\1/g' <cert.bin | eval "$mangle_unprintable_command" | tr '\000' '\t' | sed 's/\t/\\000/g' | tr '\012' '\t' | sed 's/\t/\\012/g'
			echo "\" },"
			rm -f cert.tmp cert.bin
		fi
	else
		echo "$x" >>cert.tmp
	fi
done <links.crt
echo '};'
) >../certs.inc
echo
echo Done.
echo
