epm-assure 2.4 KB
Newer Older
1 2
#!/bin/sh
#
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3 4
# Copyright (C) 2013, 2014, 2015  Etersoft
# Copyright (C) 2013, 2014, 2015  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 25
#
# 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
}


26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# copied from strings
# CHECKME: the same like estrlist has ?
# Note: used egrep! write '[0-9]+(first|two)', not '[0-9]\+...'
rhas()
{
	echo "$1" | egrep -q -- "$2"
}

# copied from strings
is_dirpath()
{
    [ "$1" = "." ] && return $?
    rhas "$1" "/"
}

41 42 43 44 45 46 47
# Do fast checking for command and install package if the command does not exist

# $1 - command name
# $2 - package name

__epm_assure()
{
48 49

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

        [ -n "$2" ] || fatal "You need run with package name param when use with absolute path"

        docmd epm --auto --skip-installed install "$2"
        return
    fi

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

    # TODO: use package name normalization
Vitaly Lipatov's avatar
Vitaly Lipatov committed
74
    info "Installing appropriate package for $1 command..."
75 76 77

    local PACKAGE="$2"
    [ -n "$PACKAGE" ] || PACKAGE="$1"
78

79 80
    local PACKAGEVERSION="$3"
    warning "TODO: check for PACKAGEVERSION is missed"
81

82
    docmd epm --auto --skip-installed install "$PACKAGE"
83 84 85 86 87
}


epm_assure()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
88
    [ -n "$pkg_filenames" ] || fatal "Assure: Missing params. Check $0 --help for info."
89 90

    # use helper func for extract separate params
91
    __epm_assure $(eval echo $quoted_args)
92
}