#!/bin/sh
set -e
LC_ALL=C
export LC_ALL
if ! which idn2 >/dev/null; then
	echo idn2 not installed
	exit 1
fi
echo
echo Generating suffix tables.
for file in suffix suffix_x; do
	printf $file" "
	if [ $file = suffix ]; then
		filter="grep -v !"
	else
		filter="sed -n s/^[!]//p"
	fi
	(
	echo '/* Automatically generated by gen-suffix */'
	echo
	echo "static_const const_char_ptr domain_$file[] = {"
	(cat public_suffix_list.dat; echo onion) | sed 's/[ 	][ 	]*$//' | grep -v '\/\/' | grep -v '^$' | $filter | LC_ALL=en_US.utf8 idn2 | sort -u | sed 's/^\(.*\)$/	"\1",/'
	echo "};"
	) >../$file.inc
	if grep -q '[^"/ ]\*' ../$file.inc; then
		echo
		echo Wildcard can be only on the beginning. Fix the code.
		exit 1
	fi
done
echo
echo Done.
echo