#!/bin/sh
#  Copyright (C) 1997-2005 Tarantella, Inc.
#  $Id: archive.sh 109677 2015-03-31 12:24:31Z pc161604 $
#
#  Author: Bill Weir
#  Log file archiver 

# This is a generic product script used to establish an environment
# before starting/stopping a product or component.
# This script was built from shell library
#   $Id$
# This script has to run on every platform we support.  Any suggestions
# for improvement are welcome (but must be portable!)

# Version numbers
PRODUCT_VERSION="5.60.514"
PRODUCT_VERSION_FULL="5.60.514"
PRODUCT_VERSION_MAJOR=5
PRODUCT_VERSION_FEATURE=6
PRODUCT_VERSION_BUG=0
PRODUCT_VERSION_BUILD=514
PRODUCT_VERSION_BUILD_DECIMAL=514
PRODUCT_VERSION_AS_NUMBER=560514
PRODUCT_VERSION_CAB=5,6,0,514
PRODUCT_VERSION_RPM=5.60-514

# Product names
PRODUCT_FULL_NAME="Oracle Secure Global Desktop"
PRODUCT_SHORT_NAME="Secure Global Desktop"
PRODUCT_IDENT="SSGD"
PRODUCT_SHORT_FILENAME="Tarantella"
PRODUCT_LSBSCRIPTNAME="sun.com-sgd-base"
PRODUCT_LSBSCRIPT_RUN_LEVEL="90"
TSP_FULL_NAME="Oracle Secure Global Desktop Security Pack"
TSP_SHORT_NAME="Security Pack"
WCP_FULL_NAME="Oracle Secure Global Desktop Windows Connectivity Pack"
WCP_SHORT_NAME="Windows Connectivity Pack"
TN3270_FULL_NAME="Oracle Secure Global Desktop Mainframe Connectivity Pack"
TN3270_SHORT_NAME="Mainframe Connectivity Pack"
AS400_FULL_NAME="Oracle Secure Global Desktop AS/400 Connectivity Pack"
AS400_SHORT_NAME="AS/400 Connectivity Pack"
S1PSTK_FULL_NAME="Oracle Secure Global Desktop Channel for Sun ONE Portal Server"
S1PSIP_FULL_NAME="Oracle Secure Global Desktop Integration Pack for Sun ONE Portal Server"
S1PSTK_SHORT_NAME="Secure Global Desktop Channel for Sun ONE Portal Server"
S1PSIP_SHORT_NAME="Secure Global Desktop Integration Pack for Sun ONE Portal Server"

OBJECT_MGR_NAME="Object Manager"
ARRAY_MGR_NAME="Array Manager"
NATIVE_WIN16_FULL_NAME="Oracle Secure Global Desktop Native Client for Windows 3.1"
NATIVE_WIN16_SHORT_NAME="Native Client for Windows 3.1"
NATIVE_WIN32_FULL_NAME="Oracle Secure Global Desktop Native Client for Windows 95, 98 and NT"
NATIVE_WIN32_SHORT_NAME="Native Client for Windows 95, 98 and NT"
NATIVE_WYSE_FULL_NAME="Oracle Secure Global Desktop Native Client for Wyse"
NATIVE_WYSE_SHORT_NAME="Native Client for Wyse"
NATIVE_FULL_NAME="Oracle Secure Global Desktop Native Client"
NATIVE_SHORT_NAME="Native Client"
NATIVE_REGISTRY_NAME="Tarantella Native Client"
INTCLI_FULL_NAME="Oracle Secure Global Desktop Client"
INTCLI_SHORT_NAME="Client"
TCC_FULL_NAME="Oracle Secure Global Desktop Client"
TCC_HELPER_NAME="Oracle Secure Global Desktop Client Helper"
TCC_ACTIVEX_NAME="Oracle Secure Global Desktop ActiveX Control"
TCC_PLUGIN_NAME="Oracle Secure Global Desktop Netscape Plug-in"
T3COMPONENT_WIN_NAME="Oracle Secure Global Desktop Enhancement Module for Windows"
T3COMPONENT_UNIX_NAME="Oracle Secure Global Desktop Enhancement Module for UNIX(TM) and Linux Platforms"
T3COMPONENT_SHORT_UNIX_NAME="tem"
T3COMPONENT_NAME="Oracle Secure Global Desktop Enhancement Module"
T3COMPONENT_LSBSCRIPTNAME="sun.com-sgd-em"
T3COMPONENT_LSBSCRIPT_RUN_LEVEL="90"
GWCOMPONENT_UNIX_NAME="Oracle Secure Global Desktop Gateway for UNIX(TM) and Linux Platforms"
GWCOMPONENT_NAME="Oracle Secure Global Desktop Gateway"
GWCOMPONENT_SHORTNAME="SGD Gateway"
GWCOMPONENT_LSBSCRIPTNAME="sun.com-sgd-gw"
GWCOMPONENT_LSBSCRIPT_RUN_LEVEL="91"

