#!/bin/sh
# configSvc
# Usage:   configSvc services=svcname0 svcname1 svcname3 ... 
#	             headers=header0 header1
# Purpose: construct a CDEV static service finder routine
# Author:  Jie Chen
# CEBAF Data Acquisition Group
# configSvc,v
# Revision 1.16  1998/02/13  14:00:20  chen
# add cdevDirRequestObj
#
# Revision 1.15  1998/01/14  13:38:38  chen
# fix a small problem
#
# Revision 1.14  1997/12/22  18:23:04  akers
# Ongoing development of 1.6.2
#
# Revision 1.13  1997/08/21  13:30:45  chen
# move -D_CDEV_ACCOUNTING_ to Makefile.config
#
# Revision 1.12  1997/08/15  18:10:54  akers
# Addition of CDEV Accounting
#
# Revision 1.11  1997/03/26  15:49:52  akers
# Ongoing development...
#
# Revision 1.10  1997/03/25  22:24:32  akers
# Development in support of a new cdevDirectory
#
# Revision 1.9  1996/10/15  12:54:04  akers
# Minor Change...
#
# Revision 1.8  1996/10/07  12:36:24  akers
# Minor Change
#
# Revision 1.7  1996/10/02  17:17:00  akers
# Inserted setup for Makefile.common
#
# Revision 1.6  1996/09/20  12:24:51  akers
# Changes added for Release 1.4
#
# Revision 1.4  1996/06/26  15:54:04  akers
# Modifications to support multiple OS Versions
#
# Revision 1.3  1996/05/14  20:37:58  akers
# Makefile modifications
#
# Revision 1.2  1995/11/14  17:14:44  chen
# change to work under new makefiles
#
#

configf=./directory/cdevSvcConfig.cc
makef=./directory/Makefile
os=`uname`

case $os in
Linux)
    ECHO='echo -e';;
linux)
    ECHO='echo -e';;
*)
    ECHO=echo;;
esac

case $# in
0) $ECHO 'Usage: configSvc services=svcname ... headers=header0.. flags=flags0 ..\n. Generating Makefile and cdevSvcConfig.cc for dynamic loading only' 1>&2
esac

# remove old configuration file and makefile first
if test -f $configf
then
	rm -f $configf
fi
if test -f $makef
then
	rm -f $makef
fi

# first get first service and first header
for arg
do
case $arg in
services=*)
	firstsvc=`$ECHO $arg | sed 's/[a-z]*=//'`;;
headers=*)
	firsthdr=`$ECHO $arg | sed 's/[a-z]*=//'`;;
flags=*)
	firstflg=`$ECHO $arg | sed 's/[a-z]*=//'`;;
*)
esac
done

hassvc=false

#check empty service and header
case $firstsvc in
"") $ECHO "no service provided";;
*)hassvc=true;;
esac

case $firsthdr in
"") $ECHO "no header directories provided"
    case $hassvc in
    true) $ECHO "Need header file location for services"
	  exit 2;;
    esac;;
*)
esac

case $firstflg in
"") $ECHO "no compiling flags provided";;
esac

# copy common stuff to the top of the files
# c++ file
$ECHO // cdevSvcConfig.cc generated by configSvc, do not edit  > $configf
$ECHO "#ifdef SHOBJ" >> $configf
$ECHO "	// ******************************************" >> $configf
$ECHO "	// * This section added to provide a credible" >> $configf
$ECHO "	// * object file for the shared library..." >> $configf
$ECHO "	// ******************************************" >> $configf
$ECHO "	int CDEV_SVC_CONFIG_SHARED = 1;" >> $configf
$ECHO "#else"      >> $configf

# loop over first to include all headers and find out the last arguments
lastargument=nothing
svcs=true
hflag=false
fflag=false

for arg
do
	case $arg in
	services=*) 
		$ECHO "#include <"$firstsvc"Service.h>" >> $configf
		lastargument=$firstsvc;;
	headers=*) 
		headers=-I$firsthdr
		lastargument=$firsthdr
		hflag=true
		svcs=false;;
	flags=*)
		flags=-D$firstflg
		lastargument=$firstflg
		fflag=true
		hflag=false
		svcs=false;;
	*)
		lastargument=$arg
		case $svcs in
		false);;
		true)
			$ECHO "#include <"$arg"Service.h>" >> $configf
		esac
		case $hflag in
		false);;
		true)
			headers="$headers -I$arg"
		esac
		case $fflag in
		false);;
		true)
			flags="$flags -D$arg"
		esac
	esac
done
$ECHO "#include \"cdevSvcFinder.h\" " >> $configf
$ECHO "#include <ctype.h>" >> $configf
$ECHO "                  " >> $configf
$ECHO "                  " >> $configf
$ECHO "cdevService*      " >> $configf
$ECHO "cdevSvcFinder::findService (char* serviceName)" >> $configf
$ECHO "{"                  >> $configf
$ECHO "	cdevService* svc = 0;" >> $configf

# beginning of the loop indicator
svcs=true

# now loop through all services
for arg 
do
	case $arg in
	services=*) 
		$ECHO configuring service with name "$firstsvc"Service ......		
		$ECHO "	if (::strcmp (serviceName, \""$firstsvc"Service\") == 0) {" >> $configf
		$ECHO " 	   if (!system_.serviceCreated (serviceName))" >> $configf
		$ECHO "		svc = new "$firstsvc"Service (serviceName, system_);" >> $configf
		$ECHO "	   else" >> $configf
		$ECHO "		svc = system_.service (serviceName);" >> $configf
		$ECHO "	}" >> $configf;;
	headers=*)
		svcs=false;;
	flags=*)
		svcs=false;;
	*)
		case $svcs in
		true)
			$ECHO configuring service with name "$arg"Service ......
			$ECHO "	else if (::strcmp (serviceName, \""$arg"Service\") == 0) {" >> $configf;
			$ECHO " 	   if (!system_.serviceCreated (serviceName))" >> $configf
			$ECHO "		svc = new "$arg"Service (serviceName, system_);" >> $configf
			$ECHO "	   else" >> $configf
			$ECHO "		svc = system_.service (serviceName);" >> $configf
			$ECHO "	}" >> $configf
		esac
	esac
