#! /bin/sh
#  Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
#  $Id: templates.sh 109677 2015-03-31 12:24:31Z pc161604 $
#
#  Author: TobyD
#  Copy files from templates directory.

# 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


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 ?) TTALANG=en-us;; esac
    tmpTTALANG=$TTALANG
    for a_lang in $posslangs; do
      output=`echo $a_lang | sed -e 's/.*\///'`
      if [ -f $output/common -o -f ../$output/common ]; then
	[ "$tmpTTALANG" != "en-us" ] && tmpTTALANG=$output
      fi
    done
    [ "$TTALANG" = "$tmpTTALANG" ] && TTALANG=en-us || TTALANG=$tmpTTALANG	
  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


CopyExtras()
{
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" pattern="${3:-.}" namepattern="$4"
    if [ -d "$src" ]; then
    (
      cd $src
      mkdir -p $dest 2>/dev/null
	find $pattern -depth $namepattern -print 2>/dev/null |
	    cpio -pdum $dest 2>/dev/null
	find $pattern \( ! -type d \) $namepattern -print 2>/dev/null |
	    sed 's/^\.\///' |
	    xargs -i@ echo $cmd $src/@ $dest/@ >>$instdir/etc/pkg/$pkginst/extras
    )
    fi
}

### Copy files to INSTDIR/var, no record is kept, files will not be
### removed during uninstall
NewCopyExtras()
{
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" pattern="${3:-.}" namepattern="$4"
    if [ -d "$src" ]; then
    (
      cd $src
      mkdir -p $dest 2>/dev/null
      find $pattern -depth $namepattern -print 2>/dev/null |
          cpio -pdum $dest 2>/dev/null
    )
    fi
}

### As NewCopyExtras(), but don't copy over the top of an 
### existing file
MaybeCopyExtras()
{
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" pattern="${3:-.}" namepattern="$4"
    if [ -d "$src" ]; then
    (
	cd $src
	mkdir -p $dest 2>/dev/null
	for copy in `find $pattern -depth $namepattern -print 2>/dev/null`; do
	    [ ! -f "$dest/$copy" -a ! -d "$copy" ] && echo "$copy"
	done | cpio -pdum $dest 2>/dev/null
    )
    fi   
}  

### As NewCopyExtras(), but don't copy if dest file exists and is different
### to template file
MaybeCopyExtras2()
{
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" template="$3"
    if [ -d "$src" ]; then
    (
        # a list of any files that the administrator has edited by hand or added
        NotUpgOut=$instdir/var/log/customized.list

        # a list of any files that the administrator has edited, and which would also 
        # have been changed by the upgrade 
        CustChanged=$instdir/var/log/customizedchanged.list
        CustMissing=$instdir/var/log/customized.missing

        cd $src
	mkdir -p $dest 2>/dev/null
	for copy in `find . -depth -print 2>/dev/null`; do
            case $copy in
                *_0x3d_*) thecopy=`echo $copy | sed 's;_0x3d_;=;g'` ;;
                * ) thecopy=$copy ;;
            esac	    
            if [ -f "$dest/$thecopy" ]; then
                res_cmp=`cmp -s $template/$copy $dest/$thecopy; echo $?` 
                case $res_cmp in
                    0 ) echo "$copy" ;;
                    1 ) echo "$dest/$thecopy" >> $NotUpgOut
                        # If the new file is different to the original template
                        res=`cmp -s $copy $template/$copy; echo $?`
                        case $res in
                            0 ) : ;;
                            1 ) echo "$dest/$thecopy" >>$CustChanged ;;
                            2 ) echo "$template/$copy" >>$CustMissing ;;
                        esac 
                    ;;                   
                    2 ) echo "$template/$copy" >>$CustMissing ;;
                esac
            elif [ ! -d $copy ]; then
                echo $copy 
            fi
	done | cpio -pdum $dest 2>/dev/null
    )
    fi   
}  

