###############################################################################
#
#   Copyright (c) 2006 Zenoss, Inc. all rights reserved.
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License.
#
###############################################################################

#
# functions that are shared (referenced) by both the build-functions
# and install-functions.  this is logic that we don't want to link to
# installation or building because it is actually common to both of them
#

# reset our stty to be sane
export LD_LIBRARY_PATH=$ZENHOME/lib

cleanup()
{
    stty sane 2>/dev/null
}

# print the error message provided and then exit with a 1 return code
fail()
{
    echo $*
    exit 1
}

# disable echo
noecho()
{
    stty -echo 2>/dev/null
}

# enable echo
echook()
{
    echo
    stty echo 2>/dev/null
}

# replace SEARCH with REPLACE in $FILE using sed
replace() {
    SEARCH=$1
    REPLACE=$2
    FILE=$3
    TEMP=/tmp/`basename $FILE`

    sed -e "s%${SEARCH}%${REPLACE}%g" < ${FILE} > ${TEMP}
    mv ${TEMP} ${FILE}
}

# tests to see if VARIABLE is set in FILE.  if it is set don't do
# anything.  if it is not set then set it to the VALUE provided.
append() {
    VARIABLE=$1
    VALUE=$2
    FILE=$3

    TESTVALUE=`grep ${VARIABLE} ${FILE}`
    if [ -z "${TESTVALUE}" ]; then
        echo "export $VARIABLE=\"${VALUE}\"" >> ${FILE}
    fi
}

shebang() {
   # replace the first line of any python sh-bang script with
   # #!/usr/bin/env python
   ls -1 $ZENHOME/bin/* | grep -v zensocket | xargs \
   sed -i '1,1 s%#!.*python$%#!/usr/bin/env python%' 
}
