epm-assure 2.88 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2013-2016  Etersoft
# Copyright (C) 2013-2016  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

__check_command_in_path()
{
    PATH=$PATH:/sbin:/usr/sbin which "$1" 2>/dev/null
}

25 26 27 28 29
__epm_need_update()
{
    local PACKAGE="$1"
    local PACKAGEVERSION="$2"

30
    [ -n "$PACKAGEVERSION" ] || return 0
31 32 33 34 35 36 37

    load_helper epm-query
    is_installed "$PACKAGE" || return 0

    load_helper epm-print
    # epm print version for package N
    local INSTALLEDVERSION=$(query_package_field "version" "$PACKAGE")
38 39
    # if needed >= installed, return 0
    [ "$(compare_version "$PACKAGEVERSION" "$INSTALLEDVERSION")" -gt 0 ] && return 0
40 41 42 43

    return 1
}

44
__epm_assure_checking()
45
{
46 47 48
    local CMD="$1"
    local PACKAGE="$2"
    local PACKAGEVERSION="$3"
49 50

    [ -n "$PACKAGEVERSION" ] && return 1
51

52 53
    if is_dirpath "$CMD" ; then
        if [ -e "$CMD" ] ; then
54
            if [ -n "$verbose" ] ; then
55 56
                info "File or directory $CMD is already exists."
                epm qf "$CMD"
57
            fi
58
            return 0
59 60
        fi

61
        [ -n "$PACKAGE" ] || fatal "You need run with package name param when use with absolute path"
62
        return 1
63 64
    fi

65
    if __check_command_in_path "$CMD" >/dev/null ; then
66 67
        if [ -n "$verbose" ] ; then
            local compath="$(__check_command_in_path "$1")"
68
            info "Command $CMD is exists: $compath"
69 70
            epm qf "$compath"
        fi
71
        return 0
72 73
    fi

74 75 76 77 78 79 80 81 82
    return 1
}

# Do fast checking for command and install package if the command does not exist

# $1 - command name
# $2 - [package name]
# $3 - [needed package version]

83
epm_assure()
84 85 86 87 88 89 90 91
{
    local CMD="$1"
    local PACKAGE="$2"
    local PACKAGEVERSION="$3"
    [ -n "$PACKAGE" ] || PACKAGE="$1"

    __epm_assure_checking $CMD $PACKAGE $PACKAGEVERSION && return 0

92
    info "Installing appropriate package for $CMD command..."
93 94
    __epm_need_update $PACKAGE $PACKAGEVERSION || return 0

95 96 97 98
    # can't be used in epm ei case
    #docmd epm --auto install $PACKAGE || return
    load_helper epm-install
    non_interactive=1 pkg_names="$PACKAGE" pkg_files='' pkg_urls='' epm_install
99 100 101 102 103

    [ -n "$PACKAGEVERSION" ] || return 0
    # check if we couldn't update and still need update
    __epm_need_update $PACKAGE $PACKAGEVERSION && return 1
    return 0
104
}