#!/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.

containerConfig()
{
  if [ "$WEB_CONTAINER" = "WS6" ]; then
    WC_CONFIG=./amws61config
  elif [ "$WEB_CONTAINER" = "WL6" ]; then
    WC_CONFIG=./amwl61config
  elif [ "$WEB_CONTAINER" = "WL7" ]; then
    WC_CONFIG=./amwl70config
  elif [ "$WEB_CONTAINER" = "WL8" ]; then
    WC_CONFIG=./amwl81config
  elif [ "$WEB_CONTAINER" = "WAS4" ]; then
    WC_CONFIG=./amwas40config
  elif [ "$WEB_CONTAINER" = "WAS5" ]; then
    WC_CONFIG=./amwas51config
  elif [ "$WEB_CONTAINER" = "AS7" ]; then
    WC_CONFIG=./amas70config
  elif [ "$WEB_CONTAINER" = "AS8" ]; then
    WC_CONFIG=./amas81config
  fi
}

###########################################################
# Finds amutils and sources it. This function is identical
# in all scripts. Any changes made here need to made in
# other scripts too.
source_amutils()
{
	# Try to guess the location of amutils
	if [ "$BASEDIR" != "" -a "$PRODUCT_DIR" != "" -a -f $BASEDIR/$PRODUCT_DIR/bin/amutils ]; then
		AM_DIR=$BASEDIR/$PRODUCT_DIR/bin
		AMUTILS=$BASEDIR/$PRODUCT_DIR/bin/amutils
	elif [ "$BASEDIR" != "" -a "$PRODUCT_DIR" != "" -a -f $BASEDIR/$PRODUCT_DIR/share/bin/amutils ]; then
		AM_DIR=$BASEDIR/$PRODUCT_DIR/share/bin
		AMUTILS=$BASEDIR/$PRODUCT_DIR/share/bin/amutils
	elif [ -f ./amutils ]; then
		AM_DIR=.
		AMUTILS=./amutils
	elif [ -f `dirname $0`/amutils ]; then
		AM_DIR=`dirname $0`
		AMUTILS=`dirname $0`/amutils
	elif [ -f /opt/SUNWam/bin/amutils ]; then
		AM_DIR=/opt/SUNWam/bin
		AMUTILS=/opt/SUNWam/bin/amutils
	elif [ -f /opt/SUNWam/share/bin/amutils ]; then
		AM_DIR=/opt/SUNWam/bin/share/bin
		AMUTILS=/opt/SUNWam/share/bin/amutils ]
	elif [ -f /opt/sun/identity/bin/amutils ]; then
		AM_DIR=/opt/sun/identity/bin
		AMUTILS=/opt/sun/identity/bin/amutils
	elif [ -f /opt/sun/identity/share/bin/amutils ]; then
		AM_DIR=/opt/sun/identity/share/bin
		AMUTILS=/opt/sun/identity/share/bin/amutils ]
	else
		echo "$0: FATAL: Could not find amutils. Exiting."
		exit 2
	fi

	# Source it!
	. $AMUTILS
	export AM_DIR
	export AMUTILS
}