done
$ECHO "	return svc;"      >> $configf
$ECHO "}"                  >> $configf
$ECHO "#endif"             >> $configf


$ECHO "#      Makefile generated by configSvc. Do not Edit" > $makef
$ECHO "include ../Makefile.config" >> $makef
$ECHO "                                       " >> $makef
$ECHO "ifeq (\$(INCDIR), )" >> $makef
$ECHO "     INCDIR = ../.." >> $makef
$ECHO "endif"               >> $makef
$ECHO "          "          >>  $makef
$ECHO "ifeq (\$(SHOBJ),YES)" >> $makef
$ECHO "	OBJTYPE=.shobj/\$(TARGETDIR)" >> $makef
$ECHO "else" >> $makef
$ECHO "	OBJTYPE=.obj/\$(TARGETDIR)" >> $makef
$ECHO "endif" >> $makef
$ECHO "                                       " >> $makef
$ECHO "ifeq (\$(SHOBJ), YES)" >> $makef
$ECHO "XTRACXXFLAGS = -I/usr/include/CC $flags" >> $makef
$ECHO "else" >> $makef
$ECHO "XTRACXXFLAGS = -I/usr/include/CC $headers $flags" >> $makef
$ECHO "endif" >> $makef
$ECHO "            " >> $makef
$ECHO "OBJS = \$(OBJTYPE)/cdevSvcFinder.o \$(OBJTYPE)/cdevDirectory.o \$(OBJTYPE)/cdevDirectoryTable.o \$(OBJTYPE)/cdevDirectoryTool.o \$(OBJTYPE)/cdevDirRequestObj.o \$(OBJTYPE)/cdevSvcConfig.o" >> $makef
$ECHO "            " >> $makef
$ECHO "targets: \$(OBJTYPE) \$(OBJDIR)/\$(OBJTYPE) copyobjs copyincs" >> $makef
$ECHO "            " >> $makef
$ECHO "\$(OBJTYPE) :" >> $makef
$ECHO "	@mkdir -p \$(OBJTYPE)" >> $makef
$ECHO "            " >> $makef
$ECHO "\$(OBJDIR)/\$(OBJTYPE) :" >> $makef
$ECHO "	@mkdir \$(OBJDIR)/\$(OBJTYPE)" >> $makef
$ECHO "            " >> $makef
$ECHO "copyobjs: \$(OBJS)" >> $makef
$ECHO "	cp \$(OBJS) \$(OBJDIR)/\$(OBJTYPE)" >> $makef
$ECHO "                      " >> $makef
$ECHO "copyincs:" >> $makef
$ECHO "	cp *.h \$(INCDIR)" >> $makef
$ECHO "                      " >> $makef
$ECHO "                      " >> $makef
$ECHO "clean: clean_hail" >> $makef
$ECHO "	rm -rf *.o *.a *~ *.sl .obj .shobj core prtrpository" >> $makef


#create Makefile.archive
$ECHO "# ***********************************************************************" >  ../include/makeinclude/Makefile.archive
$ECHO "# * Makefile.archive                                                    *" >> ../include/makeinclude/Makefile.archive
$ECHO "# *       The ARCHIVELIBS variable should contain a complete list of    *" >> ../include/makeinclude/Makefile.archive
$ECHO "# *       all libraries that will be needed by an ARCHIVE cdev program. *" >> ../include/makeinclude/Makefile.archive
$ECHO "# *       The variable should contain the path specification and the    *" >> ../include/makeinclude/Makefile.archive
$ECHO "# *       name of the library in the format that is required by your    *" >> ../include/makeinclude/Makefile.archive
$ECHO "# *       platform's linker.                                            *" >> ../include/makeinclude/Makefile.archive
$ECHO "# ***********************************************************************" >> ../include/makeinclude/Makefile.archive
$ECHO "ARCHIVELIBS = -L\$(CDEVLIB) \c" >> ../include/makeinclude/Makefile.archive
# now loop through all services
for arg in $*
do
	case $arg in
		services=*) $ECHO "-l${firstsvc}Service \c" >> ../include/makeinclude/Makefile.archive;;
		headers=*)  break ;;
		*)          $ECHO "-l${arg}Service \c" >> ../include/makeinclude/Makefile.archive
	esac
done
$ECHO " " >> ../include/makeinclude/Makefile.archive
$ECHO "*************************************************************************"
$ECHO "* Modifications Finished!                                               *"
$ECHO "*                                                                       *"
$ECHO "* You may need to add additional, platform-specific archive libraries   *"
$ECHO "* to the definition in the file ../include/makeinclude/Makefile.archive *"
$ECHO "*                                                                       *"
$ECHO "* For example:  If you are using EPICS you will need to append the      *"
$ECHO "* following string to the ARCHIVELIBS definition in Makefile.archive in *"
$ECHO "* order to include the necessary libraries...                           *"
$ECHO "*                                                                       *"
$ECHO "*       -L\$(EPICSLIB) -lca -lCom -lDb                                   *"
$ECHO "*                                                                       *"
$ECHO "* Other custom services may also require additional library             *"
$ECHO "* definitions, consult the service's user documentation for information *"
$ECHO "*************************************************************************"
# finished and quit
exit 0
