#!/bin/sh
# $Id$
# *===========================================================================*
# |                 Copyright (c) 2008 Applaud Solutions UK Ltd               |
# |                             All rights reserved                           |
# *===========================================================================*

header_string="$Id$"
prog_version=`echo "$header_string" | awk '{print $3}'`
program=`basename $0 .sh`

printf "\n\nStarting Applaud Solutions Customizations\n"


apps_rel_file=$XXAS_PATCH_SQL/xxas_com_appsrel.sql
ptch_lst_file=$XXAS_PATCH_SQL/xxas_com_ltst_ptch.sql
auto_config="N"

apps_release=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_rel_file`
major_apps_release=`echo "$apps_release" | awk -F. '{print $1$2}'`
chk_ptch=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptch_lst_file $XXAS_PATCH_NAME`
prev_ptch=`echo $chk_ptch|awk -F- '{print $2}'|awk -Fv '{print $2}'`
curr_ptch=2083

# Function
rest_cont ()
{
sed -e '/<!-- OAF REST Servlet -->/i \
<!-- Applaud Solutions RestManager Servlets --> \
  <servlet> \
    <servlet-name>ApplaudRestManager</servlet-name> \
    <servlet-class>xxas.oracle.apps.xxas.com.RestManager</servlet-class> \
  </servlet> \
\
  <servlet-mapping> \
    <servlet-name>ApplaudRestManager</servlet-name> \
    <url-pattern>/applaud/api/*</url-pattern> \
  </servlet-mapping> \
' $FND_TOP/admin/template/custom/${oacore_templt_file} > $FND_TOP/admin/template/custom/${oacore_templt_file}_mod
mv $FND_TOP/admin/template/custom/${oacore_templt_file}_mod $FND_TOP/admin/template/custom/${oacore_templt_file}
dos2unix $FND_TOP/admin/template/custom/${oacore_templt_file} >/dev/null 2>&1
}

rest_sinin ()
{
sed -e '/<!-- OAF REST Servlet -->/i \
<!-- Applaud Solutions RestSignIn Servlets --> \
  <servlet> \
    <servlet-name>ApplaudRestSignIn</servlet-name> \
    <servlet-class>xxas.oracle.apps.xxas.com.SignIn</servlet-class> \
  </servlet> \
\
  <servlet-mapping> \
     <servlet-name>ApplaudRestSignIn</servlet-name> \
    <url-pattern>/applaud/auth/signin</url-pattern> \
  </servlet-mapping> \
' $FND_TOP/admin/template/custom/${oacore_templt_file} > $FND_TOP/admin/template/custom/${oacore_templt_file}_mod
mv $FND_TOP/admin/template/custom/${oacore_templt_file}_mod $FND_TOP/admin/template/custom/${oacore_templt_file}
dos2unix $FND_TOP/admin/template/custom/${oacore_templt_file} >/dev/null 2>&1
}

rest_sinup ()
{
sed -e '/<!-- OAF REST Servlet -->/i \
<!-- Applaud Solutions RestSignUp Servlets --> \
  <servlet> \
    <servlet-name>ApplaudRestSignUp</servlet-name> \
    <servlet-class>xxas.oracle.apps.xxas.com.SignUp</servlet-class> \
  </servlet> \
\
  <servlet-mapping> \
    <servlet-name>ApplaudRestSignUp</servlet-name> \
    <url-pattern>/applaud/auth/signup</url-pattern> \
  </servlet-mapping> \
' $FND_TOP/admin/template/custom/${oacore_templt_file} > $FND_TOP/admin/template/custom/${oacore_templt_file}_mod
mv $FND_TOP/admin/template/custom/${oacore_templt_file}_mod $FND_TOP/admin/template/custom/${oacore_templt_file}
dos2unix $FND_TOP/admin/template/custom/${oacore_templt_file} >/dev/null 2>&1
}

if [ "$major_apps_release" = "122" ]; then
	oacore_templt_file=oacore_web_xml_FMW.tmp
elif [ "$major_apps_release" = "121" ]; then
	oacore_templt_file=orion_web_xml_1013.tmp
else
	XXAS_REST_DEPLOYMENT="Y"
fi

if [ ! -d $FND_TOP/admin/template/custom ]; then
		mkdir -p $FND_TOP/admin/template/custom
		cp $FND_TOP/admin/template/$oacore_templt_file $FND_TOP/admin/template/custom/.
		rest_sinup
		rest_sinin
		rest_cont
		auto_config="Y"
else
	if [ -f $FND_TOP/admin/template/custom/$oacore_templt_file ]; then
		if [ $prev_ptch -le $curr_ptch ]; then
			cp $FND_TOP/admin/template/custom/${oacore_templt_file} $FND_TOP/admin/template/custom/${oacore_templt_file}_Applaud
			grep "ApplaudRestManager" $FND_TOP/admin/template/custom/$oacore_templt_file > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				rest_cont
				auto_config="Y"
			fi
			grep "ApplaudRestSignIn" $FND_TOP/admin/template/custom/$oacore_templt_file > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				rest_sinin
				auto_config="Y"
			fi
			grep "ApplaudRestSignUp" $FND_TOP/admin/template/custom/$oacore_templt_file > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				rest_sinup
				auto_config="Y"
			fi
		fi
	else
		cp $FND_TOP/admin/template/$oacore_templt_file $FND_TOP/admin/template/custom/.
		rest_sinup
		rest_sinin
		rest_cont
		auto_config="Y"
	fi
fi

if [ $auto_config = "Y" ];
then
	printf "\nThere are changes to the template file $oacore_templt_file"
	printf "\nand requires autoconfig to be run\n"
	printf "\nDo you want to run Autoconfig (Y or N):"
	retries=0
	max_retries=10
	while [ $retries -lt $max_retries ]
	do
			read AUTO_CFG_STATUS;
					case $AUTO_CFG_STATUS in
							y|Y|YES|yes|Yes)
									printf "\nRunning Autoconfig..\n"
									autocfg_log=$XXAS_PATCH_TOP/autoconfig_`date +%F_%H%M%S`.log
									printf "Autoconfig log file: $autocfg_log\n"
									echo $XXAS_APPS_PW | $ADMIN_SCRIPTS_HOME/adautocfg.sh > $autocfg_log  2>&1
									if [ $? -ne 0 ]; then
											printf "\nThere seems to be issue with autoconfig"
											printf "\ncheck autoconfig log file"
											printf "$autocfg_log\n"
									else
											printf "\nAutoconfig Completed\n"
									break
									fi
							;;
							n|N|NO|no|No)
									printf "\nWARNING: Make sure to run AUTOCONFIG after the patch"
									printf "\nWARNING: it's mandatory step to run autoconfig manually\n\n"
									break
							;;
							*)
									printf "Invalid Option ('$AUTO_CFG_STATUS' given)" >&2
									retries=`expr $retries + 1`
									if [ $retries -ge $max_retries ]; then break 2; fi
							;;
					esac
	done
else
	exit 0
fi

exit 0
