#!/bin/bash
# generate a default ens folder into the artifacts folder
set -o errexit -o nounset -o pipefail

# Locations
INSTDIR="$1"
OBJECT_SCRIPT="$2"
BIN_DIR="${INSTDIR}/bin"
SCRIPT_DIR="${BIN_DIR}/scripts"
DATA_DIR="${INSTDIR}/var"
TTA_MSG="${SCRIPT_DIR}/en-us"
CURRENT_DIR="$(pwd)"
hostname="$(hostname)"

###############################################################################

# Function to run a given tarantella command with provided arguments
# Args:
# 1 - the tarantella command to run
# $@ - the arguments to the tarantella command
#
#
function run_tcl_command {
    # take the first argument and use it as the command to run 
    cmd="$1"

    # shift past the first argument and stores the remaining
    # arguments in an array
    shift
    args=("$@")

    # the script must be run in the installed scrtip dir
    pushd "${BIN_DIR}/scripts"

        TTA_INSTALL_MODE=t \
        ${JAVA_BIN}/java ${JAVA_OPTS} -Djava.library.path="${INSTDIR}/lib" \
            -classpath "${DEFAULT_JAVA_CLASSPATH}" \
            ${DEFAULT_JAVA_PROPERTIES} \
            ${JAVA_SYSTEM_PROPERTIES} \
            ${TTA_APP_CLASS} \
                tcl.lang.Shell \
                "${cmd}" \
                "${args[@]}" \
                --ttamsg "${BIN_DIR}/scripts/en-us" \
                --ttabindir "${BIN_DIR}" \
                --datadir "${DATA_DIR}" \
                --currentdir "${CURRENT_DIR}" \
                --ttainstdir "${INSTDIR}" \
                --cmdlineargs "SESSIONTOKENFILE=" \
                CONTEXTSTART=.../_ens \
                INSTALLDIR="${INSTDIR}" \
                PEERHOST="${hostname}" \
                EXTERNALHOST="${hostname}" \
                EXTERNALPORT=3144 \
                SECUREPORT=5307 \
                PEERPORT=5427
    popd
}

###############################################################################

# setup the java environment & other config to run the tarantella commands
source "${INSTDIR}/bin/scripts/install_scripts/set_java_defaults"
: ${JAVA_OPTS=""}
: ${JAVA_SYSTEM_PROPERTIES:="
-Djava.naming.tfn.flavour=autoAdmin
"}

TTA_APP_CLASS="com.sco.tta.common.util.TarantellaApplication"
ADMIN_SHELL_APP="com.sco.tta.admin.shell.AdminShellApplication"



###############################################################################

# create the default ens by executing the provided batch script

rm -rf "${INSTDIR}/var/ens"
mkdir -p "${INSTDIR}/var/ens"

printf "Running ens creation script..."

# the script must be run in the installed script dir
pushd "$SCRIPT_DIR"
cat "$OBJECT_SCRIPT" | ${JAVA_BIN}/java ${JAVA_OPTS} -Djava.library.path="${INSTDIR}/lib" \
     -classpath "${DEFAULT_JAVA_CLASSPATH}" \
     ${DEFAULT_JAVA_PROPERTIES} \
     ${JAVA_SYSTEM_PROPERTIES} \
     ${TTA_APP_CLASS} ${ADMIN_SHELL_APP} \
     --ttamsg "${TTA_MSG}" \
     SCRIPTDIR="${SCRIPT_DIR}"\
     --currentdir "${CURRENT_DIR}"\
     --ttabindir "${BIN_DIR}"\
     --cmdlineargs \
     CONTEXTSTART=.../_ens/ \
     INSTALLDIR="${INSTDIR}"\
     PEERHOST="localhost"\
     EXTERNALHOST="localhost"\
     PEERPORT=5427\
     SECUREPORT=5307\
     EXTERNALPORT=3144\
     TFNUSER=.../_ens/o=org/cn=prefs-user-no-delete
popd

printf "Done.\n"

###############################################################################

# create the default profile preferences
DEFAULT_PROFILE_CLASS="com.sco.tta.setup.CreateDefaultProfile"
DEFAULTPROFILE=".../_ens/o=Tarantella System Objects/_service/sco/tta/resources/Default"

rm -rf "${INSTDIR}/var/userresources"
mkdir -p "${INSTDIR}/var/userresources"

printf "Creating the default profile ...."

${JAVA_BIN}/java ${JAVA_OPTS} -Djava.library.path="${INSTDIR}/lib" \
     -classpath "${DEFAULT_JAVA_CLASSPATH}" \
     ${DEFAULT_JAVA_PROPERTIES} \
     ${JAVA_SYSTEM_PROPERTIES} \
     ${DEFAULT_PROFILE_CLASS} \
     "${DEFAULTPROFILE}" \
     CONTEXTSTART=.../_ens/ \
     INSTALLDIR="${INSTDIR}"\
     PEERHOST="localhost"\
     EXTERNALHOST="localhost"\
     PEERPORT=5427\
     SECUREPORT=5307\
     EXTERNALPORT=3144

EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]
then
    FILE_NAME=$(basename "$0")
    printf "FAIL in %s:%s , Default profile not created" "$FILE_NAME" "$LINENO"
    exit 1
fi

