#!/bin/sh
#  Copyright (C) 1999-2005 Tarantella, Inc.
#  $Id: printing.sh,v 1.18 2005/07/19 12:43:13 billwe Exp $
#
#  Author: Bill Weir
#  Printing support conversion script

# 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: startup.shlib,v 10.165.6.1 2006/04/03 09:51:35 billwe Exp $
# This script has to run on every platform we support.  Any suggestions
# for improvement are welcome (but must be portable!)

# Version numbers
PRODUCT_VERSION="4.2"
PRODUCT_VERSION_FULL="4.20.983"
PRODUCT_VERSION_MAJOR=4
PRODUCT_VERSION_FEATURE=2
PRODUCT_VERSION_BUG=0
PRODUCT_VERSION_BUILD=983
PRODUCT_VERSION_CAB=4,2,0,983

# Product names
PRODUCT_FULL_NAME="Sun Secure Global Desktop Software"
PRODUCT_SHORT_NAME="Secure Global Desktop"
PRODUCT_SHORT_FILENAME="Tarantella"
TSP_FULL_NAME="Sun Secure Global Desktop Security Pack"
TSP_SHORT_NAME="Security Pack"
WCP_FULL_NAME="Sun Secure Global Desktop Windows Connectivity Pack"
WCP_SHORT_NAME="Windows Connectivity Pack"
TN3270_FULL_NAME="Sun Secure Global Desktop Mainframe Connectivity Pack"
TN3270_SHORT_NAME="Mainframe Connectivity Pack"
AS400_FULL_NAME="Sun Secure Global Desktop AS/400 Connectivity Pack"
AS400_SHORT_NAME="AS/400 Connectivity Pack"
S1PSTK_FULL_NAME="Sun Secure Global Desktop Channel for Sun ONE Portal Server"
S1PSIP_FULL_NAME="Sun 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="Sun Secure Global Desktop Native Client for Windows 3.1"
NATIVE_WIN16_SHORT_NAME="Native Client for Windows 3.1"
NATIVE_WIN32_FULL_NAME="Sun 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="Sun Secure Global Desktop Native Client for Wyse"
NATIVE_WYSE_SHORT_NAME="Native Client for Wyse"
NATIVE_FULL_NAME="Sun Secure Global Desktop Native Client"
NATIVE_SHORT_NAME="Native Client"
NATIVE_REGISTRY_NAME="Tarantella Native Client"
TCC_FULL_NAME="Sun Secure Global Desktop Client"
TCC_HELPER_NAME="Sun Secure Global Desktop Client Helper"
TCC_ACTIVEX_NAME="Sun Secure Global Desktop ActiveX Control"
TCC_PLUGIN_NAME="Sun Secure Global Desktop Netscape Plug-in"
T3COMPONENT_WIN_NAME="Sun Secure Global Desktop Enhancement Module for Windows"
T3COMPONENT_UNIX_NAME="Sun Secure Global Desktop Enhancement Module for UNIX/Linux"
T3COMPONENT_SHORT_UNIX_NAME="tem"
T3COMPONENT_NAME="Sun Secure Global Desktop Enhancement Module"

# 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"

ARCHIVE_SUFFIX="F"

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

PRODUCT_VENDOR="Sun Microsystems, Inc."
PRODUCT_SHORT_VENDOR="Sun"

# 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"


if [ -z "$0" ]; then
  who_i_am=""
else
  who_i_am=`expr "//$0" : '.*/\(.*\)' `
fi

lf_nomessages="No message files installed for LANG="
l_langdefault="Attempting to use default English (en-us) messages"
l_langunknown="Unrecognized \$LANG: "
l_langunset="\$LANG is not set."
lf_langright="Please check the \$LANG variable is correct."
l_langsetto="\$LANG is currently set to: "
l_gottheselangs="Message files are installed for the following languages:"
l_usinglang="Using messages in"
[ -z "$LANG" ] && LANG="C"

TTALANG=$LANG
case "$TTALANG" in
C)  TTALANG="en-us"
    ;;
[a-z][a-z]_[A-Z][A-Z])
    # convert LANG format from ab_CD to ab-cd
    TTALANG=`echo "$TTALANG" | LANG=C LC_ALL=C tr '[A-Z]_' '[a-z]-'`
esac

if [ ! -d $TTALANG -o -z "$TTALANG" ]; then
  [ ! -z "$TTALANG" ] && posslangs=`find . -type d -name "$TTALANG*" -print 2>/dev/null | sed 's;.*\/.*\/.*;;'`

  [ -z "$posslangs" ] && posslangs=`find . -type d -name '[a-z][a-z]-[a-z][a-z]*' -print 2>/dev/null | sed 's;.*\/.*\/.*;;'`

  nposslangs=`echo $posslangs | wc -w`
  if [ "$nposslangs" -gt 1 ]; then
    case "$TTALANG" in
      ??* )
            echo "${lf_nomessages}${LANG}" #!!!
	    echo "$lf_langright"
            ;;
      "" )
           echo "$l_langunset"
           ;;
      * )
          TTALANG=en-us
          echo "$l_langunknown $LANG"
          echo "$l_langdefault"
          ;;
    esac
    echo "$l_gottheselangs"
    tmpTTALANG=$TTALANG
    for a_lang in $posslangs; do
      output=`echo $a_lang | sed -e 's/.*\///'`
      echo "  $output"
      if [ -f $output/common -o -f ../$output/common ]; then
	[ "$tmpTTALANG" != "en-us" ] && tmpTTALANG=$output
      fi
    done
    [ "$TTALANG" = "$tmpTTALANG" ] && TTALANG=en-us || TTALANG=$tmpTTALANG	
    echo "$l_usinglang $TTALANG"
  elif [ "$nposslangs" -eq 1 ]; then
    TTALANG=`echo $posslangs | sed -e 's/.*\///'`
  else
    echo "$lf_reinst"
    exit 5
  fi
