#!/bin/sh
# *******************************************************************
# **  @file eisman_check_version.sh - check installation status
# **
# **  Creation:     2003-07-19  fm
# **  Last update:  $Id: eisman_check_version.sh 44513 2017-01-15 17:45:15Z dv $
# **
# **  Copyright (c) 2013-@@YEARE@@ 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/eismanlib

inst_status="installed"
notinst_status="not-installed"

# setup global database path for testing
if [ -n "${EISMAN_TEST_DB}" ]
then
    EISMAN_PATH="${EISMAN_TEST_DB}"
fi


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

show_help()
{
cat <<EOF

Called without the option --diff, the command checks, if a package is
installed or not. If an optional package version is given, it further
checks, if the version of the installed package is older or newer than
the version of the installed package. The command produces the folloing
output:

The command produces the following output on stdout:
    "new"           - version is newer than the installed package
    "old"           - version is older than the installed package
    "installed"     - version of package is identical (installed)
    "not-installed" - package is not installed

When called with the option "--diff", two versions are compared against
each other using comparison rules that are in some way eisfair specific.
The command produces the following result:
    "new"           - version 1 is newer than version 2
    "old"           - version 1 is older than version 2
    "installed"     - both versions are identical

usage:
    eisman check [OPTIONS] PACKAGE [VERSION]
    eisman check [OPTIONS] --diff VERSION1 VERSION2

options:
    --diff  VERSION1 VERSION2   compare the two versions given on
                                command line
    --help                      show this help screen

PACKAGE
    a unique package name. No wildcards are allowed.

VERSION
    a version in the format: <major>.<minor>.<patch>[svn<no>]

EOF

}


# *******************************************************************
# **  fetch package version from database of installed packages
# **  @param  $1  package name
# **  @return n/a
# *******************************************************************

read_package_version()
{
    local package=${1}
    local result

    result=$(grep -e  "^\[.*\]\[${package}\]\[.*\]\[.*\]\[.*\]version " \
          "${EISMAN_PATH}/installed.db" |
          sed  -e 's#^\[.*\]\[.*\]\[.*\]\[.*\]\[.*\]version   *##g')

    have_package_version="${result}"
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    if [ "${1}" = "--help" ]
    then
        show_help
        exit 0
    fi

    if [ "${1}" = "-diff" ] || [ "${1}" = "--diff" ]
    then
        shift
        v1=${1}
        v2=${2}

        package_status="$(diff_version ${v1} ${v2})"
    else
        if [ "${1}" = "--list" ]
        then
            # this only exists for compatibility and may be removed soon
            shift
        fi

        package_name=${1}

        # special handling of kernel requirements, since it must be trans-
        # lated into it's installed variant
        if [ "${package_name}" = "eiskernel" ]
        then
            package_name=${package_name}-$(uname -r |
                awk -F'-' '{ print tolower($NF) }')
        fi

        check_version=${2}

        read_package_version "${package_name}"
        if [ -n "${have_package_version}" ]
        then
            if [ -z "${check_version}" ]
            then
                package_status="${inst_status}"
            else
                package_status="$(diff_version ${check_version} ${have_package_version})"
            fi
        else
            package_status="${notinst_status}"
        fi
    fi

    echo ${package_status}

    exit 0
}

main "${@}"
