epm-play 8.93 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 27 28 29
__check_installed_app()
{
	[ -s $epm_vardir/installed-app ] || return 1
	grep -q -- "^$1\$" $epm_vardir/installed-app
}

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

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

47 48 49 50 51 52 53

__is_app_installed()
{
	__run_script "$1" --installed "$2"
	return
}

54
__run_script()
55
{
56 57
	local script="$psdir/$1.sh"
	[ -x "$script" ] || return
58 59
	shift
	$script "$@"
60
	return
61
}
62

63 64 65
__get_app_package()
{
	__run_script "$1" --package-name "$2" 2>/dev/null
66 67
}

68

69

70 71 72 73 74 75 76 77 78 79
__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
}

80 81 82 83 84 85 86 87 88 89
__list_all_packages()
{
    local name
    for name in $(__list_all_app) ; do
        __get_app_package $name
    done
}

# pkg app
__list_app_packages_table()
90
{
91 92
    local name
    for name in $(__list_all_app) ; do
93 94 95
        local pkg="$(__get_app_package $name)"
        [ -n "$pkg" ] || continue
        echo "$pkg $name"
96
    done
97 98 99 100 101 102 103 104 105 106 107 108
}

__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
109
    return
110

111 112 113
    cat $epm_vardir/installed-app 2>/dev/null
}

114 115 116 117 118 119 120 121 122 123 124 125
__list_installed_packages()
{
    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 | cut -f1 -d" "
    done
    rm -f $tapt
}

126

127
# args: script, host arch
128 129
__get_app_description()
{
130
    __run_script "$1" --description "$2" 2>/dev/null
131
}
132

133
__check_play_script()
134 135 136 137
{
    local script="$psdir/$1.sh"
    shift

138 139 140 141 142 143 144 145
    [ -x "$script" ]
}


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

147 148
    # TODO: use epm print info instead of one?
    # we will have DISTRVENDOR there
149 150
    export PATH=$PROGDIR:$PATH

151
    set_sudo
152 153
    export SUDO

154 155
    [ -n "$non_interactive" ] && export EPM_AUTO="--auto"

156 157
    local bashopt=''
    [ -n "$verbose" ] && bashopt='-x' && export EPM_VERBOSE="$verbose"
158
    #info "Running $($script --description 2>/dev/null) ..."
159
    docmd bash $bashopt $script "$@"
160 161
}

162 163 164 165 166 167 168 169 170 171
__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:"
172 173
    for i in $(__list_installed_app) ; do
        local desc="$(__get_app_description $i)"
174
        #[ -n "$desc" ] || continue
175 176 177 178 179 180 181 182 183
        [ -n "$quiet" ] || echo -n "  "
        printf "%-20s - %s\n" "$i" "$desc"
    done
}


__epm_play_list()
{
    local psdir="$1"
184
    local extra="$2"
185 186
    local i
    local IGNOREi586
187 188
    local arch="$($DISTRVENDOR -a)"
    [ "$arch" = "x86_64" ] && IGNOREi586='' || IGNOREi586=1
189 190

    if [ -n "$short" ] ; then
191
        for i in $(__list_all_app) ; do
192
            local desc="$(__get_app_description $i $arch)"
193
            [ -n "$desc" ] || continue
194
            echo "$i"
195 196 197 198 199
            if [ -n "$extra" ] ; then
                for j in $(__run_script "$i" "--product-alternatives") ; do
                    echo "  $i $j"
                done
            fi
200 201 202
        done
        exit
    fi
203

204
    for i in $(__list_all_app) ; do
205
        local desc="$(__get_app_description $i $arch)"
206 207
        [ -n "$desc" ] || continue
        [ -n "$quiet" ] || echo -n "  "
208
        printf "%-20s - %s\n" "$i" "$desc"
209 210 211 212 213
        if [ -n "$extra" ] ; then
            for j in $(__run_script "$i" "--product-alternatives") ; do
                printf "  %-20s - %s\n" "$i $j" "$desc"
            done
        fi
214 215 216 217
    done
}


218 219 220 221 222 223 224 225 226 227
__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
228
    --list-scripts        - list all available scripts
229 230 231 232 233
    --short (with --list) - list names only"
    --installed <app>     - check if the app is installed"
EOF
}

