#  $Id: util.sh,v 1.5 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:
#  Author: 
#
#  upgrade utility functions
#

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
	_privusrgrp=`cat $instdir/var/serverconfig/global/array.properties 2>/dev/null | awk -F= \
	    'BEGIN { u="ttasys"; g="ttaserv" }
	    $1 == "tarantella.config.array.privilegeduser"  { u = $2 }
	    $1 == "tarantella.config.array.privilegedgroup" { g = $2 }
	    END { print u " " g }'`
	_privusr=`expr "$_privusrgrp" : '\(.*\) .*'`
	_privgrp=`expr "$_privusrgrp" : '.* \(.*\)'`
        chmod 600 "$propfile"; chgrp $_privgrp "$propfile"; chown $_privusr "$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
}

append_property ()
{
    # $1=property_file $2=property $3=new_value
    # only append to the property if it was 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
	    sed "/$prop=/s/\$/,$val/" "$propfile" >${propfile}.new
	    if [ `wc -l <${propfile}.new` -eq `wc -l <${propfile}` ]; then
		cp ${propfile}.new ${propfile} && rm -f ${propfile}.new 2>/dev/null
		echo "Appended $prop=$val to $propfile"
	    fi
        else
            # it's not already there, don't do anything
            :
        fi
    fi
}

remove_property ()
{
    # $1=property_file $2=property
    propfile="$instdir/var/serverconfig/$1"
    prop="$2"
    if [ ! -f "$propfile" ]; then
        echo "$l_missing $propfile"
    else
        grep -v "$prop=" "$propfile" >${propfile}.new
	if [ `wc -c <${propfile}.new` -lt `wc -c <${propfile}` ]; then
	    cp ${propfile}.new ${propfile} && rm -f ${propfile}.new 2>/dev/null
            echo "Removed $prop from $propfile"
        fi
    fi
}

install_file_unconditionally ()
{
    # $1=property_file
    while [ -n "$1" ]; do
	template="$instdir/etc/templates/serverresources/serverconfig/$1"
	propfile="$instdir/var/serverconfig/$1"
	cp "$template" "$propfile" 
	shift
    done
}

delete_file_unconditionally ()
{
    # $1=property_file
    while [ -n "$1" ]; do
	propfile="$instdir/var/serverconfig/$1"
	rm -f "$propfile" >/dev/null 2>&1
	shift
    done
}

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

get_value() {
  # get_value attribute
  # returns the value of the attribute

  case "$#" in
  1)	currentval=`trace="" $INSTALLDIR/bin/tarantella config list $1 2>/dev/null`
	echo "$currentval" | sed 's/.*: \(.*\)/\1/'
	;;
  2)	propfile="$instdir/var/serverconfig/$1"
	prop="$2"
        sed "/^${prop}=/!d;s/${prop}=//" "$propfile"
	;;
  *) return
  esac
}

change_all() {
  # change_all regexp value
  # searches config for matches to regexp and replaces them
  # with value.
  for file in "$INSTALLDIR"/var/serverconfig/*/*.properties; do
    sed "s/$1/$2/g" < $file > $file.new
    [ `wc -l <"$file"` -eq `wc -l <"$file.new"` ] && cp $file.new $file && rm -f $file.new 2>/dev/null
  done
}

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="" $INSTALLDIR/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
}

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
