#!/bin/sh

echo "generating rpc-code"

rpcgen rpc.x
rpcgen -m rpc.x > rpc_svc.c
mv rpc_clnt.c rpc_clnt.cc
mv rpc_svc.c rpc_svc.cc
mv rpc_xdr.c rpc_xdr.cc

# boilrpc_call_1 for the client is renamed to boilrpc_call_1_clnt to avoid
# a name clash with the server code

sed -e "s/boilrpc_call_1(rpc_callargs/boilrpc_call_1_clnt(rpc_callargs/g" \
rpc_clnt.cc > rpc_clnt.cc1
mv rpc_clnt.cc1 rpc_clnt.cc

sed -e "s/boilrpc_call_1(rpc_callargs/boilrpc_call_1_clnt(rpc_callargs/g" \
rpc.h > rpc.h1
mv rpc.h1 rpc.h

# rpc_svc_fork.cc, the forking version of the servercode, is generated from
# rpc_svc.cc

cp rpc_svc.cc rpc_svc_fork.cc
sed -e "s/result = (\*local)/pid_t childpid;if ((childpid=fork())) { fork_handler(childpid);svc_freeargs(transp, xdr_argument, (caddr_t) \&argument); return; }; result = (\*local)/g" \
rpc_svc_fork.cc > rpc_svc_fork.cc1
mv rpc_svc_fork.cc1 rpc_svc_fork.cc

sed -e "s/if (!svc_freeargs(/_exit(0);if (!svc_freeargs(/g" \
rpc_svc_fork.cc > rpc_svc_fork.cc1
mv rpc_svc_fork.cc1 rpc_svc_fork.cc

sed -e "s/boilrpc_1(struct svc_req /boilrpc_1_fork(struct svc_req /g" \
rpc_svc_fork.cc > rpc_svc_fork.cc1
mv rpc_svc_fork.cc1 rpc_svc_fork.cc
