#!/bin/sh
# compile the printid utility
test -x ./printid || cc -o printid printid.c || exit 1
# Check file integrity if the md5sum utility can be found somewhere
# in the path.
sh -c 'pgp >/dev/null 2>&1'
if [ "$?" = "127" ]
then
echo "Cannot check PGP signature of Filelist, pgp not found" 1>&2
else
pgp Filelist 
fi
sh -c 'md5sum /dev/null >/dev/null 2>&1'
if [ "$?" = "127" ]
then
echo "Cannot check file integrity, md5sum utility not found" 1>&2
else
md5sum -c Filelist || exit 1
fi
# make the subs.sed file, which contains those variable substitutions which do
# not depend on commands.
trap "rm -f /tmp/conf$$" 0 1 2 3 15
awk 'BEGIN {
	OFS = FS = "\t"
	}
/^[#|]/	{ next }
/\?/	{
		printf "%s(yn)? [%s] ",substr($1,2), $2 > "/dev/tty"
		answer = ""
		getline answer < "/dev/tty"
		if (answer == "")
			answer = $2
		if (answer ~ /^[yY]/)
			value = $3
		else
			value = $4
		option[$5] = value
		next;
	}
	{
		printf "%s? [%s] ",$1,$2 > "/dev/tty"
		answer = ""
		getline answer < "/dev/tty"
		if (answer == "")
			answer = $2
		option[$3]= answer
	}
END	{
		printf "ok(yn)? [n] " > "/dev/tty"
		getline answer < "/dev/tty"
		if (answer ~ /^[yY]/)
		{
			for (i in option) 
				printf "s@_%s@%s@g\n", i, option[i]
		}
		else
		{
			exit 1
		}
	}' config.in > /var/tmp/conf$$ || exit 1
	mv /var/tmp/conf$$ subs.sed

# Now, get the values from the conmmands we saved.

sed -f subs.sed config.in | grep -e "^|" | sed -e "s/|//" |
awk 'BEGIN {
	FS = "\t"
	}
	{
		default = $2
		sign = $3
		$1 | getline
		print "s@_" sign "@" (length==0? default:$0) "@g"
	}' >> /tmp/conf$$ || exit 1
cat /tmp/conf$$ >>subs.sed
rm /tmp/conf$$
sed -f subs.sed Makefile.in > Makefile
