#!/bin/bash
#  $Id: activate 26530 2012-08-16 12:05:36Z abaranetsky $
# -----------------------------------------------------------------------------
#  Name:    om:install.root
#  Purpose: om specific installation steps by root
#
#  Note:    started from base:install.root
#
#  env:     following variables are avaiable:
#                APL                         HTTPD_CONF          TARGET_OS
#                APL_CONF     APL_DB_USR     HTTPD_DIRS          DYLIB_SUFFIX
#                APL_USR      APL_HTTPD_GRP  HTTPD_MOD_DIR       AXIS2_HOME
#                APL_VAR      APL_HTTPD_USR  HTTPD_SHORT_MOD_DIR
#  www:     module defined apache loc and dir should be placed in HTTPD_DIRS
# -----------------------------------------------------------------------------
#  Author: A Baranetsky
#  Edited by:
#  QA by:
#  Copyright: videoNEXT LLC
# -----------------------------------------------------------------------------

# Proxy servicemix services using mod_proxy
#
cp $APL/smix/etc/va-smix-apache.inc $HTTPD_DIRS

# replace $APL in config files
#
perl -i -p -e "s{\\\$APL}{$APL}g" ../etc/master-features.xml
perl -i -p -e "s{\\\$APL}{$APL}g" ../etc/node-features.xml
perl -i -p -e "s{\\\$APL}{$APL}g" ../etc/logger.conf


if [ -d "$SMX_HOME" ]; then

    # patch servicemix-wrapper to set 'preferipv4stack'
    patch -N -d $SMX_HOME/etc/ -i $APL/smix/bin/servicemix-wrapper.patch
	
	# Add smix ssh key to known_hosts
	has_key=$(/usr/bin/ssh-keygen -F localhost | grep -P 'found.+DSA')
	if [ -n "$has_key" ]; then
    		echo "key found"
    	else
		echo "key not found. Add key to known_hosts";
		[ ! -d "/root/.ssh" ] && mkdir -m 0700 /root/.ssh
		ssh-keyscan -t dsa -p 8101 localhost >> /root/.ssh/known_hosts
		if [ "$?" != "0" ]; then
			echo "/usr/bin/ssh-keyscan failed. Exiting."
			exit 1
		fi;
	fi;

	# install stratus bundles
	#
	$APL/smix/bin/smxctl install
	[ "$?" != "0" ] && exit 1

	# add logging
	#
	SMX_LOG_CFG=$SMX_HOME/etc/org.ops4j.pax.logging.cfg
	echo "## BEGIN STRATUS" >> $SMX_LOG_CFG
	cat ../etc/logger.conf >> $SMX_LOG_CFG
	echo "## END STRATUS" >> $SMX_LOG_CFG

        $APL/smix/bin/smxctl cleanstart

else
	echo "Directory $SMX_HOME is missing"
	exit 1
fi;
