#!/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/^ *//' | 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
			printf "`expr $line \* 100 / $total || true`%%\r" 1>&2
			base64 --decode <cert.tmp >cert.bin
			printf "{ `wc -c cert.bin|sed 's/^ *//'|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/	/\\000/g' | tr '\012' '\t' | sed 's/	/\\012/g'
			echo "\" },"
			rm -f cert.tmp cert.bin
		fi
	else
		echo "$x" >>cert.tmp
	fi
done <links.crt
echo '};'
) | sed 's/\\012$/" },/' | sed '/^"/d' >../certs.inc
echo
echo Done.
echo
