#! /bin/sh
#
# Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# @version "@(#)createfabric.sh 1.17     02/01/31 SMI"
#
# This script sets up a new fabric.  This entails setting up directories
# to hold the fabric configuration files and message logs.  It also
# requires this script to set up as policy file so the instance of the
# FM that manages this fabric can access the fabric files.
#

# Uncomment when an FM package starts to stabilize
#USER=`id | /usr/bin/awk -F'[()]' '{print $2}'`
#if [ "$USER" != "root" ]; then
#    echo "Error: user \"$USER\" must be root to run ${0}"
#    exit 1
#fi

USAGE="Usage: ${0} { fabric_name }\n"

# must specify a fabric name
if [ $# -lt 1 ]; then
    echo $USAGE
    exit 1
fi

if [ $1 = "-h" -o $1 = "--help" ]; then
    echo $USAGE
    exit 1
fi

# save off fabric name
FABRIC_NAME=$1

# Name of the file containing the FM data directory.  This is the directory
# where all FM fabric's will store their log files and config files.  Normally
# this directory will be set by the user at package install.  The directory
# will be written to a predefined  location in the FM installation tree so
# scripts that access fabric data will know where it is.
DATA_DIR_FILENAME="wcfm_base_data_dir.cfg"

# The name of the directory appended to the FM base directory and FM package
# name.  It contains the file that holds the FM data directory.
LOG_AND_CONFIG_DIR="config"

# The name of the directory appended to the fabric configuration directory
# used to hold a fabric message log.
LOG_DIR="log" 

# The name of the directory appended to the fabric configuration directory
# used to hold a fabric's configuration files.
CONFIG_DIR="cfg"

# This script file can only be run after doing a package install.
FM_PKG=SUNWwcfmu
FM_PKG_DIR=SUNWwcfm
# Assume the FM package is installed, get the real base directory and construct the
# FM config directory where the FM data directory file lives.
FM_BASE_DIR=`/usr/bin/pkgparam ${FM_PKG} BASEDIR`
FM_CONFIG_DIR=${FM_BASE_DIR}/${FM_PKG_DIR}/${LOG_AND_CONFIG_DIR}
FABRIC_DATA_DIR_FILE="${FM_CONFIG_DIR}/${DATA_DIR_FILENAME}"
XML_DIR=${FM_BASE_DIR}/${FM_PKG_DIR}/xml_config_files
DTD_DIR=${FM_BASE_DIR}/${FM_PKG_DIR}/dtds
FM_JARS=${FM_BASE_DIR}/${FM_PKG_DIR}/classes

# Read the FM data directory from the wfm_base_data_dir.cfg file.
FM_DATA_DIR=`/usr/bin/cat ${FABRIC_DATA_DIR_FILE}`
FABRIC_LOG_DIR="${FM_DATA_DIR}/${FABRIC_NAME}/${LOG_DIR}"
FABRIC_CFG_DIR="${FM_DATA_DIR}/${FABRIC_NAME}/${CONFIG_DIR}"

# Build the fabric log directory and the fabric configuration directory.
# The log directory contains the message logs for this fabric and the
# configuration directory contains the current fabric state information.
if [ -d "${FABRIC_LOG_DIR}" ]; then
    echo "Error, the fabric log directory exists \"${FABRIC_LOG_DIR}\""
    echo "Run \"deletefabric ${FABRIC_NAME}\" to remove the existing fabric"
    exit 1
else
    /usr/bin/mkdir -p ${FABRIC_LOG_DIR}
    echo "Created fabric log directory \"${FABRIC_LOG_DIR}\""
fi

if [ -d "${FABRIC_CFG_DIR}" ]; then
    echo "Error, the fabric cfg directory exists \"${FABRIC_CFG_DIR}\""
    echo "Run \"deletefabric ${FABRIC_NAME}\" to remove the existing fabric"
    exit 1
else
    /usr/bin/mkdir -p ${FABRIC_CFG_DIR}
    echo "Created fabric data directory \"${FABRIC_CFG_DIR}\""
fi

# Create the java policy file for this fabric. The policy should give
# give the instance of the FM managing this fabric the ability to read
# and write all files under the ${FM_DATA_DIR}.  This include the fabric
# log and configuration files.  We should also allow socket connections
# for the RMI infastructure.  The file will be placed in the fabric config
# directory.
POLICY_FILE_EXT=".policy"
POLICY_FILE=${FABRIC_CFG_DIR}/${FABRIC_NAME}${POLICY_FILE_EXT}
echo "grant {" >> $POLICY_FILE
echo "  permission java.lang.RuntimePermission \"modifyThread\";" >> $POLICY_FILE
echo "  permission java.lang.RuntimePermission \"modifyThreadGroup\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"${FM_DATA_DIR}/${FABRIC_NAME}/-\", \"read, write, delete, execute\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"${FM_DATA_DIR}/security.info\", \"read, execute\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"${FM_DATA_DIR}/fmKeyStore/-\", \"read, execute\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"/tmp/-\", \"read, write, delete, execute\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"/etc/-\", \"read, write, delete, execute\";" >> $POLICY_FILE
echo "  permission java.net.SocketPermission \"*\", \"connect, resolve, listen, accept\";" >> $POLICY_FILE
echo "  permission java.lang.RuntimePermission \"accessClassInPackage.sun.wildcat.rmiInterfaces\";" >> $POLICY_FILE
echo "  permission java.lang.RuntimePermission \"accessClassInPackage.sun.wildcat.wciSwitch\";" >> $POLICY_FILE
echo "  permission java.lang.RuntimePermission \"accessClassInPackage.sun.serengeti.fabricmgr\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"${XML_DIR}/-\", \"read\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"${DTD_DIR}/-\", \"read\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"${FM_BASE_DIR}/${FM_PKG_DIR}/-\", \"read\";" >> $POLICY_FILE
echo "  permission java.lang.RuntimePermission \"createClassLoader\";" >> $POLICY_FILE
echo "  permission java.io.FilePermission \"<<ALL FILES>>\", \"execute\";" >> $POLICY_FILE
#echo "  permission java.security.AllPermission;" >> $POLICY_FILE
echo "  permission java.util.PropertyPermission \"FM.keyStoreLocation\", \"read\";" >> $POLICY_FILE
echo "  permission java.util.PropertyPermission \"FM.keyStorePWD\", \"read\";" >> $POLICY_FILE
echo "};" >> $POLICY_FILE
echo "Created policy file \"${POLICY_FILE}\""
exit 0
