#!/bin/sh
# *******************************************************************
# **  @file eisman_upgrade.sh - upgrade all stable packages
# **
# **  Creation:     2016-05-27 dv
# **  Last update:  $Id: eisman_upgrade.sh 42775 2016-08-03 19:57:26Z dv $
# **
# **  Copyright (c) 2013-2016 the eisfair team, team(at)eisfair(dot)org
# **
# **  This program is free software; you can redistribute it and/or modify
# **  it under the terms of the GNU General Public License as published by
# **  the Free Software Foundation; either version 2 of the License, or
# **  (at your option) any later version.
# *******************************************************************

. /var/install/include/eislib
. /var/install/include/eismanlib


NUM_INSTALLED="0"
NUM_AVAILABLE="0"

# *******************************************************************
# **  show help on arguments and possible options
# **  @param  n/a
# **  @return n/a
# *******************************************************************

show_help()
{
cat <<EOF

Update packages of a system

usage: eisman upgrade [OPTIONS]

options:
   --listonly      only produce output like eisman queury does
   --no-wait-anim  do not show wait animation
   --help          show this help screen"

EOF
}


# *******************************************************************
# **  query a single package with a specific name and version
# **  from the package db
# **  @param  $1  name of package
# **  @param  $2  version of package
# **  @param  $3  state of package (either 's' or '.*' for any)
# **  @return n/a
# *******************************************************************

query_single_package()
{
    local fields="name |short |status |version |url |------"
    local package=${1}
    local version=${2}
    local state=${3}

    grep -E "^\[.*\]\[${package}\]\[${version}\]\[${state}\]\[.*\](${fields})" \
         "${EISMAN_PATH}/packages.db" |
    cut  -d ']' -f 6
}


# *******************************************************************
# **  main routine
# **  @param  $@ list of command line arguments
# **  @return n/a
# *******************************************************************

listonly="no"
unstable="no"
wait_animation="yes"
arguments=""

main()
{
    while [ "${1:0:2}" = "--" ]
    do
        case $1 in
        --help)
            show_help
            return 0
            ;;
        --listonly)
            shift
            listonly="yes"
            ;;
        --unstable)
            shift
            unstable="yes"
            arguments="${arguments} --unstable"
            ;;
        --no-wait-anim)
            wait_animation="no"
            arguments="${arguments} --no-wait-anim"
            shift
            ;;
        *)
            echo "error: unknown option \"$1\"!" >&2
            echo ""
            show_help
            return 1
            ;;
        esac
    done

    # start query of upgradable packages
    if [ "${unstable}" = "yes" ]
    then
        ${EISMAN_BIN_PATH}/eisman_find_upgradables.sh --unstable \
            > "/tmp/tmpresult$$" &
    else
        ${EISMAN_BIN_PATH}/eisman_find_upgradables.sh \
            > "/tmp/tmpresult$$" &
    fi

    # show wait animation and fetch exit code
    if [ "${wait_animation}" = "yes" ]
    then
        show_wait "searching..."
    fi
    wait $!

    # if exit code signals failure, abort script
    if [ "$?" != 0 ]
    then
        echo 'Search for packages aborted!' >&2
        rm -f "/tmp/tmpresult$$"
        exit 1
    fi

    # read result and remove temporary file
    packages="$(cat /tmp/tmpresult$$)"
    rm -f "/tmp/tmpresult$$"

    # process results
    if [ "${listonly}" = "no" ]
    then
        if [ -z "${packages}" ]
        then
            echo '-------------------------------------'
            echo 'Your system is up to date!'
            echo '-------------------------------------'
            return 0
        else
            ${EISMAN_BIN_PATH}/eisman_install.sh ${arguments} "${packages}"
            return $?
        fi
    else
        if [ -n "${packages}" ]
        then
            OLDIFS=IFS
            IFS=" "
            for update in ${packages}
            do
                local package="$(echo ${update} | cut -d '=' -f 1)"
                local version="$(echo ${update} | cut -d '=' -f 2)"
                if [ "${unstable}" = "yes" ]
                then
                    query_single_package "${package}" "${version}" ".*"
                else
                    query_single_package "${package}" "${version}" "s"
                fi
            done
            IFS=${OLDIFS}
        fi
    fi

    exit $?
}

main "${@}"
