##
##  @l_prefix@/etc/rc.func -- Run-Command Helper Functions
##  Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#
#   display an error message.
#
#   Usage:   opErr <message>
#   Example: opErr "invalid command line"
#
opErr () {
    echo "OpenPKG:ERROR: $*" 1>&2
    exit 1
}

#
#   display a warning message.
#
#   Usage:   opWarn <message>
#   Example: opWarn "unknown variable"
#
opWarn () {
    echo "OpenPKG:WARNING: $*" 1>&2
}

#
#   append (or optionally prepend) one or more directories (optionally
#   have to be existing) to a colon-separated path variable. In case a
#   directory already exists, it is first removed.
#
#   Usage:   opPathAdd [-p] [-e] <variable> <dir> [<dir> ...]
#   Example: opPathAdd -e PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
#
opPathAdd () {
    _prepend=0
    _exists=0
    while [ $# -gt 0 ]; do
        case $1 in
            -p ) _prepend=1; shift ;;
            -e ) _exists=1;  shift ;;
            *  ) break ;;
        esac
    done
    _var="$1"
    shift
    _edit_del=""
    _edit_add=""
    for _dir in "$@"; do
        if [ ".${_exists}" = .1 ] && [ ! -d "${_dir}" ]; then
            continue
        fi
        _edit_del="${_edit_del} -e 's;^${_dir}\$;;' -e 's;^${_dir}:;;'"
        _edit_del="${_edit_del} -e 's;:${_dir}:;:;' -e 's;:${_dir}\$;;'"
        if [ ".${_prepend}" = .0 ]; then
            _edit_add="${_edit_add} -e 's;\$;:${_dir};'"
        else
            _edit_add="-e 's;^;${_dir}:;' ${_edit_add}"
        fi
    done
    if [ ".${_edit_del}${_edit_add}" != . ]; then
        eval "${_var}=\`echo \"\$${_var}\" | sed ${_edit_del} ${_edit_add}\`"
    fi
    unset _prepend _exists _var _edit_del _edit_add _dir
}

#
#   remove one or more directories from a colon-separated path variable
#
#   Usage:   opPathDel <variable> <dir> [<dir> ...]
#   Example: opPathDel PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
#
opPathDel () {
    _var="$1"
    shift
    _edit=""
    for _dir in "$@"; do
        _edit="${_edit} -e 's;^${_dir}\$;;' -e 's;^${_dir}:;;'"
        _edit="${_edit} -e 's;:${_dir}:;:;' -e 's;:${_dir}\$;;'"
    done
    eval "${_var}=\`echo \"\$${_var}\" | sed ${_edit}\`"
    unset _var _edit _dir
}

#
#   check whether a variable contains a positive "Yes" value.
#
#   Usage:   opVarIsYes <variable>
#   Example: if opVarIsYes foo; then ...
#
opVarIsYes () {
    _var="${1}"
    eval "_val=\"\$${_var}\""
    case "${_val}" in
        [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 ) 
            unset _var _val
            return 0
            ;;
        [Nn][Oo] | [Ff][Aa][Ll][Ss][Ee] | [Oo][Ff][Ff] | 0 )
            unset _var _val
            return 1
            ;;
        *)
            opWarn "variable \$${_var} is not set properly."
            unset _var _val
            return 1
            ;;
    esac
}

#
#   check whether a service is enabled.
#
#   Usage:   opServiceEnabled <variable>
#   Example: if opServiceEnabled openssh; then ...
#
opServiceEnabled () {
    opVarIsYes ${1}_enable
}

#
#   generate temporary file directory
#
#   Usage:   opTmpDirGen <program>
#   Example: opTmpDirGen openssh
#
opTmpDirGen () {
    _tmpdir="@l_prefix@/RPM/TMP/${1}"
    rm -rf ${_tmpdir} >/dev/null 2>&1 || true
    mkdir ${_tmpdir} >/dev/null 2>&1 || true
    chmod 700 ${_tmpdir} >/dev/null 2>&1 || true
}

#
#   set filename of temporary file
#
#   Usage:   opTmpDirFile <program> <filename> <variable>
#   Example: opTmpDirFile openssh 0 tmpfile
#
opTmpDirFile () {
    _tmpdir="@l_prefix@/RPM/TMP/${1}"
    eval "${3}=\"${_tmpdir}/${2}\""
    touch "${_tmpdir}/${2}" >/dev/null 2>&1 || true
    chmod 700 "${_tmpdir}/${2}" >/dev/null 2>&1 || true
}

#
#   delete temporary file directory
#
#   Usage:   opTmpDirDel <program>
#   Example: opTmpDirDel openssh
#
opTmpDirDel () {
    _tmpdir="@l_prefix@/RPM/TMP/${1}"
    rm -rf ${_tmpdir} >/dev/null 2>&1 || true
}