### Given the top level of an installed directory of webtop or login 
### themes ($2) check each theme directory against a template 
### theme directory ($3), if any file in a theme is found 
### to be modified, don't upgrade that theme from new templates ($1).
### Potentially, the files copied are .../locale_0x3d_..., renaming is handled
### by the files_rename script that is run after the templates script.
MaybeCopyExtras3()
{
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" template="$3"
    if [ -d "$src" ]; then
    (
        # a list of any files that the administrator has edited by hand or added
        NotUpgOut=$instdir/var/log/customized.list

        # a list of any files that the administrator has edited, and which would also 
        # have been changed by the upgrade 
        CustChanged=$instdir/var/log/customizedchanged.list
        CustMissing=$instdir/var/log/customized.missing

        cd $src
	mkdir -p $dest 2>/dev/null
        # get a list of all themes we want to install
        for theme in `ls -d * | grep -v "^\.$"`; do
            mod_flag=0
            # if a theme of the same name is already installed, check against 
            # the original template
            if [ -d $dest/$theme ]; then
  	        for copy in `find $theme -depth -print 2>/dev/null`; do
                    # locale templates haven't had the '0x3d' -> '=' conversion
                    case $copy in
                        *_0x3d_*) thecopy=`echo $copy | sed 's;_0x3d_;=;g'` ;;
                        * ) thecopy=$copy ;;
                    esac
	            if [ -f "$dest/$thecopy" ]; then
                        res_cmp=`cmp -s "$template/$copy" "$dest/$thecopy"; echo $?` 
                        case $res_cmp in
                            0 ) : ;;
                            1 ) # files are different, don't upgrade
                                mod_flag=1 
                                echo "$dest/$thecopy" >>$NotUpgOut 
                                # If the new file is different to the original template
                                res=`cmp -s $copy $template/$copy; echo $?`
                                case $res in
                                    0 ) : ;;
                                    1 ) echo "$dest/$thecopy" >>$CustChanged ;;
                                    2 ) echo $template/$copy >>$CustMissing ;;
                                esac
                            ;;
                            2 ) # cmp failed, possibly 'cos no template file - user added file?
                                echo "$template/$copy" >> $CustMissing ;;
                        esac
                    fi
                done
            fi
            case $mod_flag in
                0) find $theme -depth -print 2>/dev/null;;
                1) echo "Not upgrading theme $src/$theme" >> $NotUpgOut ;;
            esac
	done | cpio -pdum $dest 2>/dev/null
    )
    fi   
}  

### Almost identical to CopyExtras3, except that all themes are overwritten
### This just serves to tell the user what modifed bits have been changed.
### There is a check at the end to warn of user added themes that they will
### need to fix to work with 3.3.
### fitz/603863 for more.
MaybeCopyExtras4()
{
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" template="$3"
    if [ -d "$src" ]; then
    (
        # a list of any files that the administrator has edited by hand or added
        NotUpgOut=$instdir/var/log/customized.list

        # a list of any files that the administrator has edited, and which would also 
        # have been changed by the upgrade 
        CustChanged=$instdir/var/log/customizedchanged.list
        CustMissing=$instdir/var/log/customized.missing

        cd $src
	mkdir -p $dest 2>/dev/null
        # get a list of all themes we want to install
        for theme in `ls -d * | grep -v "^\.$"`; do
            mod_flag=0
            # if a theme of the same name is already installed, check against 
            # the original template
            if [ -d $dest/$theme ]; then
  	        for copy in `find $theme -depth -print 2>/dev/null`; do
                    # locale templates haven't had the '0x3d' -> '=' conversion
                    case $copy in
                        *_0x3d_*) thecopy=`echo $copy | sed 's;_0x3d_;=;g'` ;;
                        * ) thecopy=$copy ;;
                    esac
	            if [ -f "$dest/$thecopy" ]; then
                        res_cmp=`cmp -s "$template/$copy" "$dest/$thecopy"; echo $?` 
                        case $res_cmp in
                            0 ) : ;;
                            1 ) # files are different, don't upgrade
                                mod_flag=1 
                                echo "File $dest/$thecopy changed.">>$CustChanged
                                # don't care about original and new template diffs
                                # it's going to get replaced, whatever
                            ;;
                            2 ) # cmp failed, possibly 'cos no template file - user added file?
                                echo "$template/$copy" >> $CustMissing ;;
                        esac
                    fi
                done
            fi
            case $mod_flag in
                1) 
                    echo "Modified theme $dest/$theme has been replaced" >> $CustChanged 
                ;;
            esac
            find $theme -depth -print 2>/dev/null
	done | cpio -pdum $dest 2>/dev/null

        # now check for user added themes that won't have been upgraded
        cd $dest
        for theme in `ls -d * | grep -v "^\.$"`; do
            if [ ! -d $src/$theme ]; then
                echo "Theme $theme in $dest not upgraded" >> $NotUpgOut
            fi
        done
    )
    fi   
}  