if [ ${#} -eq 0 ]; then
    echo "Usage: amconfig -s <silentinputfile>"
    exit 0
elif [ ${#} -eq 2 -a $1 = "-s" ]; then
    if [ ! -f $2 ]; then
        echo "Silent file not found: $2"
        exit 2
    fi
    IS_SILENT_INSTALL_FILE=$2
    export IS_SILENT_INSTALL_FILE
else
   echo "Usage: amconfig -s <silentinputfile>"
   exit 1
fi

source_amutils

link_shared_jars

check_root_user
check_os
create_debug_log_directory

#change all the files ownership
change_ownership

#get the container specific script
containerConfig

#verify that we know where the scripts are
if [ ! -f $AM_DIR/amdsconfig ]; then
  $ECHO "AM script directory was not found!"
  exit 2
fi

case $DEPLOY_LEVEL in
  1)
      $ECHO "Full install!"
      $AM_DIR/amdsconfig $1 $2
      RETVAL=$?
      if [ "$DIRECTORY_MODE" = "5" ]; then
	    ## update amconfig with new properties if not
 	    ## there
	    $BASEDIR/$PRODUCT_DIR/bin/amconfigupdate $2
      fi
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/amsvcconfig $1 $2
        RETVAL=$?
        if [ "$RETVAL" = "0" ]; then
          $AM_DIR/$WC_CONFIG $1 $2
          RETVAL=$?
        fi
      fi

      if [ "$RETVAL" = "0" ]; then
        link_amconfig
        unjar_docs
        tag_swap_samples
      fi
      ;;
  2)
      $ECHO "Console only install!"
      $AM_DIR/amsdkconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/$WC_CONFIG $1 $2
        RETVAL=$?
      fi
      if [ "$RETVAL" = "0" ]; then
        link_amconfig
        unjar_docs
        tag_swap_samples
      fi
      ;;
  3)
      $ECHO "SDK only install!"
      if [ "$DIRECTORY_MODE" = "5" ]; then
            ## update amconfig with new properties if not
            ## there
            $BASEDIR/$PRODUCT_DIR/bin/amconfigupdate $2
      fi
      $AM_DIR/amsdkconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        link_amconfig
        unjar_docs
        tag_swap_samples
      fi
      ;;
  4)
      $ECHO "SDK only with container configuration!"
      if [ "$DIRECTORY_MODE" = "5" ]; then
            ## update amconfig with new properties if not
            ## there
            $BASEDIR/$PRODUCT_DIR/bin/amconfigupdate $2
      fi
      $AM_DIR/amsdkconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/$WC_CONFIG $1 $2
        RETVAL=$?
      fi
      if [ "$RETVAL" = "0" ]; then
        link_amconfig
        unjar_docs
        tag_swap_samples
      fi
      ;;
  5)
      $ECHO "Federation common domain install!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        link_amconfig
      fi
      ;;
  6)
      $ECHO "Server only install!"
      $AM_DIR/amdsconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/amsvcconfig $1 $2
        RETVAL=$?
        if [ "$RETVAL" = "0" ]; then
          $AM_DIR/$WC_CONFIG $1 $2
          RETVAL=$?
        fi
      fi

      if [ "$RETVAL" = "0" ]; then
        link_amconfig
        unjar_docs
        tag_swap_samples
      fi
      ;;
  7)
      $ECHO "Container configuration!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        link_amconfig
        unjar_docs
        tag_swap_samples
      fi
      ;;
  11)
      $ECHO "Full uninstall!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  12)
      $ECHO "Uninstall console only!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  13)
      $ECHO "Uninstall SDK!"
      ;;
  14)
      $ECHO "Uninstall SDK and unconfigure container!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  15)
      $ECHO "Uninstall Federation!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  16)
      $ECHO "Uninstall server!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  17)
      $ECHO "Unconfigure container!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  21)
      $ECHO "Redeploy all!"
      $AM_DIR/amdsconfig $1 $2 tagswap
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/amsvcconfig $1 $2 tagswap
        RETVAL=$?
        if [ "$RETVAL" = "0" ]; then
          $AM_DIR/$WC_CONFIG $1 $2
          RETVAL=$?
        fi
      fi
      ;;
  26)
      $ECHO "Undeploy all!"
      $AM_DIR/amdsconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/amsvcconfig $1 $2
        RETVAL=$?
        if [ "$RETVAL" = "0" ]; then
          $AM_DIR/$WC_CONFIG $1 $2
        fi
      fi
      ;;
  31)
      $ECHO "Reinstall SDK!"
      $AM_DIR/amsdkconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/$WC_CONFIG $1 $2
        RETVAL=$?
      fi
      ;;
  32)
      $ECHO "Reinstall console!"
      $AM_DIR/amsdkconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/$WC_CONFIG $1 $2
        RETVAL=$?
      fi
      ;;
  33)
      $ECHO "Reinstall console SDK!"
      $AM_DIR/amsdkconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
        $AM_DIR/$WC_CONFIG $1 $2
        RETVAL=$?
      fi
      ;;
  35)
      $ECHO "Reinstall liberty!"
      $AM_DIR/$WC_CONFIG $1 $2
      RETVAL=$?
      ;;
  40)
      #This option is exclusively set for single war deployment of AM.
      #It is NOT invoked by any of the deploy-level option set in amsamplesilent file 
      #The deploy-level is set dynamically by Makefile.war
      $ECHO "Single War deployment!"
      $AM_DIR/amdsconfig $1 $2
      RETVAL=$?
      if [ "$RETVAL" = "0" ]; then
          $AM_DIR/amsvcconfig $1 $2
          RETVAL=$?
      fi
      ;;
  *) $ECHO "No such configuration option!"
      ;;
esac

if [ "$RETVAL" != "0" ]; then
  exit $RETVAL
else
  exit 0
fi


