#!/bin/sh -
#	$Id: s_rpc,v 11.13 2002/01/07 13:34:45 bostic Exp $
#
# Build the automatically generated RPC files

msgc="/* DO NOT EDIT: automatically built by dist/s_rpc. */"

echo "Building RPC client/server files"

. ./RELEASE

client_file=../rpc_client/gen_client.c
ctmpl_file=./template/gen_client_ret
defs_file=../include_auto/rpc_defs.in
header_file=../include_auto/db_server.h
rpcclnt_file=../rpc_client/db_server_clnt.c
rpcsvc_file=../rpc_server/c/db_server_svc.c
rpcxdr_file=../rpc_server/c/db_server_xdr.c
sed_file=../rpc_server/c/db_server_proc.sed
server_file=../rpc_server/c/gen_db_server.c
stmpl_file=./template/db_server_proc
xdr_file=../rpc_server/db_server.x

#
# NOTE: We do NOT want to remove proc_file.  It is what we apply
# sed_file to, but we do not want to remove it.
#
dbinc_file=../include/db.in
proc_file=../rpc_server/c/db_server_proc.c
svrsed_file=../rpc_server/c/db_server_svc.sed
rpcsed_file=../rpc_server/db_server.sed

xidsize=`awk '/^#define/ { if ($2 == "DB_XIDDATASIZE") { print $3 }}' $dbinc_file`

rm -f   $client_file $ctmpl_file $header_file $rpcsvc_file $rpcclnt_file \
	$rpcxdr_file $sed_file $server_file $stmpl_file $xdr_file

#
# Generate client/server/XDR code
#
awk -f gen_rpc.awk \
    -v major=$DB_VERSION_MAJOR \
    -v minor=$DB_VERSION_MINOR \
    -v xidsize=$xidsize \
    -v client_file=$client_file \
    -v ctmpl_file=$ctmpl_file \
    -v sed_file=$sed_file \
    -v server_file=$server_file \
    -v stmpl_file=$stmpl_file \
    -v xdr_file=$xdr_file < ../rpc_server/rpc.src
chmod 444 $client_file $server_file

#
# Now run rpcgen to generate all our sources from the XDR file
#
rpcgen -h $xdr_file > $header_file
rpcgen -l $xdr_file > $rpcclnt_file
rpcgen -s tcp $xdr_file > $rpcsvc_file
rpcgen -c $xdr_file > $rpcxdr_file

# Run server files through sed
sed -f $svrsed_file $rpcsvc_file > ${rpcsvc_file}.new
mv ${rpcsvc_file}.new $rpcsvc_file
sed -f $sed_file $proc_file > ${proc_file}.new
mv ${proc_file}.new $proc_file

# Run rpcgen files through sed to add HAVE_RPC ifdef
sed -f $rpcsed_file $rpcsvc_file > ${rpcsvc_file}.new
mv ${rpcsvc_file}.new $rpcsvc_file
sed -f $rpcsed_file $rpcxdr_file > ${rpcxdr_file}.new
mv ${rpcxdr_file}.new $rpcxdr_file
sed -f $rpcsed_file $rpcclnt_file > ${rpcclnt_file}.new
mv ${rpcclnt_file}.new $rpcclnt_file

# Move the DB_RPC SERVER #defines into a separate file so
# they can be part of db.h.
(echo  "" && echo "$msgc" &&
    sed -n -e "/DB_RPC_SERVER/p" $header_file) > $defs_file
sed -e "/DB_RPC_SERVER/d" $header_file > ${header_file}.new
mv ${header_file}.new $header_file

chmod 444 $header_file $rpcclnt_file $rpcsvc_file $rpcxdr_file
