#!/bin/sh
#
#	base/tools $Id$
# 	Author: Andrew Johnson (RGO)
# 	Date:	14-Mar-95
# 

# tool to build envData.c from envDefs.h and config/CONFIG*ENV

# Usage 	bldEnvData

HERE=`pwd`
cd $1
CONFIG_DIR=`pwd`
cd ${HERE}
SRC=../envDefs.h
ENV_DATA=${CONFIG_DIR}/CONFIG_ENV
SITE_DATA=${CONFIG_DIR}/CONFIG_SITE_ENV
OBJ=envData.c
TOOL=`basename $0`

# Start by creating a list of the ENV_PARAM declarations
PARAMS=`sed -n -e 's/;//' \
	-e 's/^[	 ]*epicsShareExtern[     ][      ]*READONLY[	 ][	 ]*ENV_PARAM[	 ][	 ]*//p' \
	${SRC}` 

# Create a new header file
rm -rf ${OBJ}
cat >${OBJ} <<!EOF
/* ${HERE}/${OBJ}
 *
 * created by ${TOOL}
 *
 * from:
 * ${SRC}
 * ${ENV_DATA}
 * ${SITE_DATA}
 *
 * `date`
 *
 */

#include "envDefs.h"
#include "shareLib.h"

!EOF

# Make sure no corresponding shell environment variables
unset ${PARAMS}

# Read the default values from the config file into shell variables
. ${ENV_DATA}
. ${SITE_DATA}

# Scan through the parameters to create the definition
for ENV in ${PARAMS}
do
	# Get the default, complain if not present
	if [ `set | grep -c ${ENV}=` = 0 ];
	then
		echo No default value for ${ENV}
		DEFAULT=""
	else
		VAR='$'${ENV}
		DEFAULT=`eval echo ${VAR}`
	fi

	# Add this definition to the header file
#	echo ${ENV} = ${DEFAULT}
	echo epicsShareDef READONLY ENV_PARAM ${ENV}'={"'${ENV}'","'${DEFAULT}'"};' >>${OBJ}
done

# Now create an array pointing to all parameters
cat >>${OBJ} <<!EOF

epicsShareDef READONLY ENV_PARAM* env_param_list[EPICS_ENV_VARIABLE_COUNT+1] = {
!EOF

# Contents are the addresses of each parameter
for ENV in ${PARAMS}
do
	echo '	&'${ENV}, >>${OBJ}
done

# Finally finish list with 0
cat >>${OBJ} <<!EOF
	0
};

!EOF

exit 0

