#!/bin/sh
#
# Copyright (C) 2015, 2017, 2019, 2020, 2022  Etersoft
# Copyright (C) 2015, 2017, 2019, 2020, 2022  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
	__check_installed_app "$1" && return 0
	echo "$1" | sudorun tee -a $epm_vardir/installed-app >/dev/null
}

__remove_installed_app()
{
	[ -d "$epm_vardir" ] || return 0
	local i
	for i in $* ; do
		sudorun sed -i "/^$i$/d" $epm_vardir/installed-app
	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
}

__get_app_description()
{
    [ -x "$1" ] || return
    $1 --description 2>/dev/null
}

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

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

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

    set_sudo
    export SUDO

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

epm_play()
{
local psdir="$CONFIGDIR/play.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
    --short        - print only names
    --update [APP|all]  - update APP (or all installed apps) if there is new version
EOF
    exit
fi


if [ "$1" = "--remove" ] ; then
    shift
    #__check_installed_app "$1" || warning "$1 is not installed"
    prescription="$1"
    shift
    __epm_play_run $prescription --remove "$@"
    __remove_installed_app "$prescription"
    exit
fi


if [ "$1" = "--update" ] ; then
    shift
    if [ "$1" = "all" ] ; then
        shift
        for i in $(__list_installed_app) ; do
            echo "$i"
            prescription="$i"
            __epm_play_run $prescription --run "$@"
        done
        exit
    fi
    if [ -z "$1" ] ; then
        fatal "run --update with 'all' or a project name"
    fi
    __check_installed_app "$1" || fatal "$1 is not installed"
    prescription="$1"
    shift
    __epm_play_run $prescription --run "$@"
    exit
fi


if [ "$1" = "--list" ] || [ "$1" = "--installed" ] ; then
    shift
    local i
    if [ -n "$short" ] ; then
        for i in $(__list_installed_app) ; do
            echo "$i"
        done
        exit
    fi
    echo "Installed:"
    for i in $(__list_installed_app) ; do
        local desc="$(__get_app_description $psdir/$i.sh)"
        [ -n "$desc" ] || continue
        printf "  %-20s - %s\n" "$i" "$desc"
    done
    exit
fi

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

if [ "$1" = "--list-all" ] || [ -z "$*" ] ; then
    local i
    if [ -n "$short" ] ; then
        for i in $psdir/*.sh ; do
            local name=$(basename $i .sh)
            [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
            rhas "$name" "^common" && continue
            echo "$name"
        done
        exit
    fi
    echo "Run with a name of a play script to run:"
    for i in $psdir/*.sh ; do
        local desc="$(__get_app_description $i)"
        [ -n "$desc" ] || continue
        local name=$(basename $i .sh)
        [ -n "$IGNOREi586" ] && rhas "$name" "^i586-" && continue
        rhas "$name" "^common" && continue
        printf "  %-20s - %s\n" "$name" "$desc"
    done
    echo
    echo "run epm play"
    echo "             --list                - to list installed only"
    echo "             --list-all (default)  - to list all app"
    echo "             --remove <app>        - to remove app"
    echo "             --update <app>|all    - to update app or all installed apps"
    echo "             --short (with --list) - list names only"
    exit
fi

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."
}