fi

TTAMSG=`pwd`/$TTALANG
export TTAMSG TTALANG
if [ $trace ]; then echo "   trace: Language dir = $TTAMSG"; fi

if [ -f $TTAMSG/common ]; then
  . $TTAMSG/common
else
  _lang=`basename $TTAMSG`
  _parent=`dirname $TTAMSG`
  _parent=`dirname $_parent`
  if [ -f $_parent/$_lang/common ]; then
    . $_parent/$_lang/common
  else
    echo "Unable to open message file: $TTAMSG/common"
    exit 1
  fi
fi
if [ -f $TTAMSG/setup ]; then
  . $TTAMSG/setup
else
  echo "Unable to open message file: $TTAMSG/setup"
  exit 1
fi

GetPrivUsrGrp()
{
    cat $datadir/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

[ -z "$PKGINST" -a -f "${instdir}/etc/pkg/ttainstance" ] && PKGINST=`cat "${instdir}/etc/pkg/ttainstance" 2>/dev/null`

DoInstallf()
{
    [ -z "${TTA_RPM}" ] && [ -n "$PKGINST" ] && installf "$@"
}

DoCreate()
{
    # Create new installation.
    [ ! -d $datadir/spool ] && MkTtaDir -p $datadir/spool
    [ ! -d $datadir/print/queue ] && MkTtaDir -p $datadir/print/queue
    chmod 733 $datadir/spool
    chmod 1733 $datadir/spool 2>/dev/null
    chmod 711 $datadir/print
    chmod 4700 $datadir/print/queue
    SHORTLANG="en"
    [ -n "$TTALANG" ] && SHORTLANG="`expr $TTALANG : '\(..\).*'`"
    prtinstall=$instdir/bin/scripts/prtinstall.${SHORTLANG}.sh
    if [ ! -f $prtinstall ]; then
	echo "${m_noprtinstallscript}" >&2
	return 1
    fi
    #
    # patch some config information into the lp_interface script
    #
    LP_INTERFACE=$instdir/bin/scripts/lp_interface.${SHORTLANG}
    PRINTTESTPAGE=$instdir/bin/scripts/printtestpage.${SHORTLANG}.sh
    PRINT_MOVE=$instdir/bin/scripts/print_move
    TTAPRINTER=$instdir/var/docroot/cgi-bin/ttaprinter.cgi
    tmp=$INSTDIR/var/tmp/tta_printing.$$
    trap 'rm -f $tmp; exit' 1 2 3 15
    SHORTARCH=`expr "$SCOARCH" : '\(....\).*'`
    sed "s|@@ARCH@@|$SHORTARCH|g;s|@@TTADIR@@|$instdir|g" ${LP_INTERFACE} >$tmp
    cp $tmp ${LP_INTERFACE}
    DoInstallf $PKGINST ${LP_INTERFACE}
    sed "s|@@ARCH@@|$SHORTARCH|g;s|@@TTADIR@@|$instdir|g" ${PRINTTESTPAGE} >$tmp
    cp $tmp ${PRINTTESTPAGE}
    DoInstallf $PKGINST ${PRINTTESTPAGE}
    sed "s|@@ARCH@@|$SHORTARCH|g;s|@@TTADIR@@|$instdir|g" ${PRINT_MOVE} >$tmp
    cp $tmp ${PRINT_MOVE}
    DoInstallf $PKGINST ${PRINT_MOVE}
    sed "s|@@ARCH@@|$SHORTARCH|g;s|@@TTADIR@@|$instdir|g" ${TTAPRINTER} >$tmp
    cp $tmp ${TTAPRINTER}
    DoInstallf $PKGINST ${TTAPRINTER}
    rm -f $tmp
    #
    # Create the tarantella printers.
    #
    PDF_PRINTERNAME='' PRINTERNAME='' TTAPRINTER_CONF="/etc/ttaprinter.conf"
    if [ -n "${peerdns}" -a -s "${TTAPRINTER_CONF}" ]; then
	# if /etc/ttaprinter.conf exists, get printer names from there instead of using defaults
	PDF_PRINTERNAME=`sed "/LPDEST=.*#TTA_HOST=${peerdns}/!d;s/.*LPDEST=//;s/[ 	].*//" ${TTAPRINTER_CONF} 2>/dev/null | tail -1 | egrep -i pdf`
	PRINTERNAME=`sed "/LPDEST=.*#TTA_HOST=${peerdns}/!d;s/.*LPDEST=//;s/[ 	].*//" ${TTAPRINTER_CONF} 2>/dev/null | tail -1 | egrep -v -i pdf`
    fi
    sh $prtinstall --ttahost localhost --ttaprinter ${PDF_PRINTERNAME:-tta_pdfprinter} --nocheck 2>&1
    sh $prtinstall --ttahost localhost --ttaprinter ${PRINTERNAME:-tta_printer} --append --nocheck 2>&1
    ret=$?
    DoInstallf $PKGINST $instdir/bin/lp
    DoInstallf $PKGINST $instdir/bin/lpr
    DoInstallf -f $PKGINST
    return $ret
}

DoUpgrade()
{
    # Convert/upgrade old installation.
    #
    # Nothing to upgrade: just create a new installation.
    [ -d $datadir/spool/queue ] && rmdir $datadir/spool/queue 2>/dev/null
    [ -p $datadir/printfifo ] && rm $datadir/printfifo
    DoCreate
    return $?
}

if [ -z "$oldinstdir" ]; then
    DoCreate
    exitCode=$?
else
    DoUpgrade
    exitCode=$?
fi

exit $exitCode
