#!/bin/sh
#  $Id: sso_disable.sh 109677 2015-03-31 12:24:31Z pc161604 $
#  Copyright (c) 2014, 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_disable ]; then
  . $TTAMSG/common
  . $TTAMSG/sso_disable
else
  echo "Unable to open message file: $TTAMSG/sso_disable"
  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


TTA=${TTADIR}/bin/tarantella

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

PURGING=0

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

# Warn about restart of apache
echo "${lf_warnrestart}"
while :; do
   echo $N "${lf_confirm}" $C
   read reply
   case "${reply}" in
     [yY]*) break;;
     [nN]*) echo "${lf_nochanges}"; exit 0;;
   esac
done

TPLA=`cat ${TTADIR}/var/info/sso_tpla.orig 2> /dev/null`
if [ -z "${TPLA}" ]; then
   # No previous value of TPLA, so reset to 0
   TPLA=0
fi
# Reset TPLA
echo "${lf_loginauth}"
${TTA} config edit --tarantella-config-components-thirdpartyloginauthority ${TPLA} > /dev/null

# Turn off SSO
${TTA} config edit --tarantella-config-sso-enabled 0 > /dev/null

# Reset client overrides
echo "${lf_setpriv}"
OVERRIDES=`${TTA} config list --tarantella-config-applaunch-allowclientoverrides| awk '{print $2}' | sed -e s/\"//g | sed -e s/sso_pw//g | sed -e s/,,/,/g | sed -e s/,\$//g | sed -e s/\^,//g`
${TTA} config edit --tarantella-config-applaunch-allowclientoverrides ${OVERRIDES} > /dev/null

# If purging the config, delete all SSO settings both array and local
if [ "${PURGING}" -eq 1 ]; then
    echo "${lf_purge}"
    ${TTA} config edit --tarantella-config-sso-url "" --tarantella-config-sso-webgateinstance "" --tarantella-config-sso-webgateinstall "" --tarantella-config-sso-webgateconf "" --tarantella-config-sso-server-webgateinstance "" --tarantella-config-sso-server-webgateinstall "" --tarantella-config-sso-server-webgateconf "" > /dev/null
    
    # Clear the SGD version of webgate.conf and remove the reference from 
    # httpd.conf
    APACHE_VERSION=`cat ${TTADIR}/var/info/apacheversion`
    APACHE_CONF="${TTADIR}/webserver/apache/${APACHE_VERSION}/conf"
    WEBGATE="${APACHE_CONF}/webgate.conf"
    if [ -s "${WEBGATE}" ]; then
	mv -f "${WEBGATE}" "${APACHE_CONF}/webgate.conf.old"
	sed '/<IfDefine OAM>/,/<\/IfDefine>/d' ${APACHE_CONF}/httpd.conf > "${APACHE_CONF}/tmp"
	mv -f "${APACHE_CONF}/tmp" "${APACHE_CONF}/httpd.conf"
    fi
fi

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

echo ${lf_complete}
