#!/bin/bash
# *******************************************************************
# **  @file eisman_query.sh - query package database
# **
# **  Creation:     2013-01-13 dv
# **  Last update:  $Id: eisman_query.sh 44488 2017-01-14 21:39:55Z 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/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

usage:
    eisman query [OPTIONS] [PACKAGE] [VERSION]

options:
    --help                      show this help screen
    --excl-sections <sec-list>  comma separated list of sections
    --sections      <sec-list>  comma separated list of sections
    --fields        <fld-list>  comma separated list of fields
    --unstable                  show unstable packages too
    --installed                 query installed packages database
    --outdated                  query outdated packages database
    --output=<file>             redirect result into a file
    --url           <url>       only search packages from this source

possible values for fields:
    name     package name
    version  package version
    short    short package description
    date     date of creation
    author   author of the package
    section  section of the package
    space    disk space needed in MB
    url      URL of the package
    status   status of the package
    require  required packages (multiple results possible)
    link     linked packages (that need update if installed)
    text     descriptive text (multiple results possible)

PACKAGE:
    a package name that may contain wildcards like
    * -> matches multiple characters
    ? -> matches exactly one character
    example:  *sql  matches postgresql, mysql,...
              ??sql matches mysql, ...

VERSION:
    a version string that may contain wildcards too

EOF
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    # initialize defaults
    local sections="[^.].*"
    local exclsections=""
    local fields="name|short|status|version|url|date|------"
    local package=""
    local version=""
    local state="s"
    local filterurl=""
    local database="${EISMAN_PATH}/packages.db"
    local outfile=""

    # read option switches
    while [ "${1:0:2}" = "--" ]
    do
        case ${1} in
        --help)
            shift
            show_help
            exit 0
            ;;
        --sections)
            shift
            if [ "${1}" != "" -a "${1:0:2}" != "--" ]
            then
                sections="${1}"
                sections="${sections//,/|}"
                shift
            else
                echo 'error: missing argument for --sections!' >&2
                show_help
                exit 1
            fi
            ;;
        --sections=*)
            sections="${1:11}"
            sections="${sections//,/|}"
            shift
            ;;
        --excl-sections)
            shift
            if [ "${1}" != "" -a "${1:0:2}" != "--" ]
            then
                exclsections="${1}"
                exclsections="${exclsections//,/|}"
                shift
            else
                echo 'error: missing argument for --excl-sections!' >&2
                show_help
                exit 1
            fi
            ;;
        --excl-sections=*)
            exclsections="${1:16}"
            exclsections="${exclsections//,/|}"
            shift
            ;;
        --fields)
            shift
            if [ "${1}" != "" -a "${1:0:2}" != "--" ]
            then
                fields="${1}"
                fields="${fields//,/|}|------"
                shift
            else
                echo 'error: missing argument for --fields!' >&2
                show_help
                exit 1
            fi
            ;;
        --fields=*)
            fields="${1:9}"
            fields="${fields//,/|}|------"
            shift
            ;;
        --unstable)
            state=".*"
            shift
            ;;
        --url)
            shift
            filterurl="${1}.*"
            shift
            ;;
        --url=*)
            filterurl="${1:6}.*"
            shift
            ;;
        --output)
            shift
            outfile="${1}"
            shift
            ;;
        --output=*)
            outfile="${1:9}"
            shift
            ;;
        --installed)
            database="${EISMAN_PATH}/installed.db"
            shift
            ;;
        --outdated)
            database="${EISMAN_PATH}/outdated.db"
            shift
            ;;
        *)
            echo "error: invalid switch \"$1\"!" >&2
            show_help
            exit 1
            ;;
        esac
    done

    if [ -n "${1}" ]
    then
        package="${1}"
        package="${package//\*/.*}"
        package="${package//\?/.}"
        package="${package//,/|}"
        shift
    fi

    if [ -n "${1}" ]
    then
        version="${1}"
        version="${version//\*/.*}"
        version="${version//\?/.}"
        shift
    fi

    if [ -z "${outfile}" ]
    then
       query_package_db      \
        "${sections}"     \
        "${exclsections}" \
        "${package}"      \
        "${version}"      \
        "${state}"        \
        "${filterurl}"    \
        "${fields}"       \
        "${database}" |
            sed -e ":a;N;\$!ba;s/\\n/||/g"  \
                -e "s/||${EISMAN_SEPARATOR}||/||${EISMAN_SEPARATOR}\\n/g" |
            sort                                        |
            sed 's/||/\n/g'
    else
       query_package_db      \
        "${sections}"     \
        "${exclsections}" \
        "${package}"      \
        "${version}"      \
        "${state}"        \
        "${filterurl}"    \
        "${fields}"       \
        "${database}" |
            sed -e ":a;N;\$!ba;s/\\n/||/g"  \
                -e "s/||${EISMAN_SEPARATOR}||/||${EISMAN_SEPARATOR}\\n/g" |
            sort                                        |
            sed 's/||/\n/g' > "${outfile}"
    fi

    exit 0
}

main "${@}"
