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

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

__remove_installed_app()
{
	[ -d "$epm_vardir" ] || return 0
	local i
	for i in $* ; do
35
		sudorun sed -i "/^$i$/d" $epm_vardir/installed-app
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	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
}


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

    if [ ! -x "$script" ] ; then
        fatal "Can't find play script $script."
    fi

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

    #info "Running $($script --description 2>/dev/null) ..."
67
    docmd $script "$@"
68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

epm_play()
{
# TODO: change to play.d
local psdir="$CONFIGDIR/prescription.d"

if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
    cat <<EOF
Options:
    APP            - install APP
    --remove APP   - remove APP
    --list         - list all installed apps
    --list-all     - list all available apps
82
    --short        - print only names
83 84 85 86 87 88
EOF
    exit
fi

if [ "$1" = "--remove" ] ; then
    shift
89
    #__check_installed_app "$1" || warning "$1 is not installed"
90 91 92
    prescription="$1"
    shift
    __epm_play_run $prescription --remove "$@"
93
    __remove_installed_app "$prescription"
94 95 96 97 98 99
    exit
fi

if [ "$1" = "--list" ] || [ "$1" = "--installed" ] ; then
    shift
    local i
100 101 102 103 104 105 106
    if [ -n "$short" ] ; then
        for i in $(__list_installed_app) ; do
            echo "$i"
        done
        exit
    fi
    echo "Installed:"
107 108 109 110 111 112
    for i in $(__list_installed_app) ; do
        printf "  %-20s - %s\n" "$i" "$($psdir/$i.sh --description 2>/dev/null)"
    done
    exit
fi

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
115
if [ "$1" = "--list-all" ] || [ -z "$*" ] ; then
116
    echo "Run with a name of a play script to run:"
117 118
    local i
    local desc
119 120 121 122 123
    if [ -n "$short" ] ; then
        for i in $psdir/*.sh ; do
            # print all
            #desc="$($i --description 2>/dev/null)"
            #[ -z "$desc" ] && continue
124 125 126
            local name=$(basename $i .sh)
            [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
            echo "$name"
127 128 129
        done
        exit
    fi
130
    for i in $psdir/*.sh ; do
131 132
        desc="$($i --description 2>/dev/null)"
        [ -z "$desc" ] && continue
133 134 135
        local name=$(basename $i .sh)
        [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
        printf "  %-20s - %s\n" "$name" "$desc"
136 137 138 139 140 141
    done
    echo
    echo "run epm play --list to list installed only or --remove to remove one"
    exit
fi

142 143 144 145 146
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."
147
}