epm-play 7.1 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
__save_installed_app()
{
26
	return 0 # stub
27
	[ -d "$epm_vardir" ] || return 0
28
	__check_installed_app "$1" && return 0
29
	echo "$1" | sudorun tee -a $epm_vardir/installed-app >/dev/null
30 31 32 33
}

__remove_installed_app()
{
34
	return 0 # stub
35
	[ -s $epm_vardir/installed-app ] || return 0
36 37
	local i
	for i in $* ; do
38
		sudorun sed -i "/^$i$/d" $epm_vardir/installed-app
39 40 41 42 43 44
	done
	return 0
}

__check_installed_app()
{
45 46 47 48 49
	local script="$psdir/$1.sh"
	[ -x "$script" ] || return
	$script --installed
	return

50 51 52 53
	[ -s $epm_vardir/installed-app ] || return 1
	grep -q -- "^$1\$" $epm_vardir/installed-app
}

54 55 56 57 58 59 60 61 62 63
__list_all_app()
{
    for i in $psdir/*.sh ; do
       local name=$(basename $i .sh)
       [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
       rhas "$name" "^common" && continue
       echo "$name"
    done
}

64 65 66 67 68 69 70 71 72 73
__list_all_packages()
{
    local name
    for name in $(__list_all_app) ; do
        __get_app_package $name
    done
}

# pkg app
__list_app_packages_table()
74
{
75 76
    local name
    for name in $(__list_all_app) ; do
77
        echo "$(__get_app_package $name) $name"
78
    done
79 80 81 82 83 84 85 86 87 88 89 90
}

__list_installed_app()
{
    local i
    local tapt=$(mktemp) || fatal
    __list_app_packages_table >$tapt
    # get all installed packages and convert it to a apps list
    for i in $(epm query --short $(cat $tapt | sed -e 's| .*$||') 2>/dev/null) ; do
        grep "^$i " $tapt | sed -e 's|^.* ||'
    done
    rm -f $tapt
91
    return
92

93 94 95
    cat $epm_vardir/installed-app 2>/dev/null
}

96 97 98 99 100 101 102 103
__get_app_package()
{
    local script="$psdir/$1.sh"
    [ -x "$script" ] || return
    $script --package 2>/dev/null
}


104 105
__get_app_description()
{
106 107 108
    local script="$psdir/$1.sh"
    [ -x "$script" ] || return
    $script --description 2>/dev/null
109
}
110

111
__check_play_script()
112 113 114 115
{
    local script="$psdir/$1.sh"
    shift

116 117 118 119 120 121 122 123
    [ -x "$script" ]
}


__epm_play_run()
{
    local script="$psdir/$1.sh"
    shift
124 125 126 127 128 129

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

130
    set_sudo
131 132
    export SUDO

133 134
    [ -n "$non_interactive" ] && export EPM_AUTO="--auto"

135 136
    local bashopt=''
    [ -n "$verbose" ] && bashopt='-x' && export EPM_VERBOSE="$verbose"
137
    #info "Running $($script --description 2>/dev/null) ..."
138
    docmd bash $bashopt $script "$@"
139 140
}

141 142 143 144 145 146 147 148 149 150
__epm_play_list_installed()
{
    local i
    if [ -n "$short" ] ; then
        for i in $(__list_installed_app) ; do
            echo "$i"
        done
        exit
    fi
    [ -n "$quiet" ] || echo "Installed applications:"
151 152
    for i in $(__list_installed_app) ; do
        local desc="$(__get_app_description $i)"
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        [ -n "$desc" ] || continue
        [ -n "$quiet" ] || echo -n "  "
        printf "%-20s - %s\n" "$i" "$desc"
    done
}


__epm_play_list()
{
    local psdir="$1"
    local i
    local IGNOREi586
    [ "$($DISTRVENDOR -a)" = "x86_64" ] && IGNOREi586='' || IGNOREi586=1

    if [ -n "$short" ] ; then
168 169
        for i in $(__list_all_app) ; do
            echo "$i"
170 171 172
        done
        exit
    fi
173
    for i in $(__list_all_app) ; do
174 175 176
        local desc="$(__get_app_description $i)"
        [ -n "$desc" ] || continue
        [ -n "$quiet" ] || echo -n "  "
177
        printf "%-20s - %s\n" "$i" "$desc"
178 179 180 181
    done
}


182 183 184 185 186 187 188 189 190 191
__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
192
    --list-scripts        - list all available scripts
193 194 195 196 197
    --short (with --list) - list names only"
    --installed <app>     - check if the app is installed"
EOF
}

198 199
epm_play()
{
200
local psdir="$(realpath $CONFIGDIR/play.d)"
201
local prsdir="$(realpath $CONFIGDIR/prescription.d)"
202 203

if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
204
    __epm_play_help
205 206 207
    exit
fi

208

209
if [ "$1" = "--remove" ] || [ "$1" = "remove" ]  ; then
210
    shift
211
    #__check_installed_app "$1" || warning "$1 is not installed"
212 213 214
    prescription="$1"
    shift
    __epm_play_run $prescription --remove "$@"
215
    __remove_installed_app "$prescription"
216 217 218
    exit
fi

219

220 221 222 223
if [ "$1" = "--update" ] ; then
    shift
    if [ "$1" = "all" ] ; then
        shift
224
        RES=0
225
        for i in $(__list_installed_app) ; do
226
            echo
227 228
            echo "$i"
            prescription="$i"
229 230
            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."
231
                RES=1
232 233
                continue
            fi
234
            __epm_play_run $prescription --update "$@" || RES=$?
235
        done
236
        exit $RES
237
    fi
238
    if [ -z "$1" ] ; then
239
        fatal "run --update with 'all' or a project name"
240
    fi
241 242 243
    __check_installed_app "$1" || fatal "$1 is not installed"
    prescription="$1"
    shift
244
    __epm_play_run $prescription --update "$@"
245 246 247
    exit
fi

248
if [ "$1" = "--installed" ] || [ "$1" = "installed" ]  ; then
249 250
    shift
    __check_installed_app "$1"
251
    #[ -n "$quiet" ] && exit
252 253
    exit
fi
254

255
if [ "$1" = "--list" ] || [ "$1" = "--list-installed" ] || [ "$1" = "list" ] || [ "$1" = "list-installed" ]  ; then
256
    __epm_play_list_installed
257 258 259
    exit
fi

260
if [ "$1" = "--list-all" ] || [ "$1" = "list-all" ] || [ -z "$*" ] ; then
261 262
    [ -n "$short" ] || [ -n "$quiet" ] || echo "Available applications:"
    __epm_play_list $psdir
263
    [ -n "$quiet" ] || [ -n "$*" ] && exit
264
    echo
265 266
    #echo "Run epm play --help for help"
    __epm_play_help
267 268 269
    exit
fi

270
if [ "$1" = "--list-scripts" ] || [ "$1" = "list-scripts" ] ; then
271 272 273 274 275
    [ -n "$short" ] || [ -n "$quiet" ] || echo "Run with a name of a play script to run:"
    __epm_play_list $prsdir
    exit
fi

276 277 278
prescription="$1"
shift

279 280 281 282 283 284 285 286
if __check_play_script "$prescription" ; then
    #__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."
else
    psdir=$prsdir
    __check_play_script "$prescription" || fatal "We have no idea how to play $prescription (checked in $psdir and $prsdir)"
    __epm_play_run "$prescription" --run "$@" || fatal "There was some error during run the script."
fi
287
}