#!/bin/bash
# -------
# Script for apply AMPs to installed WAR
# -------
CATALINA_BASE=<%= node['alfresco']['home'] %>

TOMCAT_BIN=<%= node['alfresco']['bin'] %>

ALF_TOMCAT_WEBAPPS=<%= node['tomcat']['webapp_dir'] %>
<% if node['tomcat']['instances']['alfresco'] %>
  ALF_TOMCAT_WEBAPPS="<%= node['alfresco']['home'] %>-alfresco/webapps"
<% end -%>

SHARE_TOMCAT_WEBAPPS=<%= node['tomcat']['webapp_dir'] %>
<% if node['tomcat']['instances']['share'] %>
  SHARE_TOMCAT_WEBAPPS="<%= node['alfresco']['home'] %>-share/webapps"
<% end -%>

AMPS_FOLDER=<%= node['alfresco']['amps_folder'] %>
AMPSSHARE_FOLDER=<%= node['alfresco']['amps_share_folder'] %>

if [ -f $TOMCAT_BIN/setenv.sh ]; then
  . $TOMCAT_BIN/setenv.sh
fi

#echo "Shutting down tomcat . . ."
#service tomcat7 stop
#sleep 2
#if [ "x$CATALINA_PID" != "x" ]; then
#   kill -0 $CATALINA_PID 2>/dev/null
#fi

echo "This script will apply all the AMPs in amps and amps_share to the alfresco.war and share.war files in $TOMCAT_WEBAPPS"

java -jar $TOMCAT_BIN/alfresco-mmt.jar install $AMPS_FOLDER $ALF_TOMCAT_WEBAPPS/alfresco.war -nobackup -directory -force
java -jar $TOMCAT_BIN/alfresco-mmt.jar install $AMPSSHARE_FOLDER $SHARE_TOMCAT_WEBAPPS/share.war -nobackup -directory -force

echo "About to clean out $TOMCAT_WEBAPPS and share directories and temporary files..."

rm -rf $ALF_TOMCAT_WEBAPPS/alfresco
rm -rf $SHARE_TOMCAT_WEBAPPS/share

echo "Cleaning Tomcat . . ."
# . $ALF_HOME/bin/clean_tomcat.sh
rm -rf $CATALINA_BASE-*/logs/*
rm -rf $CATALINA_BASE-*/temp/*
rm -rf $CATALINA_BASE-*/work/*

#echo "Starting Tomcat again . . ."
#service tomcat7 start
