#! /bin/bash
#######################################################################
# Copyright 2012 VMware, Inc.  All rights reserved.                   #
#######################################################################

echo "Setting up VMware VSA Cluster Service..."

# Get the fully qualified directory name where this script is located
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"


INSTALL_PATH=$(cd ${DIR}/.. && pwd)
JAVA_HOME=${INSTALL_PATH}/jre

$JAVA_HOME/bin/keytool -genkey -keyalg RSA -keysize 1024 -alias sva -keystore ${INSTALL_PATH}/conf/sva.jks -storepass svapass -keypass svapass -dname 'CN=sva, O=VMware'

if [ $? != 0 ]
 then
   echo "Failed to generate Java Key Store."
   exit $?
fi

chmod +x ${INSTALL_PATH}/bin/vmvcs ${INSTALL_PATH}/bin/wrapper-script ${INSTALL_PATH}/bin/wrapper

if [ $? != 0 ]
 then
   echo "Failed to add execution permissions."
   exit $?
fi

mkdir -p ${INSTALL_PATH}/etc/zookeeper
mkdir -p ${INSTALL_PATH}/var/zookeeper
mkdir -p ${INSTALL_PATH}/etc/sva

if [ $? != 0 ]
 then
   echo "Failed to create required directories."
   exit $?
fi

chmod ugo-x ${DIR}/setup

echo "The setup completed successfully."
echo -n
echo "Run \"${INSTALL_PATH}/bin/vmvcs start\" to start the cluster service."
echo "Run \"${INSTALL_PATH}/bin/vmvcs stop\" to stop the cluster service."

exit 0