#! /bin/sh

# $NetBSD: heimdal2netbsd,v 1.9 2004/09/14 08:08:20 lha Exp $

if [ \! -f kuser/kinit.c ]; then 
	echo "`basename $0`: should be run in top src directory" 1>&2
	exit 1
fi

if ! type perl > /dev/null 2>&1 ; then
	echo "`basename $0`: you need perl to run this" 1>&2
	exit 1
fi


echo 'Removing preformatted man pages.'
find . -name '*.cat[1358]' | xargs rm -f
rm doc/heimdal.info

echo 'Fixing .Os in man pages.'
perl -i~ -p -e 's/\.Os HEIMDAL/.Os/' `find . -name '*.[1358]'`

perl -i~ -p -e 's/\.Pa krb5.h/.Pa krb5\/krb5.h/' ./lib/krb5/krb5.3
perl -i~ -p -e 's/\.Pa gssapi.h/.Pa gssapi\/gssapi.h/' ./lib/gssapi/gssapi.3

perl -i~ -p -e 's/^#include <krb5.h>/#include <krb5\/krb5.h>/' \
	./lib/krb5/krb5_verify_user.3
perl -i~ -p -e 's/\.In krb5.h/.In krb5\/krb5.h/' \
	`find . -name '*.[1358]'`
perl -i~ -p -e 's/\.In gssapi.h/.In gssapi\/gssapi.h/' \
	`find . -name '*.[1358]'`

echo 'Changing CVS Id tags to Heimdal tags.'
## Ugh!
set -- 'RCSID\("'	'"\)'	'__RCSID("'	'"\n        "\$NetBSD\$")' \
	'/\* '		' \*/'	'/* '		'\n   \$NetBSD\$ */'       \
	'# '		''	'# '		'\n# \$NetBSD\$'           \
	'\.\\" '	''	'.\\" '		'\n.\\" \$NetBSD\$'        \
	'dnl '		''	'dnl '		'\ndnl \$NetBSD\$'


e=
while [ $# -gt 0 ]; do
	e="${e}s,$1\\\$Id([^\\\$]*)\\\$$2,$3\\\$Heimdal\\1\\\$$4,;"
	shift 4
done

perl -i~ -p -e "$e" `find . -type f`

echo 'Inlining <krb5-types.h> in krb5.h and gssapi.h.'
perl -i~ -p -e 's,#include <krb5-types.h>,#include <sys/types.h>\n#include <inttypes.h>\n#include <sys/socket.h>\ntypedef socklen_t krb5_socklen_t;\ntypedef ssize_t krb5_ssize_t;,' lib/gssapi/gssapi.h lib/krb5/krb5.h

echo 'Fixing up include paths:'

update_inc() {
	echo " fixing $1 leaving old file in $1~"
	mv $1 $1~
	< $1~ > $1 \
	sed -e 's,#include <asn1_err.h>,#include <krb5/asn1_err.h>,'	      \
	    -e 's,#include <krb5_err.h>,#include <krb5/krb5_err.h>,'	      \
	    -e 's,#include <heim_err.h>,#include <krb5/heim_err.h>,'	      \
	    -e 's,#include <k524_err.h>,#include <krb5/k524_err.h>,'	      \
	    -e 's,#include <krb5_asn1.h>,#include <krb5/krb5_asn1.h>,'	      \
	    -e 's,#include <hdb_asn1.h>,#include <krb5/hdb_asn1.h>,'	      \
	    -e 's,#include <hdb_err.h>,#include <krb5/hdb_err.h>,'	      \
	    -e 's,#include <hdb-protos.h>,#include <krb5/hdb-protos.h>,'      \
	    -e 's,#include <krb5-protos.h>,#include <krb5/krb5-protos.h>,'    \
	    -e 's,#include <sl.h>,#include <krb5/sl.h>,'
}

update_inc lib/krb5/krb5.h
update_inc lib/hdb/hdb.h
update_inc lib/sl/ss.h

rm -r doc/standardisation
echo 'commands to run:
echo '   configure --prefix=/usr --with-krb4=/usr --with-krb4-include=/usr/include/kerberosIV'
echo '   find . -name Makefile.in -o -name Makefile -o -name \*~ -exec rm {} \;'
echo 'Remember to update files in basesrc/include/heimdal/.'
echo '   cvs -d ... import src/crypto/dist/heimdal HEIMDAL heimdal-0-...'
echo '   cvs -d ... checkout -kk -jheimdal-old -jheimdal-new src/crypto/dist/heimdal'

exit 0