# Watch the length of these names: IE refuses to remove packages if
# the name is too long.
ARCHIVE_NAME_ttalw="TCC Helper, version"
ARCHIVE_NAME_asad="3.x Framework Java Archive"
#ARCHIVE_NAME_tta="3.x Java Archive"
#ARCHIVE_NAME_tta="3.x Combined Java Archive"
ARCHIVE_NAME_tta="Client, "
ARCHIVE_NAME_tsp="3.x Security Java Archive"
ARCHIVE_NAME_proxy="3.x Proxy Java Archive"
WEBSTART_JAR_NAME="sgdwscs"
ARCHIVE_SUFFIX="K"

NS_REGKEY="java/download/com/tarantella/tta"

PRODUCT_VENDOR="Oracle Corporation, Inc."
PRODUCT_SHORT_VENDOR="Oracle"

# Capabilities:
# The format for the capabilities required by the client end of a
# connection is: CAPABILITIES_protocol_type_REQUIRED="expression"

# The format for the capabilities provided by the server end of a
# connection is: CAPABILITIES_protocol_type_PROVIDED="c1 c2 ... c3"

# Asad between a webtop client and server:
CAPABILITIES_ASAD_WEBTOPCLIENT_REQUIRED="(1.1 || 1.2)"
CAPABILITIES_ASAD_WEBTOPCLIENT_PROVIDED="1.1 1.2"

# Asad between two peer servers, or an admin tool and a server:
# 1.1 is core E3 array functionality.
# 1.2 is 3.01 array interoperability functionality.
# 2.1 is core Pisa client device mapping functionality (not a requirement)
CAPABILITIES_ASAD_PEER_REQUIRED="(1.1 || 1.2)"
CAPABILITIES_ASAD_PEER_PROVIDED="1.1 1.2 2.1"

# Client versions of server strings; same format, name prefixed by CLIENT
# Asad between a webtop client and server:
CLIENTCAPABILITIES_ASAD_WEBTOPCLIENT_REQUIRED="(1.1 || 1.2)"
CLIENTCAPABILITIES_ASAD_WEBTOPCLIENT_PROVIDED="1.1 1.2"

# Top-level item names in ENS datastore
DEFAULT_ROOT_ENS="organization"
SYSTEM_ROOT_ENS="Tarantella System Objects"
APP_ROOT_ENS="applications"
APPSERVER_ROOT_ENS="appservers"

WEB_SERVICES_VERSION=1.9


# be paranoid about the current path
if [ -d /bin ]; then
  PATH=/bin:$PATH
fi
if [ -d /sbin ]; then
  PATH=/sbin:$PATH
fi
if [ -d /usr/bin ]; then
  PATH=/usr/bin:$PATH
fi
if [ -d /usr/sbin ]; then
  PATH=/usr/sbin:$PATH
fi
if [ -d /etc ]; then
  PATH=/etc:$PATH
fi
export PATH

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 [ -z "$0" ]; then
  who_i_am=""
else
  who_i_am=`expr "//$0" : '.*/\(.*\)' `
fi

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

