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

num_postreqs=0
print_header="Y"
REST_steps=$XXAS_PATCH_BIN/xxas_com_post_REST.sh
apps_rel_file=$XXAS_PATCH_SQL/xxas_com_appsrel.sql
apps_profile_file=$XXAS_PATCH_SQL/xxas_com_profile_st.sql
ptch_lst_file=$XXAS_PATCH_SQL/xxas_com_ltst_ptch.sql
sso_user_profile_chk=$XXAS_PATCH_SQL/xxas_com_sso_profile_chk.sql

print_intro () {
  if test "$print_header" = "Y" ;
  then
    printf "\nIMPORTANT: This patch will not work correctly until you \n"
    printf "           complete all of the following post-requisite steps:\n\n"
    print_header="N"
  fi
}

#
# Specify if the REST servlet needs deploying
#
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
if [ "$XXAS_REST_DEPLOYMENT" = "Y" -o $prev_ptch -le $curr_ptch ];	
then

#Deploying REST servlets
	$REST_steps
	if [ $? != 0 ]; then
		print_intro
		num_postreqs=`expr $num_postreqs + 1`
		printf "\t$num_postreqs. Deploy the REST servlet as described in\n"
		printf "\t   $XXAS_TOP/txt/REST_post_install.txt.\n\n"
	fi
fi

apps_release=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_rel_file`
major_apps_release=`echo "$apps_release" | awk -F. '{print $1$2}'`

#
# Check if patch delivers latest java file and prompt to run adcgnjar
#
if [ "$major_apps_release" = "122" -a "$XXAS_RUN_ADCGNJAR" = "Y" ];
        then
		print_intro
        num_postreqs=`expr $num_postreqs + 1`
        printf "\t$num_postreqs. Run the adcgnjar utility as described in Section 1.5.3 of MOS article 1577661.1\n"
fi

#
# Multi node specific instructions
#
# For nonshared nodes
if [ -f second_level_nodes.txt ]; then
	print_intro
	num_postreqs=`expr $num_postreqs + 1`
	 printf "\t$num_postreqs. Found non-shared node for this instance \n"
	 printf "\t   Please apply patch on non-shared node: \n"
	 printf "\t   `cat second_level_nodes.txt` \n\n"
	 rm -f second_level_nodes.txt
fi

# For shared nodes
if [ -f admin_shared_nodes.txt -a "$XXAS_REST_DEPLOYMENT" = "Y" ]; then
	print_intro
	num_postreqs=`expr $num_postreqs + 1`
	printf "\t$num_postreqs. Found shared node(s) for this instance\n"
	printf "\t   Please run autoconfig on shared node(s) : \n"
	printf "\t   `cat admin_shared_nodes.txt`\n"
	printf "\t   Also update XXAS_TOP custom environmnet for all \n"
	printf "\t   shred nodes as desribed in \n"
	printf "\t   $XXAS_TOP/txt/xxas_com_env_manual.txt \n\n"
	rm -f admin_shared_nodes.txt
fi

# 
# Specify if the Middle Tier requires restarting
#
if test "$XXAS_APP_TIER_RESTART" = "Y" ;
then
  print_intro
  num_postreqs=`expr $num_postreqs + 1`
  printf "\t$num_postreqs. Restart all Application Tier services.\n\n"
fi

#
#Specify if Result Cache is not enabled
#
result_cachefile=$XXAS_PATCH_SQL/xxas_com_rslt_cache_st.sql

RESULT_CACHE_STATUS=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$result_cachefile`

if test "$RESULT_CACHE_STATUS" = "DISABLED" ;
then
  print_intro
  num_postreqs=`expr $num_postreqs + 1`
  printf "\t$num_postreqs. Result Cache is NOT enabled for this database\n"
  printf "\t   and performance will be degraded.\n"
  printf "\t   Please see this document to learn more:\n"
  printf "\t   http://support.applaudsolutions.com/hc/en-us/articles/227627927-Oracle-11g-Result-Cache\n\n"
fi

#
# Check Upload file size limit for 12.2 at WLS level
#

