#!/bin/sh

#
# Script used for preparing DM development
#
# Usage:
#
# $0 
#

MY_DIR=`dirname $0` && cd $MY_DIR && MY_DIR=`pwd`
if [ -z "${DM_ROOT_DIR}" ]; then
    DM_ROOT_DIR=$MY_DIR/..
fi
DM_ENV_FILE=${DM_ROOT_DIR}/setup.sh
if [ ! -f ${DM_ENV_FILE} ]; then
    echo "Environment file ${DM_ENV_FILE} does not exist." 
    exit 2
fi
. ${DM_ENV_FILE} > /dev/null
DM_INSTALL_DIR=${DM_INSTALL_DIR:=$DM_ROOT_DIR/..}
DM_ETC_DIR=${DM_INSTALL_DIR}/etc
DM_LOG_DIR=${DM_INSTALL_DIR}/var/log

DM_DB_NAME=dm
DM_DB_USER=${DM_DB_NAME}
DM_DS_WEB_SERVICE_CONFIG_FILE=${DM_ETC_DIR}/${DM_DB_NAME}.ds-web-service.conf
DM_DS_WEB_SERVICE_LOG_FILE=${DM_LOG_DIR}/${DM_DB_NAME}.ds-web-service.log
DM_DAQ_WEB_SERVICE_CONFIG_FILE=${DM_ETC_DIR}/${DM_DB_NAME}.daq-web-service.conf
DM_DAQ_WEB_SERVICE_LOG_FILE=${DM_LOG_DIR}/${DM_DB_NAME}.daq-web-service.log
DM_CAT_WEB_SERVICE_CONFIG_FILE=${DM_ETC_DIR}/${DM_DB_NAME}.cat-web-service.conf
DM_CAT_WEB_SERVICE_LOG_FILE=${DM_LOG_DIR}/${DM_DB_NAME}.cat-web-service.log
DM_DB_PASSWORD_FILE=$DM_INSTALL_DIR/etc/${DM_DB_NAME}.db.passwd
DM_SYSTEM_USER=dm
DM_SYSTEM_PASSWORD_FILE=$DM_INSTALL_DIR/etc/$DM_SYSTEM_USER.system.passwd
DM_DS_WEB_SERVICE_PORT=22236
DM_DS_WEB_SERVICE_HOST=localhost
DM_STORAGE_DIR=${DM_STORAGE_DIR:=$DM_INSTALL_DIR/storage}

echo "Preparing development configuration"
mkdir -p $DM_ETC_DIR
mkdir -p $DM_LOG_DIR

#echo "Modifying glassfish-web config file"
portalSrcDir=$DM_ROOT_DIR/src/java/DmWebPortal

echo "Configuring glassfish db access"
if [ ! -f $DM_DB_PASSWORD_FILE ]; then
    echo "File $DM_DB_PASSWORD_FILE does not exist."
    exit 1
fi

DM_DB_PASSWORD=`cat $DM_DB_PASSWORD_FILE`
configFile=$portalSrcDir/setup/glassfish-resources.xml
cmd="cat $configFile.template | sed 's?DM_DB_PASSWORD?$DM_DB_PASSWORD?g' > $configFile"
eval $cmd || exit 1

# Get system account password
if [ ! -f "$DM_SYSTEM_PASSWORD_FILE" ]; then
    sttyOrig=`stty -g`
    stty -echo
    read -p "Enter system password for the $DM_SYSTEM_USER user: " DM_SYSTEM_PASSWORD
    echo
    stty $sttyOrig
    echo $DM_SYSTEM_PASSWORD > $DM_SYSTEM_PASSWORD_FILE
    chmod 600 $DM_SYSTEM_PASSWORD_FILE

    # Add dm system user to the DB
    echo "Adding $DM_SYSTEM_USER system user to the database"
    dm-add-user --username=$DM_SYSTEM_USER --first-name=System --last-name=Account --password-file=$DM_SYSTEM_PASSWORD_FILE || exit 1
    dm-add-user-system-role --username=$DM_SYSTEM_USER --role-name=Administrator || exit 1
