#!/bin/sh
# *******************************************************************
# **  @file eisman_upgrade.sh - upgrade all stable packages
# **
# **  Creation:     2016-05-27 dv
# **  Last update:  $Id: eisman_upgrade.sh 44605 2017-01-19 18:25:32Z hbfl $
# **
# **  Copyright (c) 2013-2017 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:
    --unstable      install unstable packages too
    --no-svn        ignore packages with svn version postfix
    --listonly      only produce output like eisman queury does
    --no-wait-anim  do not show wait animation
    --output=<file> redirect result into a file. This is only valid
                    if --listonly is specified too
    --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|date|version|url|------"
    local package=${1}
    local version=${2}
    local state=${3}

    query_package_db      \
        ""                \
        ""                \
        "${package}"      \
        "${version}"      \
        "${state}"        \
        ""                \
        "${fields}"       \
        "${EISMAN_PATH}/packages.db"
}


# *******************************************************************
# **  list details of found update packages
# **  @param  $1  list of packages
# **  @param  $2  unstable = "yes/no"
# **  @return n/a
# *******************************************************************

list_update_packages()
{
    local packages="${1}"
    local unstable="${2}"

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


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

main()
{
    local unstable="no"
    local listonly="no"
    local outfile=""
    local arguments=""
    local wait_animation="yes"
    local nosvn="no"

    while [ "${1:0:2}" = "--" ]
    do
        case $1 in
        --help)
            show_help
            return 0
            ;;
        --listonly|--info)
            shift
            listonly="yes"
            ;;
        --unstable)
            shift
            unstable="yes"
            arguments="${arguments} --unstable"
            ;;
        --no-svn)
            nosvn="yes"
            shift
            ;;
        --no-wait-anim)
            wait_animation="no"
            arguments="${arguments} --no-wait-anim"
            shift
            ;;
        --output)
            shift
            outfile="${1}"
            shift
            ;;
        --output=*)
            outfile="${1:9}"
            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 'error: 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$$"

    # SVN-Pakete herausfiltern
    if [ "${nosvn}" = "yes" ]
    then
        packages="$(echo "${packages}" |
                    sed 's/ [^ ]*=[0-9.]*svn[0-9-]*//g')"
    fi

    # 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
            if [ -z "${outfile}" ]
            then
                list_update_packages "${packages}" "${unstable}"
            else
                list_update_packages "${packages}" "${unstable}" > "${outfile}"
            fi
        fi
    fi

    exit $?
}

main "${@}"
