#!/bin/sh
#  $Id$
#  Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.

N=""
C=""
if echo -n | grep n > /dev/null
then
  C="\c"
else
  N="-n"
fi
case "$MACHINEARCH" in
i3li*|ppli*) N="$N -e" ;;
esac
_ss_headlines="--------------------------------------------------------------------------"

if [ -f $TTAMSG/common -a -f $TTAMSG/sso_restart ]; then
  . $TTAMSG/common
  . $TTAMSG/sso_restart
else
  echo "Unable to open message file: $TTAMSG/sso_restart"
  exit 1
fi

u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi

GetPrivUsrGrp()
{
    cat ${TTAINSTDIR}/var/serverconfig/global/array.properties 2>/dev/null | awk -F= \
	'BEGIN { u="ttasys"; wu="ttaserv"; g="ttaserv" }
	$1 == "tarantella.config.array.privilegeduser" { u = $2 }
	$1 == "tarantella.config.array.webserveruser" { wu = $2 }
	$1 == "tarantella.config.array.privilegedgroup" { g = $2 }
	END { print u " " wu " " g }'
}
_privusrgrp=`GetPrivUsrGrp`
privusr=`expr "$_privusrgrp" : '\([^ ]*\) .*'`
webservusr=`expr "$_privusrgrp" : '.* \([^ ]*\) .*'`
privgrp=`expr "$_privusrgrp" : '.* \([^ ]*\)$'`

GetSecurity()
{
   FILE=$1

   SECURITY=`tr '>' '\012' < ${FILE}  | tr '<' ' '  | grep security`
   VALUE=`echo ${SECURITY} | grep "Value"`
   if [ -z "${VALUE}" ]; then
      LINE=`grep -n security ${FILE} | awk '{print $1}' | tr ':' ' '`
      NEXT=`expr ${LINE} + 1`
      SECURITY=`head -${NEXT} ${FILE} | tail -1`
   fi

   echo ${SECURITY} | tr '"' ' ' |
   awk '{ for (i=1; i<NF; i++)
   {
       if ($i == "Value=")
       {
          n=(i+1)
          print $n
          break
       }
   } }'
}

GetWebGateConfig()
{
   CONFIG=$1

   # Try local first
   LOCAL_CONFIG="--tarantella-config-sso-server-${CONFIG}"
   CONF=`${TTAINSTDIR}/bin/tarantella config list ${LOCAL_CONFIG} | awk '{print $2}' | tr -d '\"'`
   if [ -z "${CONF}" ]; then
      GLOBAL_CONFIG="--tarantella-config-sso-${CONFIG}"
      CONF=`${TTAINSTDIR}/bin/tarantella config list ${GLOBAL_CONFIG} | awk '{print $2}' | tr -d '\"'`
   fi

   echo "${CONF}"
}


TTA=${TTADIR}/bin/tarantella

usage()
{
   echo "${restart_usage}" >&2
}

while [ -n "$1" ]; do
    case "$1" in
	--help)
	    usage
	    exit 0
	    ;;
	*)
	    usage
	    exit 0
	    ;;
    esac
    shift
done

# Is there a webgate.conf available in Apache?
APACHE_VERSION=`cat ${TTADIR}/var/info/apacheversion`
APACHE_CONF="${TTADIR}/webserver/apache/${APACHE_VERSION}/conf"
WEBGATE="${APACHE_CONF}/webgate.conf"

if [ ! -s "${WEBGATE}" ]; then
   # Get the location of webgate.conf from the SGD config
   CONF=`GetWebGateConfig webgateconf`

   if [ -z "${CONF}" ]; then
      echo "${lf_missing_conf}"
      exit 1
   fi 

   if [ ! -s "${CONF}" ]; then
      echo "${lf_invalid_conf}"
      exit 1
   fi 

   # Convert from the generic webgate.conf to SGD specific version
   # Include oam logout callback url handler directive, to set the
   # sgd cookie that indicates the oam server has issued a OAM
   # logout callback url
   [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
   LINE=`grep -n "<LocationMatch \"/\*\">" ${CONF} | ${AWK} -F ":" '{print $1}'`
   LENGTH=`wc -l ${CONF} | awk '{print $1}'`
   TMPFILE=tmp_webgate.conf
   TOP=`expr ${LINE} - 1`
   BOTTOM=`expr ${LENGTH} - ${LINE}`
   head -${TOP} ${CONF} > ${TMPFILE}
   echo "<LocationMatch \"/oam_logout_success\">" >> ${TMPFILE}
   echo "Header always set Cache-Control \"no-cache\"" >> ${TMPFILE}
   echo "Header always add Set-Cookie \"SGDOAMLOGGEDOUT=done; httponly\"" >> ${TMPFILE}
   echo "</LocationMatch>" >> ${TMPFILE}
   echo "" >> ${TMPFILE}
   echo "<LocationMatch \"/(sgd/\$|sgd/index.jsp|sgd/sso.jsp|sgd/hierarchy.jsp|sgd/thin.jsp|sgd/mydesktop/\$|sgd/mydesktop/index.jsp|obrar.cgi)\">" >> ${TMPFILE}
   tail -${BOTTOM} ${CONF} >> ${TMPFILE}
   mv ${TMPFILE} ${WEBGATE}
   chown ${privusr}:${privgrp} ${WEBGATE}

   # Edit httpd.conf to include the webgate.conf
   # Also add two Modules required by Apache 2.4 webgate
   echo "" >> ${APACHE_CONF}/httpd.conf
   echo "<IfDefine OAM>" >> ${APACHE_CONF}/httpd.conf
   echo "LoadModule alias_module modules/mod_alias.so" >> ${APACHE_CONF}/httpd.conf
   echo "LoadModule access_compat_module modules/mod_access_compat.so" >> ${APACHE_CONF}/httpd.conf
   echo "Include ${WEBGATE}" >> ${APACHE_CONF}/httpd.conf 
   echo "</IfDefine>" >> ${APACHE_CONF}/httpd.conf
fi

# Restart the webserver
if [ -f "${TTADIR}/webserver/.TWS_secure" ]; then
   ${TTA} restart webserver --https
else
   ${TTA} restart webserver --http
fi
