#!/bin/sh

# Copyright  2005 Sun Microsystems, Inc.  All rights reserved.
#
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
#
# U.S. Government Rights - Commercial software.  Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
#
# Use is subject to license terms.
#
# This distribution may include materials developed by third parties.Sun,
# Sun Microsystems and  the Sun logo are trademarks or registered trademarks
# of Sun Microsystems, Inc. in the U.S. and other countries.  
#
# Copyright  2005 Sun Microsystems, Inc. Tous droits rservs.
# Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
#
# L'utilisation est soumise aux termes du contrat de licence.
#
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
#
# Sun,  Sun Microsystems et  le logo Sun sont des marques de fabrique ou des
# marques dposes de Sun Microsystems, Inc. aux Etats-Unis et dans
# d'autres pays.


EDIT_FILE=index.html
TMP_DIR=/tmp
OS_ARCH=`/bin/uname -s`

# function to swap tag in index.html files
change_tag() {
    for file in *
    do
       if [ $file = "$EDIT_FILE" ]; then
           cp $file $file+
           sed -e "s#DEPLOY_URI#$1#g" \
              $file+ > $file
           rm -f $file+
       elif [ -d $file ]; then
           cd $file
           change_tag $1 
           cd ..
       fi
   done
}

# display usage
display_usage() {
   echo "Usage: amwar [--name|-n] <name of web application> [--deploymentURI|-u] <deployment URI> [--directory|-d] <directory>"
}


#############################################################################
# Start of main program
#############################################################################

# check for correct number of arguments.
if [ $# -ne 6 ]; then
    display_usage
    exit 1
fi

if [ $1 = "-n" -o $1 = "--name" ]; then
    CURRENT_WEB_APP=$2
    export CURRENT_WEB_APP
else
    display_usage
    exit 1
fi

if [ $3 = "-u" -o $3 = "--deploymentURI" ]; then
    DEPLOY_URI=$4
    export DEPLOY_URI
else
    display_usage
    exit 1
fi

if [ $5 = "-d" -o $5 = "--directory" ]; then
    DIR_NAME=$6
    export DIR_NAME
else
    display_usage
    exit 1
fi


if [ $OS_ARCH = "Linux" ] ; then
    BASE=`rpm -q --queryformat "%{INSTALLPREFIX}" sun-identity-utils`
    PRODUCT_DIR=identity
else
    BASE=`pkgparam SUNWamutl BASEDIR`
    PRODUCT_DIR=SUNWam
fi

WEB_SRC_DIR=$BASE/$PRODUCT_DIR/web-src
cd ${WEB_SRC_DIR}
CURRENT_WEB_APP_PATH=${WEB_SRC_DIR}/${CURRENT_WEB_APP}

if [ ! -d ${CURRENT_WEB_APP_PATH} ];then
    echo "$CURRENT_WEB_APP_PATH does not exist"
    exit 1
fi

echo "current web app is $CURRENT_WEB_APP"
CURRENT_TMP_DIR=${TMP_DIR}/${CURRENT_WEB_APP}

# remove the web application directory if it exist.
if [ -d ${CURRENT_TMP_DIR} ];then
    echo "$CURRENT_TMP_DIR exists, removing it ..."
    rm -rf ${CURRENT_TMP_DIR}
fi

cd $TMP_DIR

# create current web app tmp directory
mkdir $CURRENT_WEB_APP

# Remove leading slashes (/) from the DEPLOY_URI
WAR_FILE=`echo ${DEPLOY_URI} | sed -e "s#/*##"`".war"
WAR_FILE_DIR=${DIR_NAME}/${WAR_FILE}

# copy the current web app to the tmp directory
cp -r ${CURRENT_WEB_APP_PATH}/* ${CURRENT_TMP_DIR}/. 

. $BASE/$PRODUCT_DIR/bin/amutils 
# copy the files from package SUNWamconsdk if wep-app is applications
if [ ${CURRENT_WEB_APP} = "applications" ]; then
   SUNWAMCONSDK_DIR=${USR_DIR}/share/lib/identity/console-war
   echo "copying files from sunwamconsdk"
   cp -r ${SUNWAMCONSDK_DIR}/* ${CURRENT_TMP_DIR}/.
   cp /usr/share/lib/jato/jato.jar ${CURRENT_TMP_DIR}/WEB-INF/lib
   cp /usr/share/lib/jato/jato.tld ${CURRENT_TMP_DIR}/WEB-INF 
fi

cd $CURRENT_TMP_DIR
# tag swap
echo "Swapping tag swap in index.html files ..."
change_tag $DEPLOY_URI

# make the war file
echo "Making " $WAR_FILE
$JAVA_HOME/bin/jar cf $WAR_FILE_DIR *
cd $WEB_SRC_DIR

# remove the current web app tmp directory
rm -rf $CURRENT_TMP_DIR
