epm-play 5.38 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2015, 2017, 2019, 2020, 2022  Etersoft
# Copyright (C) 2015, 2017, 2019, 2020, 2022  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# 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/>.
#

# TODO
epm_vardir=/var/lib/eepm

23

24 25 26
__save_installed_app()
{
	[ -d "$epm_vardir" ] || return 0
27
	__check_installed_app "$1" && return 0
28
	echo "$1" | sudorun tee -a $epm_vardir/installed-app >/dev/null
29 30 31 32 33 34 35
}

__remove_installed_app()
{
	[ -d "$epm_vardir" ] || return 0
	local i
	for i in $* ; do
36
		sudorun sed -i "/^$i$/d" $epm_vardir/installed-app
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	done
	return 0
}

__check_installed_app()
{
	[ -s $epm_vardir/installed-app ] || return 1
	grep -q -- "^$1\$" $epm_vardir/installed-app
}

__list_installed_app()
{
    cat $epm_vardir/installed-app 2>/dev/null
}

52 53 54 55 56
__get_app_description()
{
    [ -x "$1" ] || return
    $1 --description 2>/dev/null
}
57

58
__check_play_script()
59 60 61 62
{
    local script="$psdir/$1.sh"
    shift

63 64 65 66 67 68 69 70 71 72
    [ -x "$script" ]
}


__epm_play_run()
{
    local script="$psdir/$1.sh"

    __check_play_script "$1" || fatal "Can't find executable play script $script. Run epm play to list all available apps."
    shift
73 74 75 76 77 78

    # allow use EGET in the scripts
    __set_EGET
    # also we will have DISTRVENDOR there
    export PATH=$PROGDIR:$PATH

79
    set_sudo
80 81
    export SUDO

82 83
    local bashopt=''
    [ -n "$verbose" ] && bashopt='-x' && export EPM_VERBOSE="$verbose"
84
    #info "Running $($script --description 2>/dev/null) ..."
85
    docmd bash $bashopt $script "$@"
86 87
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
__epm_play_help()
{
    cat <<EOF
Usage: epm play [options] [<app>]
Options:
    <app>                 - install <app>
    --remove <app>        - uninstall <app>
    --update [<app>|all]  - update <app> (or all installed apps) if there is new version
    --list                - list all installed apps
    --list-all            - list all available apps
    --short (with --list) - list names only"
    --installed <app>     - check if the app is installed"
EOF
}

103 104
epm_play()
{
105
local psdir="$(realpath $CONFIGDIR/play.d)"
106 107

if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
108
    __epm_play_help
109 110 111
    exit
fi

112

113 114
if [ "$1" = "--remove" ] ; then
    shift
115
    #__check_installed_app "$1" || warning "$1 is not installed"
116 117 118
    prescription="$1"
    shift
    __epm_play_run $prescription --remove "$@"
119
    __remove_installed_app "$prescription"
120 121 122
    exit
fi

123

124 125 126 127 128
if [ "$1" = "--update" ] ; then
    shift
    if [ "$1" = "all" ] ; then
        shift
        for i in $(__list_installed_app) ; do
129
            echo
130 131
            echo "$i"
            prescription="$i"
132 133 134 135
            if ! __check_play_script $prescription ; then
                warning "Can't find executable play script for $prescription. Try epm play --remove $prescription if you don't need it anymore."
                continue
            fi
136 137 138 139
            __epm_play_run $prescription --run "$@"
        done
        exit
    fi
140
    if [ -z "$1" ] ; then
141
        fatal "run --update with 'all' or a project name"
142
    fi
143 144 145 146 147 148 149
    __check_installed_app "$1" || fatal "$1 is not installed"
    prescription="$1"
    shift
    __epm_play_run $prescription --run "$@"
    exit
fi

150 151 152 153 154
if [ "$1" = "--installed" ] ; then
    shift
    __check_installed_app "$1"
    exit
fi
155

156
if [ "$1" = "--list" ] ; then
157 158
    shift
    local i
159 160 161 162 163 164
    if [ -n "$short" ] ; then
        for i in $(__list_installed_app) ; do
            echo "$i"
        done
        exit
    fi
165
    [ -n "$quiet" ] || echo "Installed:"
166
    for i in $(__list_installed_app) ; do
167 168
        local desc="$(__get_app_description $psdir/$i.sh)"
        [ -n "$desc" ] || continue
169 170
        [ -n "$quiet" ] || echo -n "  "
        printf "%-20s - %s\n" "$i" "$desc"
171 172 173 174
    done
    exit
fi

175
[ "$($DISTRVENDOR -a)" = "x86_64" ] && IGNOREi586='' || IGNOREi586=1
176

Vitaly Lipatov's avatar
Vitaly Lipatov committed
177
if [ "$1" = "--list-all" ] || [ -z "$*" ] ; then
178
    local i
179 180
    if [ -n "$short" ] ; then
        for i in $psdir/*.sh ; do
181 182
            local name=$(basename $i .sh)
            [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
183
            rhas "$name" "^common" && continue
184
            echo "$name"
185 186 187
        done
        exit
    fi
188
    [ -n "$quiet" ] || echo "Run with a name of a play script to run:"
189
    for i in $psdir/*.sh ; do
190 191
        local desc="$(__get_app_description $i)"
        [ -n "$desc" ] || continue
192 193
        local name=$(basename $i .sh)
        [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
194
        rhas "$name" "^common" && continue
195 196
        [ -n "$quiet" ] || echo -n "  "
        printf "%-20s - %s\n" "$name" "$desc"
197
    done
198
    [ -n "$quiet" ] || [ -n "$*" ] && exit
199
    echo
200 201
    #echo "Run epm play --help for help"
    __epm_play_help
202 203 204
    exit
fi

205 206 207 208 209
prescription="$1"
shift

#__check_installed_app "$prescription" && info "$$prescription is already installed (use --remove to remove)" && exit 1
__epm_play_run "$prescription" --run "$@" && __save_installed_app "$prescription" || fatal "There was some error during install the application."
210
}