GetPrivUsrGrp()
{
    cat $TTADATADIR/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" : '.* \([^ ]*\)$'`

MkTtaDir()
{
    _newdirs="" _pflag=""
    case "$1" in
    -p) _pflag="-p"; shift
    esac
    for _d in "$@"; do
	while [ -n "$_d" ]; do
	    [ -d "$_d" ] && break
	    _newdirs="$_newdirs $_d"
	    _d=`dirname $_d`
	done
    done
    mkdir $_pflag "$@"
    [ -n "$_newdirs" ] && { chgrp $privgrp $_newdirs 2>/dev/null; chown $privusr $_newdirs 2>/dev/null; }
}


case "$trace" in
x) set -x
esac

if [ ! -z "$1" ]; then
  if [ "$1" = "--help" ]; then
    echo "$lf_usage" 1>&2
    exit 1
  else
    echo "$lf_badarg $1" 1>&2
    echo "$lf_usage" 1>&2
    exit 2
  fi
fi

pids=''
auxserverpid=`tail -1 $TTADATADIR/info/auxserver.pid 2>/dev/null`
cdmserverpid=`cat $TTADATADIR/info/cdmserver.pid 2>/dev/null`
multiserverpid=`cat $TTADATADIR/info/multiserver.pid 2>/dev/null`
if [ -n "$auxserverpid" ] ; then
    # Check what type of ps we have, and output a full listing of PID and PPID
    tmp=${TMPDIR:-/tmp}/tta$$ export tmp
    trap 'rm -f $tmp ; exit' 1 2 3 15

    case "$SCOARCH" in
    i3li*|ppli*)
        psflags="ajxww"
       	ps $psflags | awk '{print $2 " " $1}' >$tmp 2>/dev/null
	# 515502:1 broken 'sort -n' on RedHat 6.2 (and how many others?)
	if [ -z "$TTA_ARCHIVE_SORT_FLAG" ]; then
	    sort_numeric_flag=""
	    poss_sort_numeric_flags="n g"
	    for p in $poss_sort_numeric_flags; do
		sort_res=`echo "33 22 33"  | tr ' ' '\012' | sort -$p | tr '\012' ' '`
		# tr converts the final CR of the sort output to a space ...
		if [ "$sort_res" = "22 33 33 " ]; then
		    sort_numeric_flag=$p
		    break
		fi
	    done
	    if [ -z "$sort_numeric_flag" ]; then 
		echo "$lf_brokensort"
		exit 1
	    fi
	else
	    sort_numeric_flag=$TTA_ARCHIVE_SORT_FLAG
	fi
        ;;
    *)  
        psflags="-ef"
        ps $psflags | awk '{print $2 " " $3}' >$tmp 2>/dev/null
        if [ $? -ne 0 -o ! -s "$tmp" ] ; then
            psflags="-ajxww"
       	    ps $psflags | awk '{print $2 " " $1}' >$tmp 2>/dev/null
        fi
	sort_numeric_flag="n"
    esac

    # starting from the auxserver, output all descendant process ids
    pids="$auxserverpid"
    while : ; do
	count=`echo "$pids" | wc -w`
	x=''
	for i in $pids ; do
	    x="$x `awk '$2=='$i' {print $1}' $tmp`"
	done
	pids=`echo "$pids $x" | tr -s ' ' '\012' | sort -u$sort_numeric_flag`
	[ `echo "$pids" | wc -w` -eq $count ] && break
    done
    rm -f $tmp
fi

if [ -z "$TTADATADIR" -o ! -d "$TTADATADIR/log" ]; then
    echo "$0: $lf_baddiag $TTADATADIR/log" 1>&2
    exit 3
fi
logdirs="$TTADATADIR/log"