apps_prof_value=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_profile_file UPLOAD_FILE_SIZE_LIMIT`

if [ "$major_apps_release" = "122" ];
        then
        ohs_loc=`grep s_ohs_instance_loc $CONTEXT_FILE | awk -F'>' '{print $2}' | awk -F'<' '{print $1}'`
        ohs_comp=`grep s_ohs_component $CONTEXT_FILE | awk -F'>' '{print $2}' | awk -F'<' '{print $1}'`
        grep SecRequestBodyNoFilesLimit $ohs_loc/config/OHS/$ohs_comp/security2.conf >/dev/null 2>&1
        chk_limit=$?
        if [ $chk_limit -ne 0 ];
        then
                print_intro
                num_postreqs=`expr $num_postreqs + 1`
				limit1=`expr $apps_prof_value / 2`
				limit2=`expr $apps_prof_value + $limit1`
				limit3=`expr $limit2 \* 1024`
                printf "\t$num_postreqs. WARNING: It’s recommended that the WLS SecRequestBodyNoFilesLimit is set to $limit3 \n"
                printf "\t   This is 50 percent larger than the 'Upload file size limit' profile option, which\n"
                printf "\t   allows for a larger base64 encoded file during transfer.\n"
                printf "\t   You can change this setting in:\n\n"
                printf "\t   $FND_TOP/admin/template/security2_conf_FMW.tmp\n"
                printf "\t   add below line after module <IfModule security2_module>\n"
                printf "\t      SecRequestBodyNoFilesLimit $limit3 \n"
                printf "\t   and run Autoconfig\n"
        else
                limit=`grep SecRequestBodyNoFilesLimit $ohs_loc/config/OHS/$ohs_comp/security2.conf | awk -F' ' '{print $2}'`
				if [ ${limit}x = "x" ]; then
					limit=0
				fi
				limit_kb=`expr $limit / 1024`
				if [ $limit_kb -le $apps_prof_value ];
				then
					print_intro
					num_postreqs=`expr $num_postreqs + 1`
					limit1=`expr $apps_prof_value / 2`
					limit2=`expr $apps_prof_value + $limit1`
					limit3=`expr $limit2 \* 1024`
					printf "\t$num_postreqs. WARNING: It’s recommended that the WLS SecRequestBodyNoFilesLimit is set to $limit3 \n"
					printf "\t   This is 50 percent larger than the 'Upload file size limit' profile option, which\n"
					printf "\t   allows for a larger base64 encoded file during transfer.\n"
					printf "\t   You can change this setting in:\n\n"
					printf "\t   $FND_TOP/admin/template/security2_conf_FMW.tmp\n"
					printf "\t   and run Autoconfig.\n\n"
				fi
        fi
fi

# Check for Unrestricted JSP Access for Applaud JSP's
comp_apps_release=`echo "$apps_release" | awk -F. '{print $1$2$3}'`
apps_prof_value1=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_profile_file FND_SEC_ALLOW_JSP_UNRESTRICTED_ACCESS`
if [ "$comp_apps_release" -ge "1227" ];
then
	apps_prof_value2=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_profile_file FND_SEC_ALLOWED_RESOURCES`
	if [ "$apps_prof_value2" = "CONFIG" ]; then
		if test ! -f $FND_TOP/secure/allowed_jsps_APPLAUD.conf; then
			cp -p $XXAS_PATCH_TOP/XXAS_TOP/txt/allowed_jsps_APPLAUD.conf $FND_TOP/secure/. >/dev/null 2>&1
		fi
		if test ! -f $FND_TOP/secure/allowed_servlets_applaud.conf; then
			cp -p $XXAS_PATCH_TOP/XXAS_TOP/txt/allowed_servlets_applaud.conf $FND_TOP/secure/. >/dev/null 2>&1
		fi
				print_intro
		num_postreqs=`expr $num_postreqs + 1`
		printf "\t$num_postreqs. WARNING: 'Security: Allowed Resources' is set to CONFIG\n"
		printf "\t   The app will not work until you follow the manual instructions at\n"
		printf "\t   http://docs.applaudsolutions.com/installation\$allow-applaud-jsps to allow Applaud.\n\n"
	fi
elif [ "$comp_apps_release" -eq "1226" -a "$apps_prof_value1" = "N" ]; then
		if test ! -f $FND_TOP/secure/allowed_jsps_APPLAUD.conf; then
			cp -p $XXAS_PATCH_TOP/XXAS_TOP/txt/allowed_jsps_APPLAUD.conf $FND_TOP/secure/. >/dev/null 2>&1
		fi
		print_intro
		num_postreqs=`expr $num_postreqs + 1`
		printf "\t$num_postreqs. WARNING: 'Allow Unrestricted JSP Access' is set to No\n"
		printf "\t   The app will not work until you follow the manual instructions at\n"
		printf "\t   http://docs.applaudsolutions.com/installation\$allow-applaud-jsps to allow Applaud JSPs.\n\n"
fi

#
# Validate SSO steps are performed on SSO enabled instance
#
profile_value=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_profile_file APPS_SSO` >/dev/null 2>&1

if [ "$profile_value" = "SSWA_SSO" -o "$profile_value" = "SSO_SDK" ]; then
	profile_value1=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_profile_file XXAS_SSO_LOGIN_URL` >/dev/null 2>&1
	user_profile_value=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$sso_user_profile_chk` >/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
	fi
	if [ -d $FND_TOP/admin/template/custom -a -f $FND_TOP/admin/template/custom/$oacore_templt_file ]; then
		grep "SessionToken" $FND_TOP/admin/template/custom/$oacore_templt_file > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			temp_status="N"
		else
			temp_status="Y"
		fi
	else
		temp_status="N"
	fi
	if [ "$profile_value1" = "0" -o "$temp_status" = "N" ]; then
		print_intro
		num_postreqs=`expr $num_postreqs + 1`
		printf "\t$num_postreqs. WARNING: SSO steps are NOT performed on this instance\n"
		printf "\t   Applaud desktop native login and mobile apps\n"
		printf "\t   will not work until you follow the manual instructions at\n"
		printf "\t   http://docs.applaudsolutions.com/single-sign-on \n\n"
	fi
	if [ "$user_profile_value" != 'BOTH' ]; then
		print_intro
		num_postreqs=`expr $num_postreqs + 1`
		printf "\t$num_postreqs. WARNING: SSO Profile - Applicaton SSO Login Type\n"
		printf "\t   is not set or set to LOCAL for user GUEST.\n"
		printf "\t   Applaud desktop native login and mobile apps\n"
		printf "\t   will not work until you UPDATE this profile value\n"
		printf "\t   for user GUEST to BOTH\n"	
	fi
fi

#
# Specify if Business Group specific tasks are required
#
if test "$XXAS_BG_STEPS" = "Y" ;
then
  print_intro
  num_postreqs=`expr $num_postreqs + 1`
  printf "\t$num_postreqs. Run the 'Install Business Group Data' concurrent\n"
  printf "\t   program from the 'Applaud Administrator' responsibility for\n"
  printf "\t   EACH Businesss Group that will use or may use the functionality\n"
  printf "\t   in this patch.\n\n"
fi

#
# Remove Beta Version
#
if test -d "$OA_HTML/b" ;
then
	# Remove index.html from beta release
	cd $OA_HTML/b
	rm -f index.html
fi

exit 0