printf "Done.\n"

###############################################################################

APP_ROOT_ENS="applications"
SYSTEM_ROOT_ENS="Tarantella System Objects"

role_new="${SCRIPT_DIR}/role_new.tsh"
role_add_member="${SCRIPT_DIR}/role_add_member.tsh"
role_add_link="${SCRIPT_DIR}/role_add_link.tsh"
object_edit="${SCRIPT_DIR}/object_edit.tsh"

###############################################################################

# Section for adding initial ens defaults

# run the necessary commands to add the roles to ens

# NOTE: We need to store the arguments for each command in an array so their
# quotes can be processed properly when expanded on the command line

# run all the "role new" commands to add all the default sgd roles
global_viewers_name="o=${SYSTEM_ROOT_ENS}/cn=Global Viewers"
printf "Adding new role %s ..." "$global_viewers_name"
role_new_global_viewers=(new --name "$global_viewers_name" \
                         --resource all:read)
run_tcl_command "$role_new" "${role_new_global_viewers[@]}"
printf "Done.\n"


session_admins_name="o=${SYSTEM_ROOT_ENS}/cn=Session Administrators"
printf "Adding new role %s ..." "$session_admins_name"
role_new_session_admins=(new --name "$session_admins_name" \
                         --resource session:read-write)
run_tcl_command "$role_new" "${role_new_session_admins[@]}"
printf "Done.\n"


session_viewers_name="o=${SYSTEM_ROOT_ENS}/cn=Session Viewers"
printf "Adding new role %s ..." "$session_viewers_name"
role_new_session_viewers=(new --name "$session_viewers_name" \
                         --resource session:read)
run_tcl_command "$role_new" "${role_new_session_viewers[@]}"
printf "Done.\n"


ent_manage_agent_name="o=${SYSTEM_ROOT_ENS}/cn=Enterprise Manager Agents"
printf "Adding new role %s ..." "$ent_manage_agent_name"
role_new_ent_manage_agents=(new --name "$ent_manage_agent_name" \
                         --resource all:read)
run_tcl_command "$role_new" "${role_new_ent_manage_agents[@]}"
printf "Done.\n"


global_admins_name="o=${SYSTEM_ROOT_ENS}/cn=Global Administrators"
printf "Adding new role %s ..." "$global_admins_name" 
role_new_global_admins=(new --name "$global_admins_name" \
                         --resource all:read-write)
run_tcl_command "$role_new" "${role_new_global_admins[@]}"
printf "Done.\n"


# run "role add_member" to add administrator to global admins role
printf "Adding new member to role: %s ..." "$global_admins_name" 
role_add_member_global_admins=(add_member --role "$global_admins_name" \
                               --member "o=${SYSTEM_ROOT_ENS}/cn=Administrator")
run_tcl_command "$role_add_member" "${role_add_member_global_admins[@]}"
printf "Done.\n"


gateway_deployment_name="o=${SYSTEM_ROOT_ENS}/cn=Gateway Deployment"
printf "Adding new role %s ..." "$gateway_deployment_name" 
role_new_gateway_deployment=(new --name "$gateway_deployment_name" \
                             --resource ens:read)
run_tcl_command "$role_new" "${role_new_gateway_deployment[@]}"
printf "Done.\n"


# run "role add_member" to add gateway deployer to gateway deployment role
printf "Adding new member to role: %s ..." "$gateway_deployment_name" 
role_add_member_gateway_deployment=(add_member --role "$gateway_deployment_name" \
                                    --member "o=${SYSTEM_ROOT_ENS}/cn=Gateway Deployer")
run_tcl_command "$role_add_member" "${role_add_member_gateway_deployment[@]}"
printf "Done.\n"


# create adminuser file
mkdir -p "${INSTDIR}/var/info/"
echo "root" > "${INSTDIR}/var/info/adminuser"

###############################################################################

# Section for adding initial pip (platform identity pack) ens defaults

# run the "object edit" commands to edit the --hints of the doc objects
doc_admin_console_name="o=${SYSTEM_ROOT_ENS}/cn=Administration Console"
object_edit_admin_console=(edit --name "$doc_admin_console_name"
                           --hints "showonclassic=no")
printf "Editing the object: %s..." "$doc_admin_console_name"
run_tcl_command "$object_edit" "${object_edit_admin_console[@]}"
printf "Done.\n"

doc_profile_editor_name="o=${SYSTEM_ROOT_ENS}/cn=Profile Editor"
object_edit_profile_editor=(edit --name "$doc_profile_editor_name"
                           --hints "showonclassic=no")
printf "Editing the object: %s..." "$doc_profile_editor_name"
run_tcl_command "$object_edit" "${object_edit_profile_editor[@]}"
printf "Done.\n"

# run "role add_link" command to add links for the global admin role
doc_oracle_webdoc_name="o=${APP_ROOT_ENS}/cn=Oracle web site"
printf "Adding new links to role: %s..." "$global_admins_name"
role_add_link_global_admins=(add_link --role "$global_admins_name"
                             --link "$doc_oracle_webdoc_name" 
                             "$doc_admin_console_name"
                             "$doc_profile_editor_name")
run_tcl_command "$role_add_link" "${role_add_link_global_admins[@]}"
printf "Done.\n"