### Given a directory of expect scripts
### $1 new templates
### $2 dir to install to
### $3 old templates for comparison
CopyExpect()
{
    set -x
    cmd="cp"
    case "$1" in
    --link) cmd="ln -s"; shift
    esac
    src="$1" dest="$2" template="$3"
    if [ -d "$src" ]; then
    (
        # a list of any files that the administrator has edited by hand
        # that we are going to everwrite
        ExpectUpg=$instdir/var/log/expectcustomized.list
        rm -f $ExpectUpg

        cd $src
        mkdir -p $dest 2>/dev/null
        # if a script of the same name is already installed, check against 
        # the original template
        for copy in `find . -type f -depth -print 2>/dev/null`; do 
            if [ -f "$dest/$copy" ]; then
                res_cmp=`cmp -s "$template/$copy" "$dest/$copy"; echo $?` 
                case $res_cmp in
                    1 ) # files are different, or we are about to install a
                        # new one, but they already have one of the same name.
                        # warn user
                        echo "$dest/$copy" >>$ExpectUpg
                    ;;
                esac
            fi
        done
        find . -depth -print 2>/dev/null | cpio -pdum $dest 2>/dev/null
        cd $dest
        for f in *; do
            if [ ! -f $src/$f -a "$f" != "wcpwts.exp" ]; then
                echo "File $dest/$f" >>$NotUpgOut
            fi      
        done
    )
    fi
    set +x
}  


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

templatedir=$instdir/etc/templates

tmplbkup=$templatedir/../templates.$oldversion
bkver=0
while [ -d ${tmplbkup}.`expr $bkver + 1` ]; do bkver=`expr $bkver + 1`; done
[ -d ${tmplbkup}.$bkver ] && tmplbkup=${tmplbkup}.$bkver

data="etc/data"
docroot=var/docroot 

java="var/docroot/java"
expect="var/serverresources/expect"
webtops="var/docroot/resources/webtops/sco/tta"
login="var/docroot/resources/login/sco/tta"
icons="var/docroot/resources/icons/sco/tta"
sgdicons="var/docroot/resources/icons/sgddir"
sgdstrings="var/docroot/resources/strings/sgddir"
info="var/docroot/resources/info/sco/tta"
sounds="var/docroot/resources/sounds/sco/tta"