234 235
epm_play()
{
236
local psdir="$(realpath $CONFIGDIR/play.d)"
237
local prsdir="$(realpath $CONFIGDIR/prescription.d)"
238 239

if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
240
    __epm_play_help
241 242 243
    exit
fi

244

245
if [ "$1" = "--remove" ] || [ "$1" = "remove" ]  ; then
246
    shift
247 248
    prescription="$1"
    shift
249 250 251 252 253 254 255 256
    if __check_play_script "$prescription" ; then
        __epm_play_run $prescription --remove "$@"
        __remove_installed_app "$prescription"
    else
        psdir=$prsdir
        __check_play_script "$prescription" || fatal "We have no idea how to remove $prescription (checked in $psdir and $prsdir)"
        __epm_play_run "$prescription" --remove "$@" || fatal "There was some error during run the script."
    fi
257 258 259
    exit
fi

260

261 262
if [ "$1" = "--update" ] ; then
    shift
263 264
    local CMDUPDATE="--update"
    [ -n "$force" ] && CMDUPDATE="--run"
265 266
    if [ "$1" = "all" ] ; then
        shift
267
        RES=0
268
        for i in $(__list_installed_app) ; do
269
            echo
270 271
            echo "$i"
            prescription="$i"
272 273
            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."
274
                RES=1
275 276
                continue
            fi
277
            __epm_play_run $prescription $CMDUPDATE "$@" || RES=$?
278
        done
279
        exit $RES
280
    fi
281
    if [ -z "$1" ] ; then
282
        fatal "run --update with 'all' or a project name"
283
    fi
284
    __is_app_installed "$1" || fatal "$1 is not installed"
285 286
    prescription="$1"
    shift
287
    __epm_play_run $prescription $CMDUPDATE "$@"
288 289 290
    exit
fi

291
if [ "$1" = "--installed" ] || [ "$1" = "installed" ]  ; then
292
    shift
293
    __is_app_installed "$1" "$2"
294
    #[ -n "$quiet" ] && exit
295 296
    exit
fi
297

298
case "$1" in
299
    # internal options
300 301 302 303
    "--installed-version"|"--package-name"|"--product-alternatives")
        __run_script "$2" "$1" "$3"
        exit
        ;;
304 305 306 307 308 309 310 311 312
    "--list-installed-packages")
        __list_installed_packages
        exit
        ;;
    "--list"|"--list-installed"|"list"|"list-installed")
        __epm_play_list_installed
        exit
        ;;
    # internal options
313 314 315 316 317
    "--help"|"help")
        __run_script "$2" "$1" "$3"
        exit
        ;;
esac
318

319 320 321 322 323 324
if [ "$1" = "--full-list-all" ] ; then
    [ -n "$short" ] || [ -n "$quiet" ] || echo "Available applications (for current arch $($DISTRVENDOR -a)):"
    __epm_play_list $psdir extra
    exit
fi

325
if [ "$1" = "--list-all" ] || [ "$1" = "list-all" ] || [ -z "$*" ] ; then
326
    [ -n "$short" ] || [ -n "$quiet" ] || echo "Available applications (for current arch $($DISTRVENDOR -a)):"
327
    __epm_play_list $psdir
328
    [ -n "$quiet" ] || [ -n "$*" ] && exit
329
    echo
330 331
    #echo "Run epm play --help for help"
    __epm_play_help
332 333 334
    exit
fi

335
if [ "$1" = "--list-scripts" ] || [ "$1" = "list-scripts" ] ; then
336 337 338 339 340
    [ -n "$short" ] || [ -n "$quiet" ] || echo "Run with a name of a play script to run:"
    __epm_play_list $prsdir
    exit
fi

341 342 343
prescription="$1"
shift

344 345 346 347
load_helper epm-check_updated_repo

update_repo_if_needed

348
if __check_play_script "$prescription" ; then
349
    #__is_app_installed "$prescription" && info "$$prescription is already installed (use --remove to remove)" && exit 1
350 351
    __epm_play_run "$prescription" --run "$@" && __save_installed_app "$prescription" || fatal "There was some error during install the application."
else
352
    opsdir=$psdir
353
    psdir=$prsdir
354
    __check_play_script "$prescription" || fatal "We have no idea how to play $prescription (checked in $opsdir and $prsdir)"
355 356
    __epm_play_run "$prescription" --run "$@" || fatal "There was some error during run the script."
fi
357
}