#!/bin/sh -
#	$Id: s_vxworks,v 1.21 2001/10/05 02:27:33 bostic Exp $
#
# Build the VxWorks files.

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

. RELEASE

s=/tmp/__db_a
t=/tmp/__db_b

trap 'rm -f $s $t ; exit 0' 0
trap 'rm -f $s $t ; exit 1' 1 2 3 13 15

f=../build_vxworks/db.h
cat <<ENDOFSEDTEXT > $s
/extern "C" {/{
n
n
i\\
\\
/* Tornado 2 does not provide a standard C pre-processor #define. */\\
#ifndef __vxworks\\
#define	__vxworks\\
#endif
}
s/@u_int8_decl@/typedef unsigned char u_int8_t;/
s/@u_int16_decl@/typedef unsigned short u_int16_t;/
s/@u_int32_decl@/typedef unsigned int u_int32_t;/
/@int16_decl@/d
/@int32_decl@/d
/@u_char_decl@/d
/@u_short_decl@/d
/@u_int_decl@/d
/@u_long_decl@/d
/@ssize_t_decl@/d
s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/
s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/
s/@DB_VERSION_PATCH@/$DB_VERSION_PATCH/
s/@DB_VERSION_STRING@/"$DB_VERSION_STRING"/
ENDOFSEDTEXT
(echo "$msgc" &&
    sed -f $s ../include/db.in &&
    cat ../include_auto/rpc_defs.in &&
    cat ../include_auto/global_ext.in) > $t
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

f=../build_vxworks/db_int.h
cat <<ENDOFSEDTEXT > $s
s/\(PATH_SEPARATOR[^"]*"\)\/"/\1\/\\\\\\\\\\"/
s/@db_align_t_decl@/typedef unsigned long db_align_t;/
s/@db_alignp_t_decl@/typedef unsigned long db_alignp_t;/
ENDOFSEDTEXT
(echo "$msgc" && sed -f $s ../include/db_int.in) > $t
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build VxWorks Tornado 2.0 project files for the utilities.
transform()
{
	local i

# Build a sed script that will add argument parsing support and rename all
# of the functions to be private to this file.
cat <<ENDOFSEDTEXT
/^main(argc, argv)$/{
i\\
$1(args)\\
\\	char *args;\\
{\\
\\	int argc;\\
\\	char **argv;\\
\\
\\	__db_util_arg("$1", args, &argc, &argv);\\
\\	return ($1_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);\\
}\\
\\
#include <stdio.h>\\
#define	ERROR_RETURN	ERROR\\
\\
int\\
$1_main(argc, argv)
d
}
/^	while ((ch = getopt/i\\
\\	__db_getopt_reset = 1;
/^	extern int optind;/s/;/, __db_getopt_reset;/
ENDOFSEDTEXT

# Replace all function names with VxWorks safe names.
# Function names are:
#	Tokens starting at the beginning of the line, immediately followed
#	by an opening parenthesis.
# Replace:
#	Matches preceded by a non-C-token character and immediately followed
#	by an opening parenthesis.
#	Matches preceded by a non-C-token character and immediately followed
#	by " __P".
#	Matches starting at the beginning of the line, immediately followed
#	by an opening parenthesis.
	for i in `sed -e 's/^\([a-zA-Z_][a-zA-Z0-9_]*\)(.*$/\1/p' -e d $2`; do
		echo "s/\([^a-zA-Z0-9_]\)\($i(\)/\1$1_\2/g"
		echo "s/\([^a-zA-Z0-9_]\)\($i[	 ]__P\)/\1$1_\2/g"
		echo "s/^\($i(\)/$1_\1/g"
	done

# There is a special case the rules above don't catch.  There's a txn_compare
# function, used as an argument to qsort(3).  Catch it explicitly.
	echo "s/, txn_compare);/, db_stat_txn_compare);/"
}

for i in db_archive db_checkpoint db_deadlock \
    db_dump db_load db_printlog db_recover db_stat db_upgrade db_verify; do

	transform $i ../$i/$i.c > $s
	sed -f $s < ../$i/$i.c > $t

	f=../build_vxworks/$i/$i.c
	cmp $t $f > /dev/null 2>&1 ||
	    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

	sed "s/__DB_APPLICATION_NAME__/$i/g" < wpj.in > $t
	f=../build_vxworks/$i/$i.wpj
	cmp $t $f > /dev/null 2>&1 ||
	    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

	for j in \
	    Makefile.component Makefile.custom component.cdf component.wpj; do
		#
		# Some parts of the component files needs to have the
		# name in CAPS.  Sigh.
		#
		z=`echo $i | tr "a-z" "A-Z"`
		sed -e "s/__DB_APPLICATION_NAME__/$i/g" \
		    -e "s/__DB_CAPAPPL_NAME__/$z/g" < vx_ae.in/$j > $t
		f=../build_vxworks/$i/$i/$j
		cmp $t $f > /dev/null 2>&1 ||
		    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)
	done
done