############################################################################
# If there have been local customisations to default.printerinfo.txt, try to
# merge them into the latest version of the file.
#
MergePrinterInfo()
{
    bk=\\; pnLabel="${bk}[Previous Names${bk}]" uxLabel="${bk}[UNIX${bk}]"
    tmplPinfo="${templatedir}/${data}/default.printerinfo.txt"
    instPinfo="${instdir}/${data}/default.printerinfo.txt"

    [ ! -f ${tmplPinfo} -o ! -f ${instPinfo} ] && return
    cmp -s ${tmplPinfo} ${instPinfo} && return

    # Template and actual default.printerinfo.txt files both exist, and are
    # different.  Let's go to work.

    td="${TMPDIR:-/tmp}"
    tpn="${td}/pinfoA$$" tux="${td}/pinfoB$$" opn="${td}/pinfoC$$" oux="${td}/pinfoD$$" tmod="${td}/pinfoE$$"
    trap 'rm -f ${tpn} ${tux} ${opn} ${oux} ${tmod} >/dev/null 2>&1' 1 2 3 15
    # Extract customisable bits from old and new default.printerinfo.txt
    awk '/^'"${pnLabel}"'/ { state = 1; next }
	state == 1 { if (NF == 0) exit; print }' ${tmplPinfo} >${tpn}
    awk '/^'"${uxLabel}"'/ { state = 1; next }
	state == 1 { if (NF == 0) exit; print }' ${tmplPinfo} >${tux}
    awk '/^'"${pnLabel}"'/ { state = 1; next }
	state == 1 { if (NF == 0) exit; print }' ${instPinfo} >${opn}
    awk '/^'"${uxLabel}"'/ { state = 1; next }
	state == 1 { if (NF == 0) exit; print }' ${instPinfo} >${oux}
    if cmp -s ${tpn} ${opn} && cmp -s ${tux} ${oux}; then
	# No customisations: just copy the new template file.
	cp ${tmplPinfo} ${instPinfo}
    else
	# Merge existing customisations into new template file.
	awk '/^'"${pnLabel}"'/ {
	    print; system("cat '"${opn}"'")
	    state = 1; next
	}
	/^'"${uxLabel}"'/ {
	    print; system("cat '"${oux}"'")
	    state = 1; next
	}
	state == 1 {
	    if (NF == 0) state = 0
	    else next
	}
	{
	    print
	}' ${tmplPinfo} >${tmod}
	# Crude sanity check that nothing appears to have gone drastically wrong
	oldSize=`wc -c <${tmplPinfo}` newSize=`wc -c <${tmod}`
	if [ ${newSize} -gt `expr ${oldSize} \* 9 / 10` ]; then
	    # Copy the (modified) new template file.
	    cp ${tmod} ${instPinfo}
	fi
    fi
    rm -f ${tpn} ${tux} ${opn} ${oux} ${tmod} >/dev/null 2>&1
}

