#!/bin/sh
#  $Id: UpgradeFrom_330000.sh,v 1.4 2005/10/05 14:00:49 billwe Exp $
#  Copyright 2005 Sun Microsystems, Inc. All rights reserved.
#  This software code contains proprietary information 
#  and should be treated as confidential.
#
#  Component: Installation/upgrade
#  Author: Bill Weir

FROM_VERSION=330901 TO_VERSION=330923

usage()
{
    echo "Usage: $1 <oldversion> <newversion>" >&2
    echo 1
}

case "$#" in
1)  [ "$1" = "--target" ] && { echo "$TO_VERSION"; exit 0; }
    exit `usage "$0"`
    ;;
2)  [ "$2" -le "$1" -o "$1" -lt "$FROM_VERSION" -o "$1" -ge "$TO_VERSION" -o "$2" -lt "$TO_VERSION" ] && exit 2	# no action required
    ;;
*)  exit `usage "$0"`
esac

change_property ()
{
    # $1=property_file $2=property $3=new_value
    propfile="$instdir/var/serverconfig/$1"
    prop="$2"
    val="$3"
    if [ ! -f "$propfile" ]; then
        echo "$l_missing $propfile"
    else
        sed "s;^\(${prop}=\).*;\1${val};" "$propfile" > "$propfile.$$"
        [ `wc -l <"$propfile"` -eq `wc -l <"$propfile.$$"` ] && cp "$propfile.$$" "$propfile" && rm -f "$propfile.$$" 2>/dev/null
        chmod 600 "$propfile"; chgrp bin "$propfile"; chown bin "$propfile"
        echo "Modified $prop to $val in $propfile"
    fi
}

add_property ()
{
    # $1=property_file $2=property $3=new_value
    # only add the property if it was not already present
    propfile="$instdir/var/serverconfig/$1"
    prop="$2"
    val="$3"
    if [ ! -f "$propfile" ]; then
        echo "$l_missing $propfile"
    else
        if grep "$prop=" "$propfile" >/dev/null 2>&1; then
            # it's already there, don't do anything
            :
        else
            # add it to the end of the file
            echo "$prop=$val" >> "$propfile"
            echo "Added $prop=$val to $propfile"
        fi
    fi
}

the_edit=""

do_edit() {
case $SCOARCH in 
    i3sc* )
        [ -z "$1" ] && return
        $instdir/bin/tarantella config edit "$@" 2>&1 | tee -a $LOGFILE
    ;;
    * )
        if [ -z "$1" ]; then
            (eval $the_edit) | $instdir/bin/tarantella config edit --file - 2>&1 | tee -a $LOGFILE
        else
            the_edit="echo \"$@\"; $the_edit"
        fi
    ;;
esac
}

edit_if_value_is() {
  # edit_if_value_is attribute_name default_val new_val
  # Change attribute value of attribute "attribute_name" to "new_val" if it's
  # current value is "default_val". 
  # returns 0 if no changes were made or 1 if the attribute was changed

  changed=0

  [ $# -ne 3 ] && return
  T=$trace; trace=
  currentval=`trace="" $instdir/bin/tarantella config list $1 2>/dev/null` 
  trace=$T

  currentval=`echo "$currentval" | sed 's/.*: \(.*\)/\1/'`

  if [ "$2" = "$currentval" ]; then
    do_edit "$1 $3"
    changed=1 
  fi

  return $changed
}

T=$trace; trace=""
key="--tarantella-config-scottaapplication-scottascriptvars.scottaapplication"
scottaapplication=`trace="" ${instdir}/bin/tarantella config list $key 2>/dev/null | sed -e 's;.*: ";;' -e 's;"$;;'`
trace=$T

[ `echo "$scottaapplication" | grep TTA_DeviceMapping= 1>/dev/null 2>&1; echo $?` -ne 0 ] && {
    scottaapplication="$scottaapplication,TTA_DeviceMapping=scottadevicemapping"
}
do_edit "$key \\\"$scottaapplication\\\""


## Final do_edit to commit all changes. this must be after all other calls
## to do_edit, so leaving it last is safest
do_edit

exit 0
