#! /bin/sh

#
# Copyright 1997-2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# A shell script for running assemblytool.
# By default, the tool will look for the file asenv.conf in the relative 
# directory ../config
# if ../config/asenv.conf doesn't exist, it will look for
# /etc/opt/SUNWappserver7/asenv.conf
# user can always overwrites it by specfying the -config option.


PRG=$0
PRG=`dirname $0`

usage="Usage: $0 [-userdir dir] [-config config_dir]"
config_dir=${PRG}/../config

PATH1=${PRG}/../config
PATH2=/etc/opt/SUNWappserver7
PATH3=/etc/appserver

config_dir=${PATH1}

use_custom_config=0

CONF=asenv.conf
userdir=

parse_args() {
while [ $# -gt 0 ] ; do
    case "$1" in
        -h|-help|--help)
        echo $usage
        exit
        ;;

        -userdir)
           if [ $# -gt 1 ]; then
                shift
                userdir="-userdir $1" ;
           else
                echo $usage
                exit;
           fi
	;;

	-config)
           if [ $# -gt 1 ]; then
                shift
		use_custom_config=1
                config_dir=$1 ;
           else
                echo $usage
                exit;
           fi

	;;

	*)
	   echo $usage
	   exit;
	
    esac
    shift
done
}

parse_args "$@"


if [ ! -r $config_dir/$CONF ] ; then

   if [ $use_custom_config -eq 1 ]; then
      echo "Cannot read $config_dir/$CONF"
      echo "Please restart with the [-config config_dir ] option"
      echo "config_dir is the directory where the file asenv.conf exists"
      exit;
   fi;

   config_dir=${PATH2}

   if [ ! -r $config_dir/$CONF ] ; then
	config_dir=${PATH3} ;

        if [ ! -r $config_dir/$CONF ] ; then
	    echo "Cannot read $CONF in the following directories:"
	    echo "${PATH1} \n${PATH2} \n${PATH3}"
	    echo "Please restart with the [-config config_dir ] option"
	    echo "config_dir is the directory where the file asenv.conf exists"
	    exit;
	fi;
    fi;

fi;

echo read in  $config_dir/$CONF
. $config_dir/$CONF

LIB=$AS_INSTALL/lib
IMQLIB=$AS_INSTALL/imq/lib
JHELP=$AS_JHELP
_JAVA=$AS_JAVA/bin/java

echo $_JAVA -Dcom.sun.aas.installRoot="$AS_INSTALL" -classpath $LIB/appserv-assemblytool.jar:$IMQLIB/activation.jar:$JHELP/jhall.jar:$LIB/appserv-admin.jar:$LIB/appserv-cmp.jar:$LIB/appserv-rt.jar:$LIB/javaee.jar:$LIB/appserv-ext.jar:$LIB/deployhelp.jar com.sun.enterprise.tools.deployment.main.Main $userdir


$_JAVA -Dcom.sun.aas.installRoot="$AS_INSTALL" -classpath $LIB/appserv-assemblytool.jar:$IMQLIB/activation.jar:$JHELP/jhall.jar:$LIB/appserv-admin.jar:$LIB/appserv-cmp.jar:$LIB/appserv-rt.jar:$LIB/javaee.jar:$LIB/appserv-ext.jar:$LIB/deployhelp.jar com.sun.enterprise.tools.deployment.main.Main $userdir

