#!/bin/sh
# ---------------------------------------------------------------------------
# ObjectWeb Area Management Framework
# Copyright (C) 2005 INRIA - Jacquard Team
# Contact: architecture@objectweb.org
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Initial developer(s): Philippe Merle.
# Contributor(s): Nicolas Dolet.
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# Check argument
# ---------------------------------------------------------------------------

case "$1" in
  enable)
    ACTION="enable"
	;;
  disable)
    ACTION="disable"
    ;;
  *)
    echo "Usage: controlTMF [enable|disable]"
	exit 1
	;;
esac

# ---------------------------------------------------------------------------
# Check JONAS_ROOT variable
# ---------------------------------------------------------------------------

if [ -z "$JONAS_ROOT" ]
then
   echo "JONAS_ROOT is not set, please set it."
   exit 1
fi

# ---------------------------------------------------------------------------
# Jar name setup
# ---------------------------------------------------------------------------

THREAD_JAR=ow_thread.jar
AREA_JAR=ow_area.jar
THREAD_JONAS_JAR=ow_jonas_thread.jar
JAVAMAIL_JAR=mail.jar
MAILAPI_JAR=mailapi.jar
JAF_JAR=activation.jar
ASM_JAR=asm-2.1.jar

# ---------------------------------------------------------------------------
# JAVA setup
# We try to use first the java JVM in JAVA_HOME and if not found,
# we use the one found in the path.
# ---------------------------------------------------------------------------

if [ -z "$JAVA_HOME" ]
then
    JAVA=java
else
    JAVA=$JAVA_HOME/bin/java
fi


case "$ACTION" in

enable)

  # -------------------------------------------------------------------------
  #                               Enable TMF
  # -------------------------------------------------------------------------
  # Run AdaptThreadClass to build the adapted Thread class
  # -------------------------------------------------------------------------

  mkdir -p $JONAS_ROOT/lib/tmf/tmp/java/lang

  cd $JONAS_ROOT/lib/tmf/tmp

  jar xf $JONAS_ROOT/lib/tmf/$THREAD_JAR

  echo ""
  echo "Run AdaptThreadClass: "
  echo ""

  $JAVA -classpath $JONAS_ROOT/lib/tmf/$THREAD_JAR:$JONAS_ROOT/lib/tmf/$ASM_JAR \
      org.objectweb.area.thread.AdaptThreadClass \
      $JONAS_ROOT/lib/tmf/tmp/java/lang/

  # -------------------------------------------------------------------------
  # Create jars into JONAS_ROOT/lib/endorsed and JONAS_ROOT/lib/commons/jonas
  # -------------------------------------------------------------------------

  cd $JONAS_ROOT/lib/endorsed

  echo ""
  echo -n "Create $THREAD_JAR into $JONAS_ROOT/lib/endorsed:"

  if [ -f $JONAS_ROOT/lib/tmf/tmp/java/lang/ThreadGroup.class ]
  then
    jar cf $THREAD_JAR \
        -C $JONAS_ROOT/lib/tmf/tmp java/lang/ThreadGroup.class \
        -C $JONAS_ROOT/lib/tmf/tmp org/objectweb/area/thread/ThreadManager.class \
        -C $JONAS_ROOT/lib/tmf/tmp ow_thread.xml
  else
    jar cf $THREAD_JAR \
        -C $JONAS_ROOT/lib/tmf/tmp java/lang/Thread.class \
        -C $JONAS_ROOT/lib/tmf/tmp org/objectweb/area/thread/ThreadManager.class \
        -C $JONAS_ROOT/lib/tmf/tmp ow_thread.xml
  fi

  echo "OK"

  rm -r -f $JONAS_ROOT/lib/tmf/tmp

  echo -n "Copy $AREA_JAR into $JONAS_ROOT/lib/endorsed:"
  cp -f $JONAS_ROOT/lib/tmf/$AREA_JAR $JONAS_ROOT/lib/endorsed
  echo "OK"

  echo -n "Copy $THREAD_JONAS_JAR into $JONAS_ROOT/lib/commons/jonas:"
  cp -f $JONAS_ROOT/lib/tmf/$THREAD_JONAS_JAR $JONAS_ROOT/lib/commons/jonas
	echo "OK"

  echo -n "Copy $JAF_JAR, $JAVAMAIL_JAR and $MAILAPI_JAR into $JONAS_ROOT/lib/endorsed:"
  cp -f $JONAS_ROOT/lib/tmf/$JAF_JAR \
        $JONAS_ROOT/lib/tmf/$JAVAMAIL_JAR \
        $JONAS_ROOT/lib/tmf/$MAILAPI_JAR $JONAS_ROOT/lib/endorsed
  echo " OK"

  echo ""
  echo "ThreadManagementFramework enabled !"
  echo "Please check jonas.properties to enable the thread service."
  echo ""
  exit 0
  ;;

disable)

  # -------------------------------------------------------------------------
  #                               Disable TMF
  # -------------------------------------------------------------------------
  # Delete jars from JONAS_ROOT/lib/endorsed and JONAS_ROOT/lib/commons/jonas
  # -------------------------------------------------------------------------

  cd $JONAS_ROOT/lib/endorsed

  echo ""
  echo -n "Delete $THREAD_JAR, $AREA_JAR, $JAVAMAIL_JAR, $MAILAPI_JAR and $JAF_JAR from $JONAS_ROOT/lib/endorsed:"

  rm -f $JONAS_ROOT/lib/endorsed/$THREAD_JAR
  rm -f $JONAS_ROOT/lib/endorsed/$AREA_JAR
  rm -f $JONAS_ROOT/lib/endorsed/$JAVAMAIL_JAR
  rm -f $JONAS_ROOT/lib/endorsed/$MAILAPI_JAR
  rm -f $JONAS_ROOT/lib/endorsed/$JAF_JAR
  echo "OK"

  echo -n "Delete $THREAD_JONAS_JAR from $JONAS_ROOT/lib/endorsed :"
  rm -f $JONAS_ROOT/lib/commons/jonas/$THREAD_JONAS_JAR
  echo " OK"

  echo ""
  echo "ThreadManagementFramework disabled !"
  echo "Please check jonas.properties to disable the thread service"
  echo ""
  exit 0
  ;;

esac
