#!/bin/sh

TEMPLATE_DIR=/usr/share/bes/templates
if [ ! -d ${TEMPLATE_DIR} ]
then
    echo "the templates directory does not exist."
    echo "It containes template files for use by your new server"
    exit 1
fi

echo "Enter server type, e.g. cedar, fits, netcdf: "
read serverType
if [ "$serverType" = "" ]
then
    echo "you must enter a server type"
    exit 1
fi

echo "Enter C++ class prefix, e.g. Cedar, Fits, Netcdf: "
read classPrefix
if [ "$classPrefix" = "" ]
then
    echo "you must enter a class prefix"
    exit 1
fi

DEFAULT_RESPONSE="das dds data"
RESPONSE_TYPES=
echo "Enter response types handled by this server (das dds data):"
echo "(space separated , help and version are added for you, no need to include them here)"
read responseTypes
if [ "$responseTypes" = "" ]
then
    RESPONSE_TYPES=${DEFAULT_RESPONSE}
else
    RESPONSE_TYPES=${responseTypes}
fi

for i in RequestHandler.cc RequestHandler.h ResponseNames.h
do
/bin/cp ${TEMPLATE_DIR}/${i} ./${classPrefix}${i}
ed ./${classPrefix}${i} << EOF > /dev/null 2>&1
,s/OPENDAP_CLASS/${classPrefix}/g
,s/OPENDAP_TYPE/${serverType}/g
w
q
EOF
done

for i in Module.h Module.cc
do
/bin/cp ${TEMPLATE_DIR}/${i} ./${classPrefix}${i}
ed ./${classPrefix}${i} << EOF > /dev/null 2>&1
,s/OPENDAP_CLASS/${classPrefix}/g
w
q
EOF
done

#for i in _handler.cc
#do
#/bin/cp ${TEMPLATE_DIR}/${i} ./${serverType}${i}
#ed ./${serverType}${i} << EOF > /dev/null 2>&1
#,s/OPENDAP_CLASS/${classPrefix}/g
#,s/OPENDAP_TYPE/${serverType}/g
#w
#q
#EOF
#done

#/bin/cp ${TEMPLATE_DIR}/config config_${serverType}.h.in
#ed ./config_${serverType}.h.in << EOF > /dev/null 2>&1
#,s/OPENDAP_CLASS/${classPrefix}/g
#,s/OPENDAP_TYPE/${serverType}/g
#w
#q
#EOF

for i in Makefile.am.template configure.ac
do
/bin/cp ${TEMPLATE_DIR}/${i} ./${i}
ed ./${i} << EOF > /dev/null 2>&1
,s/OPENDAP_CLASS/${classPrefix}/g
,s/OPENDAP_TYPE/${serverType}/g
w
q
EOF
done
/bin/mv Makefile.am.template Makefile.am

