#!/bin/bash
# *******************************************************************
# **  @file eisman_sections.sh - query package database sections
# **
# **  Creation:     2016-08-14 dv
# **  Last update:  $Id: eisman_sections.sh 44488 2017-01-14 21:39:55Z dv $
# **
# **  Copyright (c) 2016-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



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

show_help()
{
cat <<EOF

Query package database sections

usage:
    eisman sections [OPTIONS]

options:
    --help                      show this help screen
    --unstable                  respect unstable packages too
    --installed                 query installed packages database
    --hidden                    show hidden sections too
    --url           <url>       only search packages from this source

EOF
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    # initialize defaults
    local url_filter=".*"
    local state_filter="s"
    local section_filter="[^.].*"
    local database="${EISMAN_PATH}/packages.db"
    local header=$((${FIELDHEADER} + 1))

    # read option switches
    while [ "${1:0:2}" = "--" ]
    do
        case ${1} in
        --help)
            shift
            show_help
            exit 0
            ;;
        --unstable)
            state_filter=".*"
            shift
            ;;
        --url)
            shift
            url_filter="${1}.*"
            shift
            ;;
        --url=*)
            url_filter="${1:6}.*"
            shift
            ;;
        --installed)
            database="${EISMAN_PATH}/installed.db"
            shift
            ;;
        --hidden)
            section_filter=""
            shift
            ;;
        *)
            echo "error: invalid switch \"$1\"!" >&2
            show_help
            exit 1
            ;;
        esac
    done

    query_package_db      \
        "${section_filter}" \
        ""                  \
        ""                  \
        ""                  \
        "${state_filter}"   \
        "${url_filter}"     \
        "section"           \
        "${database}" |
            cut  -c "${header}-" |
            sort -u

    exit ${?}
}

main "${@}"
