#!/bin/bash
# *******************************************************************
# **  @file eisman_usage.sh - recursively resolve a depending packages
# **
# **  Creation:     2016-06-09 dv
# **  Last update:  $Id: eisman_usage.sh 42826 2016-08-13 19:44:23Z hbfl $
# **
# **  Copyright (c) 2016-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


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

show_help()
{
cat <<EOF

List all packages that depend on a given package

usage: eisman usage [OPTIONS] {PACKAGE}*

options:
   --help      show this help screen

EOF
}


# *******************************************************************
# **  recursively find all packages that state a requirement for the
# **  given package
# **  @param  $1  package
# **  @param  $2  line prefix (level indentation)
# **  @return n/a
# *******************************************************************

find_dependend_packages ()
{
    local package="${1}"
    local prefix="${2}"

    local result

    result=$(grep -E "^\[.*\]\[.*\]\[.*\]\[.*\]\[.*\]require    ${package}([ ]*|$) " \
            "${EISMAN_PATH}/installed.db" |
       cut  -d ']' -f 2                   |
       cut  -d '[' -f 2)

    if [ -n "${result}" ]
    then
        local IFS_BAK=$IFS
        IFS="
"
        local previous=""
        for dependency in ${result}
        do
            if [ -n "${previous}" ]
            then
                echo -n "${prefix}"
                echo -n "  |<-- "
                echo "${previous}"
                find_dependend_packages  "${previous}" "${prefix}  |    "
            fi

            previous="${dependency}"
        done

        if [ -n "${previous}" ]
        then
            echo -n "${prefix}"
            echo -n "  |<-- "
            echo "${previous}"
            find_dependend_packages  "${previous}" "${prefix}       "
        fi

        IFS=$IFS_BAK
    fi
}


# *******************************************************************
# **  print package and start recursive dependency search
# **  @param  $1  package
# **  @return n/a
# *******************************************************************

process_single_package()
{
    local package="$1"

    echo "package: ${package}"

    find_dependend_packages "${package}" "  "
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    while [ "${1:0:2}" = "--" ]
    do
        case $1 in
        --help)
            show_help
            exit 0
            ;;
        *)
            echo "error: unknown option \"$1\"!" >&2
            echo ""
            show_help
            exit 1
            ;;
        esac
    done

    while [ "$#" -gt "0" ]
    do
        name="$(echo ${1} | cut -d '=' -f 1)"

        process_single_package "${name}"
        shift
    done
}

main "${@}"