/bin/mkdir conf
/bin/cp ${TEMPLATE_DIR}/conf/* conf

for i in $RESPONSE_TYPES
do
    ed ${classPrefix}RequestHandler.h << EOF > /dev/null 2>&1
1
/${serverType}_build_vers
-1
a
    static bool		${serverType}_build_${i}( BESDataHandlerInterface &dhi ) ;
.
w
q
EOF
    responseName=
    if [ "$i" = "das" ]
    then
	responseName="DAS"
	headerFile="BESDASResponse.h"
	castLine1="BESResponseObject *response ="
	castLine2="dhi.response_handler->get_response_object();"
	castLine3="BESDASResponse *bdas = dynamic_cast < BESDASResponse * >(response);"
	castLine4="DAS *das = bdas->get_das();"
    elif [ "$i" = "dds" ]
    then
	responseName="DDS"
	headerFile="BESDDSResponse.h"
	castLine="DDS *dds = dynamic_cast<DDS *>(dhi.response_handler->get_response_object());"
	castLine1="BESResponseObject *response ="
	castLine2="dhi.response_handler->get_response_object();"
	castLine3="BESDDSResponse *bdds = dynamic_cast < BESDDSResponse * >(response);"
	castLine4="DDS *dds = bdds->get_dds();"
    elif [ "$i" = "data" ]
    then
	responseName="DATA"
	headerFile="BESDataDDSResponse.h"
	castLine="DDS *dds = dynamic_cast<DDS *>(dhi.response_handler->get_response_object());"
	castLine1="BESResponseObject *response ="
	castLine2="dhi.response_handler->get_response_object();"
	castLine3="BESDataDDSResponse *bdds = dynamic_cast < BESDataDDSResponse * >(response);"
	castLine4="DataDDS *dds = bdds->get_dds();"
    else
	responseName="$i"
	headerFile=
	castLine1="BESTextInfo *info = dynamic_cast<BESTextInfo *>(dhi.response_handler->get_response_object());"
	castLine2=""
	castLine3=""
	castLine4=""
	ed ${classPrefix}ResponseNames.h << EOF > /dev/null 2>&1
/${classPrefix}_NAME
a
    #define ${i}_RESPONSE "${i}"
.
w
q
EOF
	ed ${classPrefix}Module.cc << EOF > /dev/null 2>&1
/${classPrefix}ResponseNames
a
#include "${classPrefix}${i}ResponseHandler.h"
.
/INIT_END
-1
a
    if( BESLog::TheLog()->is_verbose() )
	(*BESLog::TheLog()) << "    adding " << ${i}_RESPONSE << " response handler" << endl ;
    BESResponseHandlerList::TheList()->add_handler( ${i}_RESPONSE, ${classPrefix}${i}ResponseHandler::${classPrefix}${i}ResponseBuilder ) ;

    cmd_name = string( GET_RESPONSE ) + "." + ${i}_RESPONSE ;
    if( BESLog::TheLog()->is_verbose() )
	(*BESLog::TheLog()) << "    adding " << cmd_name << " command" << endl;
    BESCommand::add_command( cmd_name, BESCommand::TermCommand ) ;

.
w
q
EOF

    /bin/cp ${TEMPLATE_DIR}/ResponseHandler.h ${classPrefix}${i}ResponseHandler.h
    ed ./${classPrefix}${i}ResponseHandler.h << EOF > /dev/null 2>&1
,s/OPENDAP_RESPONSE/${classPrefix}${i}/g
w
q
EOF
    /bin/cp ${TEMPLATE_DIR}/ResponseHandler.cc ${classPrefix}${i}ResponseHandler.cc
    ed ./${classPrefix}${i}ResponseHandler.cc << EOF > /dev/null 2>&1
,s/OPENDAP_RESPONSE/${classPrefix}${i}/g
w
q
EOF

    ed ./Makefile.am << EOF > /dev/null 2>&1
/^BES_SRCS
a
	${classPrefix}${i}ResponseHandler.cc \\
.
/^BES_HDRS
a
	${classPrefix}${i}ResponseHandler.h \\
.
w
q
EOF

    fi

    if [ "$headerFile" != "" ]
    then
	ed ${classPrefix}RequestHandler.cc << EOF > /dev/null 2>&1
1
/BESTextInfo
a
#include "${headerFile}"
.
w
q
EOF
    fi
    ed ${classPrefix}RequestHandler.cc << EOF > /dev/null 2>&1
1
/VERS_RESPONSE
-1
a
    add_handler( ${responseName}_RESPONSE, ${classPrefix}RequestHandler::${serverType}_build_${i} ) ;
.
1
/::${serverType}_build_vers(
-2
a
bool
${classPrefix}RequestHandler::${serverType}_build_${i}( BESDataHandlerInterface &dhi )
{
    bool ret = true ;
    ${castLine1}
    ${castLine2}
    ${castLine3}
    ${castLine4}

    // Your code goes here

    return ret ;
}

.
w
q
EOF

done

/bin/cp ${TEMPLATE_DIR}/bes.conf ./bes.conf
ed ./bes.conf << EOF > /dev/null 2>&1
,s/OPENDAP_CLASS/${classPrefix}/g
,s/OPENDAP_TYPE/${serverType}/g
w
q
EOF

echo "*******************************************************"
echo "*                                                     *"
echo "* Creating your configuration script                  *"
echo "*                                                     *"
echo "*******************************************************"
autoreconf
if [ $? != 0 ]
then
    echo "FAILED"
    exit 1
fi

echo "*******************************************************"
echo "*                                                     *"
echo "* Running your configuration script                   *"
echo "*                                                     *"
echo "*******************************************************"
./configure
if [ $? != 0 ]
then
    echo "FAILED"
    exit 1
fi

echo "*******************************************************"
echo "*                                                     *"
echo "* Building your server                                *"
echo "*                                                     *"
echo "*******************************************************"
make
if [ $? != 0 ]
then
    echo "FAILED"
    exit 1
fi

echo "*******************************************************"
echo "*                                                     *"
echo "* Your ${classPrefix} files have been created"
echo "*                                                     *"
echo "* You will need to make modifications to some of the  *"
echo "* code created here. Please refer to the document     *"
echo "* OPeNDAPCreatingServer for more information          *"
echo "*                                                     *"
echo "*******************************************************"