fi

echo "Generating web service config files"
cmd="cat $DM_ROOT_DIR/etc/ds-web-service.conf.template \
    | sed 's?sslCaCertFile=.*??g' \
    | sed 's?sslCertFile=.*??g' \
    | sed 's?sslKeyFile=.*??g' \
    | sed 's?DM_STORAGE_DIR?$DM_STORAGE_DIR?g' \
    | sed 's?DM_INSTALL_DIR?$DM_INSTALL_DIR?g' \
    | sed 's?DM_DB_NAME?$DM_DB_NAME?g' \
    | sed 's?handler=TimedRotatingFileLoggingHandler.*?handler=TimedRotatingFileLoggingHandler(\"$DM_DS_WEB_SERVICE_LOG_FILE\")?g' \
    > $DM_DS_WEB_SERVICE_CONFIG_FILE"
eval $cmd || exit 1

cmd="cat $DM_ROOT_DIR/etc/daq-web-service.conf.template \
    | sed 's?sslCaCertFile=.*??g' \
    | sed 's?sslCertFile=.*??g' \
    | sed 's?sslKeyFile=.*??g' \
    | sed 's?DM_SYSTEM_USER?$DM_SYSTEM_USER?g' \
    | sed 's?DM_SYSTEM_PASSWORD_FILE?$DM_SYSTEM_PASSWORD_FILE?g' \
    | sed 's?DM_DS_WEB_SERVICE_PORT?$DM_DS_WEB_SERVICE_PORT?g' \
    | sed 's?DM_DS_WEB_SERVICE_HOST?$DM_DS_WEB_SERVICE_HOST?g' \
    | sed 's?DM_WEB_SERVICE_PROTOCOL?http?g' \
    | sed 's?DM_INSTALL_DIR?$DM_INSTALL_DIR?g' \
    | sed 's?DM_DB_NAME?$DM_DB_NAME?g' \
    | sed 's?handler=TimedRotatingFileLoggingHandler.*?handler=TimedRotatingFileLoggingHandler(\"$DM_DAQ_WEB_SERVICE_LOG_FILE\")?g' \
    > $DM_DAQ_WEB_SERVICE_CONFIG_FILE"
eval $cmd || exit 1

cmd="cat $DM_ROOT_DIR/etc/cat-web-service.conf.template \
    | sed 's?sslCaCertFile=.*??g' \
    | sed 's?sslCertFile=.*??g' \
    | sed 's?sslKeyFile=.*??g' \
    | sed 's?handler=TimedRotatingFileLoggingHandler.*?handler=TimedRotatingFileLoggingHandler(\"$DM_CAT_WEB_SERVICE_LOG_FILE\")?g' \
    | sed 's?DM_INSTALL_DIR?$DM_INSTALL_DIR?g' \
    | sed 's?DM_SYSTEM_USER?$DM_SYSTEM_USER?g' \
    | sed 's?DM_DS_WEB_SERVICE_HOST?$DM_DS_WEB_SERVICE_HOST?g' \
    | sed 's?DM_DS_WEB_SERVICE_PORT?$DM_DS_WEB_SERVICE_PORT?g' \
    | sed 's?DM_WEB_SERVICE_PROTOCOL?$DM_WEB_SERVICE_PROTOCOL?g' \
    | sed 's?DM_DS_WEB_SERVICE_PROTOCOL?http?g' \
    | sed 's?DM_HOSTNAME?$DM_HOSTNAME?g' \
    | sed 's?DM_DB_NAME?$DM_DB_NAME?g' \
    > $DM_CAT_WEB_SERVICE_CONFIG_FILE"
eval $cmd || exit 1

echo "Done preparing development configuration"
