#!/bin/bash

## This script was made for use with RHEL6 however it can probably be modded for other flavors.

## Install before running
# yum install java-1.7.0-oracle tomcat6 tomcat-native unzip

## Make a symlink for easy upgrades.
# ln -s /usr/local/cascade-7.6 /usr/local/cascade

## Run as the user you wish to run the CMS under and create and make the directory writeable

## Config Documentation
# If enabled, script will take files in ./cascade-install-conf/HOSTNAME/ and drop them in your install directory.
# Hostname should be detected from the script so you can have multiple deployments in the install directory.
# Default Off. To enable toggle 0 to 1.
##

HOSTNAME=`hostname`
ENABLE_CONFIG="0"
INSTALL_VERSION="cascade-7.6"
INSTALL_HOME="/usr/local/$INSTALL_VERSION"
INSTALL_BIN="$INSTALL_HOME/tomcat/bin"
INSTALL_LIB="$INSTALL_HOME/tomcat/lib"
RHEL_TOMCAT_LIB="/usr/share/tomcat6/lib"
RHEL_JAVA_LIB="/usr/share/java"

typeset -a BIN_RHEL
typeset -a BIN_CASCADE
BIN_CASCADE[0]="$INSTALL_BIN/bootstrap.jar"
BIN_CASCADE[1]="$INSTALL_BIN/tomcat-juli.jar"
BIN_RHEL[0]="/usr/share/tomcat6/bin/bootstrap.jar"
BIN_RHEL[1]="/usr/share/tomcat6/bin/tomcat-juli.jar"

typeset -a LIB_CASCADE
typeset -a LIB_RHEL
LIB_CASCADE[0]="$INSTALL_LIB/annotations-api.jar"
LIB_RHEL[0]="/usr/share/tomcat6/lib/annotations-api.jar"

LIB_CASCADE[1]="$INSTALL_LIB/catalina-ant.jar"
LIB_RHEL[1]="/usr/share/tomcat6/lib/catalina-ant.jar"

LIB_CASCADE[2]="$INSTALL_LIB/catalina-ha.jar"
LIB_RHEL[2]="/usr/share/tomcat6/lib/catalina-ha.jar"

LIB_CASCADE[3]="$INSTALL_LIB/catalina.jar"
LIB_RHEL[3]="/usr/share/tomcat6/lib/catalina.jar"

LIB_CASCADE[4]="$INSTALL_LIB/catalina-tribes.jar"
LIB_RHEL[4]="/usr/share/tomcat6/lib/catalina-tribes.jar"

LIB_CASCADE[5]="$INSTALL_LIB/ecj-3.7.jar"
LIB_RHEL[5]="/usr/share/java/ecj.jar"

LIB_CASCADE[6]="$INSTALL_LIB/el-api.jar"
LIB_RHEL[6]="/usr/share/java/tomcat6-el-2.1-api.jar"

LIB_CASCADE[7]="$INSTALL_LIB/jasper-el.jar"
LIB_RHEL[7]="/usr/share/java/tomcat6/jasper-el.jar"

LIB_CASCADE[8]="$INSTALL_LIB/jasper.jar"
LIB_RHEL[8]="/usr/share/java/tomcat6/jasper.jar"

LIB_CASCADE[9]="$INSTALL_LIB/jsp-api.jar"
LIB_RHEL[9]="/usr/share/java/jsp.jar"

LIB_CASCADE[10]="$INSTALL_LIB/servlet-api.jar"
LIB_RHEL[10]="/usr/share/java/tomcat6-servlet-2.5-api.jar"

LIB_CASCADE[11]="$INSTALL_LIB/tomcat-coyote.jar"
LIB_RHEL[11]="/usr/share/tomcat6/lib/tomcat-coyote.jar"

LIB_CASCADE[12]="$INSTALL_LIB/tomcat-i18n-es.jar"
LIB_RHEL[12]="/usr/share/tomcat6/lib/tomcat-i18n-es.jar"

LIB_CASCADE[13]="$INSTALL_LIB/tomcat-i18n-fr.jar"
LIB_RHEL[13]="/usr/share/tomcat6/lib/tomcat-i18n-fr.jar"

LIB_CASCADE[14]="$INSTALL_LIB/tomcat-i18n-ja.jar"
LIB_RHEL[14]="/usr/share/tomcat6/lib/tomcat-i18n-ja.jar"

# Make sure you don't overwrite your data.
if [ "$(ls -A $INSTALL_HOME)" ]; then
    echo "Directory exists. Exiting without changes..."
    exit
fi

# Here we go!
unzip $INSTALL_VERSION.zip -d $INSTALL_HOME >/dev/null 2>&1
if [ $? != 0 ]; then
    echo "Needed: $INSTALL_VERSION.zip - Update script variable or add needed file."
    exit
fi

# Binary symlinking
for (( i=0 ; i < ${#BIN_CASCADE[@]}; i++ ))
do

if [ -f "${BIN_CASCADE[$i]}" ]; then
    rm -f "${BIN_CASCADE[$i]}"
fi

ln -s ${BIN_RHEL[$i]} "${BIN_CASCADE[$i]}" 2>/dev/null

done

# Library symlinking
for (( i=0 ; i < ${#LIB_CASCADE[@]}; i++ ))
do

    if [ -f "${LIB_CASCADE[$i]}" ]; then
        rm -f "${LIB_CASCADE[$i]}"
    fi

    ln -s ${LIB_RHEL[$i]} "${LIB_CASCADE[$i]}" 2>/dev/null

done

# Config install if enabled
if [ $ENABLE_CONFIG == 1 ] || [ $ENABLE_CONFIG == 'true' ] || [ $ENABLE_CONFIG == 'TRUE' ]; then
    cp -r ./cascade-install-conf/$HOSTNAME/* $INSTALL_HOME/
fi

exit