# There's only one directory in $logdirs at present, but we'll do this
# as a for-loop in case log directories ever become configurable per
# protocol engine.
#
for a_diagdir in $logdirs ; do
(
    cd $a_diagdir
 
    if [ -z "`ls | egrep '\.(log|jsl)$' | sed 1q`" ] ; then
	echo "$0: $lf_nologs $a_diagdir" 1>&2
	continue
    fi

    # highest numbered archive directory
    a_archives=7

    # remove largest number
    rm -rf $a_archives
    if [ -d "$a_archives" -o -f "$a_archives" ]; then
	echo "$0: $lf_badremove $a_diagdir/$a_archives" 1>&2
	continue
    fi

    # move along all existing dirs        
    while [ "$a_archives" != "1" ]; do
	newmaxarchives=`expr $a_archives - 1`
	mv $newmaxarchives $a_archives >/dev/null 2>&1
	if [ -d "$newmaxarchives" -o -f "$newmaxarchives" ]; then
	    echo "$0: $lf_badmove $a_diagdir/$newmaxarchives" 1>&2
	    continue
	fi
	a_archives=$newmaxarchives
    done

    # create new archive dir
    MkTtaDir 1 >/dev/null 2>&1
    if [ ! -d 1 ]; then
	echo "$0: $lf_badcreate $a_diagdir/1" 1>&2
	continue
    fi
    chmod 711 1 >/dev/null 2>&1

    # write summary info into new dir
    $TTABINDIR/tarantella query errlog all --server localhost >1/summary.txt 2>&1

    if [ "$?" != "0" ]; then
	echo "$0: $lf_badquery: tarantella query errlog all" 1>&2
	continue
    fi

    mv_list="${TMPDIR:-/tmp}/ttam.$$"   ; :>$mv_list
    dead_list="${TMPDIR:-/tmp}/ttad.$$" ; :>$dead_list
    trap 'rm -f $mv_list $dead_list ; exit' 1 2 3 15
  
    ls | egrep '\.(log|jsl)$' | while read logfile ; do
	case "$logfile" in
      # Don't archive patch log file
      patch.log)
          continue;;
	*_error.log|*_error.jsl) 
	    pid=`expr "$logfile" : '[^0-9][^0-9]*\([0-9][0-9]*\)_error\....$'`;;
	*)
	    pid=`expr "$logfile" : '[^0-9][^0-9]*\([0-9][0-9]*\)\....$'`
	esac
	if [ -n "$pid" ] ; then
	    if [ -z "`echo "$pids" | grep \"^$pid\$\"`" ] ; then
		# log file name contains a pid, and pid is not active
		echo $logfile >>$dead_list
	    fi
	fi
	echo $logfile >>$mv_list
    done

    # move all logs into archive directory
    [ -s "$mv_list" ] && xargs $TTABINDIR/scripts/mvto 1 <$mv_list 2>/dev/null

    if [ -n "$auxserverpid" ]; then
	# send SIGHUP to the auxserver: it will propagate the signal to all
	# the active tta processes to make them reopen their log files
	kill -HUP $auxserverpid
	sleep 5
    fi

    if [ -n "$cdmserverpid" ]; then
	# send SIGUSR1 to the cdmserver: it will propagate the signal to all
	# the active tta processes to make them reopen their log files
	kill -USR1 $cdmserverpid
        sleep 5
    fi

    if [ -n "$multiserverpid" ]; then
	# send SIGHUP to the ttamulti: 
	kill -HUP $multiserverpid 2> /dev/null
        sleep 5
    fi

    # create new print log files
    :>print.log; :>print_error.log
    chmod 600 print.log print_error.log 2>/dev/null
    chgrp $privgrp print.log print_error.log 2>/dev/null
    chown $privusr print.log print_error.log 2>/dev/null
  
    # now go and tidy up in the archived directory
    cd 1

    # compress old (non-active) log files
    [ -s "$dead_list" ] && xargs compress <$dead_list >/dev/null 2>&1

    # compress all remaining log files provided they have been re-created in
    # the active log directory
    ls | egrep '\.(log|jsl)$' | xargs -i@ sh -c '[ -f "../@" ] && compress "@"' >/dev/null 2>&1

    # get rid of empty files
    ls | egrep '\.(log|jsl)$' | xargs -i@ sh -c '[ -s "@" ] || rm -f "@"'

    rm -f $mv_list $dead_list
)
done

# finished successfully
exit 0
