#! /bin/sh
# $Id$

distdir="$1"
echo "fixing distribution in $distdir..."

test -d "$distdir" || { echo not a dir in \$distdir ; exit 1 ; }
cd $distdir

if test "$DATEDVERSION"; then
	ed -s configure.in << END
/AC_INIT/s/AC_INIT(\([^,]*\), *[^,]*, *\(.*\))/AC_INIT(\1, $DATEDVERSION, \2)/
w
q
END
	error=WARN
else
    	error=ERROR
fi

ver=`sed -n 's/AC_INIT([^,]*,\([^,]*\),.*/\1/p' configure.in`
M="*   This is version $ver.   *"
echo "$M" | sed -e 's/./*/g'
echo "$M"
echo "$M" | sed -e 's/./*/g'

ed -s configure.in << END
/test -z/s,^,#,
w
q
END
autoreconf --force --install
(cd doc && makeinfo heimdal.texi)

find . -name Makefile.am | while read f; do
	for i in `sed -n -e '/^man_MANS/{
			:loop
			p
			/[^\\]$/b quit
			n
			b loop
			}
		:quit' $f | sed 's/man_MANS//;s/=//;s/[	\\][	\\]*/ /g'`; do 
		x=`dirname $f`/$i
		y=`dirname $f`/`echo $i | sed 's/[0-9]$/cat&/'`
		echo `grog -Tascii $x` \> $y
		`grog -Tascii $x` > $y
		if [ "X${error}" = "ERROR" ] ; then
			perl -p -e 'exit 1 if (/NetBSD|FreeBSD|OpenBSD|Linux|OSF|Solaris/); exit 0;' $y
			if [ $? != 0 ] ; then
		    		echo "$error: catfile $y contains operating system"
		    		head -1 $y
				exit 1
			fi
		fi
	done
done

make_proto () {
	(top=`pwd`
	cd $1
	b=`basename $1`
        if test X"$5" != X ; then
	    e="-E $5";
	else
	    e=
	fi
	perl $top/cf/make-proto.pl $e -o $2 -p $3 `(perl -p -e 's/^(include|if|else|endif)\b/##$1/' Makefile.am; 
		echo 'print-sources:; @if test "$(proto_opts)"; then echo $(proto_opts); else echo -q -P comment; fi; echo '$4 | sort -u ) | make -f - print-sources `)
}

make_proto lib/krb5 krb5-protos.h krb5-private.h '$(dist_libkrb5_la_SOURCES)' KRB5_LIB_FUNCTION
make_proto lib/kadm5 kadm5-protos.h kadm5-private.h '$(dist_libkadm5srv_la_SOURCES) $(dist_libkadm5clnt_la_SOURCES)'
make_proto lib/hdb hdb-protos.h hdb-private.h '$(dist_libhdb_la_SOURCES)'
make_proto lib/hx509 hx509-protos.h hx509-private.h '$(dist_libhx509_la_SOURCES)'
make_proto lib/gssapi /dev/null spnego/spnego-private.h '$(spnegosrc)'
make_proto lib/gssapi /dev/null krb5/gsskrb5-private.h '$(krb5src)'
make_proto lib/gssapi /dev/null ntlm/ntlm-private.h '$(ntlmsrc)'
make_proto lib/asn1 der-protos.h /dev/null '$(dist_libasn1_la_SOURCES)'
make_proto appl/login login_protos.h /dev/null '$(login_SOURCES)'
make_proto kcm kcm_protos.h /dev/null '$(kcm_SOURCES)'
make_proto kdc kdc-protos.h kdc-private.h '$(libkdc_la_SOURCES)'

# this assumes flex and bison

yaccify () {
    :
}

lexify () {
    :
}

yaccify lib/sl/slc-gram.y
yaccify lib/sl/parse.y
yaccify appl/ftp/ftpd/ftpcmd.y
yaccify lib/asn1/parse.y
yaccify lib/com_err/parse.y
yaccify lib/sl/parse.y
lexify lib/asn1/lex.l
lexify lib/com_err/lex.l
lexify lib/sl/lex.l
lexify lib/sl/slc-lex.l

#clean

rm fix-export make-release make-release.el
find . -name .cvsignore -print | xargs rm
find . -name .__afs\* -print | xargs rm
rm -fr autom4te*.cache



echo "tar cf - ${distdir} | gzip -9 > ${distdir}.tar.gz"
echo "gpg -ba -u 0x45D901D8 ${distdir}.tar.gz"

