#!/bin/sh

#
# Script used for deploying DM CAT web service
# Deployment configuration can be set in etc/$DM_DB_NAME.deploy.conf file
#
# Usage:
#
# $0 [DM_DB_NAME]
#

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

# Use first argument as db name, if provided
DM_DB_NAME=${DM_DB_NAME:=dm}
if [ ! -z "$1" ]; then
    DM_DB_NAME=$1
fi
echo "Using DB name: $DM_DB_NAME"

# Look for deployment file in etc directory, and use it to override
# default entries
deployConfigFile=$DM_ROOT_DIR/etc/${DM_DB_NAME}.deploy.conf
if [ -f $deployConfigFile ]; then
    echo "Using deployment config file: $deployConfigFile"
    . $deployConfigFile
else
    echo "Deployment config file $deployConfigFile not found, using defaults"
fi

DM_HOST_ARCH=`uname | tr [A-Z] [a-z]`-`uname -m`
DM_DATE=`date +%Y.%m.%d`
DM_HOSTNAME=`hostname -f`

DM_CONTEXT_ROOT=${DM_CONTEXT_ROOT:=dm}
DM_INSTALL_DIR=${DM_INSTALL_DIR:=$DM_ROOT_DIR/..}
DM_ETC_DIR=${DM_INSTALL_DIR}/etc
DM_SSL_DIR=${DM_ETC_DIR}/ssl
DM_LOG_DIR=${DM_INSTALL_DIR}/var/log
DM_CA_DIR=${DM_ETC_DIR}/CA
DM_CA_CERT_FILE=${DM_SSL_DIR}/dm-ca-cert.pem
DM_WEB_SERVICE_DAEMON=cat-web-service
DM_WEB_SERVICE_CERT_FILE=${DM_SSL_DIR}/$DM_DB_NAME.$DM_WEB_SERVICE_DAEMON.crt
DM_WEB_SERVICE_KEY_FILE=${DM_SSL_DIR}/$DM_DB_NAME.$DM_WEB_SERVICE_DAEMON.key
DM_WEB_SERVICE_CONFIG_FILE=${DM_ETC_DIR}/$DM_DB_NAME.$DM_WEB_SERVICE_DAEMON.conf
DM_WEB_SERVICE_LOG_FILE=${DM_LOG_DIR}/$DM_DB_NAME.$DM_WEB_SERVICE_DAEMON.log
DM_WEB_SERVICE_INIT_CMD=${DM_ROOT_DIR}/etc/init.d/dm-$DM_WEB_SERVICE_DAEMON
DM_DB_PASSWORD_FILE=${DM_ETC_DIR}/${DM_DB_NAME}.db.passwd 
DM_SYSTEM_PASSWORD_FILE=${DM_INSTALL_DIR}/etc/${DM_SYSTEM_USER}.system.passwd
DM_USER_SETUP_FILE=${DM_ETC_DIR}/${DM_DB_NAME}.setup.sh
DM_MONGODB_SERVICE_INIT_CMD=${DM_ROOT_DIR}/etc/init.d/dm-mongodb

cmd="echo $DM_CAT_WEB_SERVICE_HOST | sed 's?DM_HOSTNAME?$DM_HOSTNAME?'"
DM_WEB_SERVICE_HOST=`eval $cmd`
DM_WEB_SERVICE_PORT=$DM_CAT_WEB_SERVICE_PORT

if [ -z "$DM_DS_WEB_SERVICE_HOST" -o "$DM_DS_WEB_SERVICE_HOST" = "DM_HOSTNAME" ]; then
    read -p "Enter DM DS Web Service Host: " DM_DS_WEB_SERVICE_HOST
fi

echo "DM CAT web service host: $DM_CAT_WEB_SERVICE_HOST"
echo "DM DS web service host: $DM_DS_WEB_SERVICE_HOST"
echo "DM install directory: $DM_INSTALL_DIR"

mkdir -p $DM_ETC_DIR
mkdir -p $DM_SSL_DIR
mkdir -p $DM_LOG_DIR
chmod 700 $DM_SSL_DIR

echo "Stopping web service for $DM_DB_NAME"
$DM_WEB_SERVICE_INIT_CMD stop $DM_DB_NAME

echo "Checking CA certificate"
if [ ! -f $DM_CA_CERT_FILE ]; then
    echo "Copying DM CA cert"
    rsync -ar $DM_DS_WEB_SERVICE_HOST:$DM_CA_CERT_FILE $DM_SSL_DIR
else
    echo "DM CA certificate exists"
fi