DoCreate ()
{
    basedirs="$@"
    [ -z "$basedirs" ] && basedirs="$data $expect $webtops $login $icons $sgdicons $sgdstrings $info $sounds $docroot"

    # a list of any files that the administrator has edited by hand or added
    NotUpgOut=$instdir/var/log/customized.list

    # a list of any files that the administrator has edited, and which would also 
    # have been changed by the upgrade 
    CustChanged=$instdir/var/log/customizedchanged.list
    CustMissing=$instdir/var/log/customized.missing
    rm -f $NotUpgOut $CustChanged $CustMissing 2>/dev/null

    for basedir in $basedirs; do
	case $basedir in
	    $docroot )
		if [ ! -z "$jive_PkgInst" ]; then
		    # fitz/516961 free these from pkg database.
		    [ -z "${TTA_RPM}" -a -z "${TTA_DEVINSTALL}" ] && {
			removef $jive_PkgInst $instdir/$docroot/*.html >/dev/null 2>&1 
			removef -f $jive_PkgInst 1>&2
		    }
		fi

                (

                    cd $templatedir/$docroot
                    for f in `ls -d *.html 2>/dev/null`; do
                        if [ -f $instdir/$docroot/$f ]; then
                            res_cmp=`cmp -s $tmplbkup/$docroot/$f $instdir/$docroot/$f; echo $?`
                            # has it changed from the original template?
                            case $res_cmp in
                                1) 
                                    # it's changed, warn them. 
                                    echo "File $instdir/$docroot/$f has been replaced" >> $CustChanged 
                                    # no point in checking whether it changed between releases
                                    # we're going to replace it anyway.
                                ;;
                            esac
                        fi
                        cp $f $instdir/$docroot/$f
                    done
                )

		# fitz/600376 - ttapatch 3.01.907 owns stuff in docroot.
		# if we don't removef them here, they'll get deleted when ttapatch get uninstalled.
		for pp in $oldpkg; do case $pp in ttapatch* ) patchpkg=$pp ;; esac; done
		if [ ! -z "$patchpkg" ]; then
		    javadir="$instdir/$docroot/java"
		    [ -z "${TTA_RPM}" -a -z "${TTA_DEVINSTALL}" ] && {
			find "$javadir" -type f -print 2>/dev/null | xargs removef $patchpkg >/dev/null 2>&1 
			removef -f $patchpkg 1>&2
		    }
		fi
	    ;;
	    * )
		if [ ! -z "$jive_PkgInst" ]; then
		    [ -z "${TTA_RPM}" -a -z "${TTA_DEVINSTALL}" ] && {
			find $instdir/$basedir -type f -print 2>/dev/null | xargs removef $jive_PkgInst >/dev/null 2>&1 
			removef -f $jive_PkgInst 1>&2
		    }
		fi
		case $basedir in
		    $data )
# get the package name
shortpkgname=`echo ttawcp | sed 's,^\(.*\)\..*$,\1,'`
# get info on instances of this package
pkginfo=`pkginfo $shortpkgname.\* 2>/dev/null | awk '{printf (" %s ",$2)}'`
res=$?
if [ $res -eq 0 ]; then
    # get list of package instances
    wcplist="$pkginfo"
else
    wcplist=""
fi

			for wcpinst in $wcplist; do
			    [ -z "${TTA_RPM}" -a -z "${TTA_DEVINSTALL}" ] && {
				find $instdir/$basedir -type f -print 2>/dev/null | xargs removef $wcpinst >/dev/null 2>&1 
				removef -f $wcpinst 1>&2
			    }
			done
                        # 603937 ttapatch 3.30.937 owns stuff in etc/data/keymaps
                        for pp in $oldpkg; do case $pp in ttapatch* ) patchpkg=$pp ;; esac; done
                        if [ ! -z "$patchpkg" ]; then
                            keydir="$instdir/$data/keymaps"
                            [ -z "${TTA_RPM}" -a -z "${TTA_DEVINSTALL}" ] && {
				find $keydir -type f -print 2>/dev/null | xargs removef $patchpkg >/dev/null 2>&1 
				removef -f $patchpkg 1>&2
			    }
                        fi
		    ;;
		esac
        case $basedir in
            $data)
                MaybeCopyExtras2 $templatedir/$basedir $instdir/$basedir $tmplbkup/$basedir
                MergePrinterInfo
            ;;
            $webtops|$login)
                case $oldversion in
                    3.3*)
                        # fitz/604610 changes to keepalive values in boot login theme
                        if [ "$basedir" = "$login" ]; then
                            # overwrite
                            MaybeCopyExtras4 $templatedir/$basedir $instdir/$basedir $tmplbkup/$basedir   
                        else
                            # leave them alone
                            MaybeCopyExtras3 $templatedir/$basedir $instdir/$basedir $tmplbkup/$basedir
                        fi
                        ;;
                    * )
                        MaybeCopyExtras3 $templatedir/$basedir $instdir/$basedir $tmplbkup/$basedir
                    ;;
                esac
            ;;
            $expect)
                CopyExpect $templatedir/$basedir $instdir/$basedir $tmplbkup/$basedir
            ;;
            * )
                MaybeCopyExtras2 $templatedir/$basedir $instdir/$basedir $tmplbkup/$basedir
            ;;
        esac
	    ;;
	esac
    done

    return 0
}
    
# should probably merge files_rename script into this.

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

exit $exitCode