echo "Checking service certificates"
if [ ! -f $DM_WEB_SERVICE_CERT_FILE -o ! -f $DM_WEB_SERVICE_KEY_FILE ]; then
    if [ ! -f $DM_CA_DIR/certs/$DM_WEB_SERVICE_HOST.crt ]; then
        echo "Attempting to create DM $DM_WEB_SERVICE_DAEMON certificate"
        ssh $DM_DS_WEB_SERVICE_HOST "$MY_DIR/dm_create_server_cert.sh $DM_WEB_SERVICE_HOST $DM_WEB_SERVICE_HOST dm@aps.anl.gov" 
    fi
    echo "Copying DM $DM_WEB_SERVICE_DAEMON certificate"
    rsync -ar $DM_DS_WEB_SERVICE_HOST:$DM_CA_DIR/certs/$DM_WEB_SERVICE_HOST.crt $DM_WEB_SERVICE_CERT_FILE || exit 1
    rsync -ar $DM_DS_WEB_SERVICE_HOST:$DM_CA_DIR/certs/$DM_WEB_SERVICE_HOST.key $DM_WEB_SERVICE_KEY_FILE || exit 1
else
    echo "DM service certificate exists"
fi

echo "Checking service configuration file"
if [ ! -f $DM_WEB_SERVICE_CONFIG_FILE ]; then
    echo "Generating service config file"
    cmd="cat $DM_ROOT_DIR/etc/$DM_WEB_SERVICE_DAEMON.conf.template \
        | sed 's?servicePort=.*?servicePort=$DM_WEB_SERVICE_PORT?g' \
        | sed 's?sslCaCertFile=.*?sslCaCertFile=$DM_CA_CERT_FILE?g' \
        | sed 's?sslCertFile=.*?sslCertFile=$DM_WEB_SERVICE_CERT_FILE?g' \
        | sed 's?sslKeyFile=.*?sslKeyFile=$DM_WEB_SERVICE_KEY_FILE?g' \
        | sed 's?handler=TimedRotatingFileLoggingHandler.*?handler=TimedRotatingFileLoggingHandler(\"$DM_WEB_SERVICE_LOG_FILE\")?g' \
        | sed 's?DM_STORAGE_DIR?$DM_STORAGE_DIR?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_DATA_DIR?$DM_DATA_DIR?g' \
        | sed 's?DM_HOSTNAME?$DM_HOSTNAME?g' \
        | sed 's?DM_DB_NAME?$DM_DB_NAME?g' \
        > $DM_WEB_SERVICE_CONFIG_FILE"
    eval $cmd || exit 1
else
    echo "Service config file exists"
fi

# Modify version
echo "Modifying python module version"
versionFile=$DM_ROOT_DIR/src/python/dm/__init__.py
cmd="cat $versionFile | sed 's?__version__ =.*?__version__ = \"${DM_SOFTWARE_VERSION}\"?g' | sed 's?DM_DATE?$DM_DATE?g' > $versionFile.2
&& mv $versionFile.2 $versionFile"
eval $cmd

# Check system account password
if [ ! -f $DM_SYSTEM_PASSWORD_FILE ]; then
    echo "Copying system account password file $DM_SYSTEM_PASSWORD_FILE"
    rsync -ar $DM_DS_WEB_SERVICE_HOST:$DM_SYSTEM_PASSWORD_FILE $DM_SYSTEM_PASSWORD_FILE || exit 1
fi

# Prepare setup file
if [ ! -f $DM_USER_SETUP_FILE ]; then
    echo "Copying user setup file $DM_USER_SETUP_FILE"
    rsync -ar $DM_DS_WEB_SERVICE_HOST:$DM_USER_SETUP_FILE $DM_USER_SETUP_FILE || exit 1
fi

# Prepare setup file
echo "Preparing setup file"
cmd="cat $DM_USER_SETUP_FILE \
        | sed 's?DM_ROOT_DIR=.*?DM_ROOT_DIR=$DM_ROOT_DIR?g' \
        | sed 's?DM_CAT_WEB_SERVICE_HOST=.*?DM_CAT_WEB_SERVICE_HOST=$DM_CAT_WEB_SERVICE_HOST?g' \
        | sed 's?DM_CAT_WEB_SERVICE_PORT=.*?DM_CAT_WEB_SERVICE_PORT=$DM_CAT_WEB_SERVICE_PORT?g' \
        | sed 's?DM_HOSTNAME?$DM_HOSTNAME?g' \
        > $DM_USER_SETUP_FILE.2 && mv $DM_USER_SETUP_FILE.2 $DM_USER_SETUP_FILE"
eval $cmd || exit 1

echo "Starting mongodb service"
$DM_MONGODB_SERVICE_INIT_CMD restart 

echo "Starting cat web service for $DM_DB_NAME"
$DM_WEB_SERVICE_INIT_CMD start $DM_DB_NAME

echo "Done deploying $DM_WEB_SERVICE_DAEMON for $DM_DB_NAME"

