#!/bin/bash
# *******************************************************************
# **  @file eisman_search.sh - fuzzy search in package database
# **
# **  Creation:     2013-01-13 dv
# **  Last update:  $Id: eisman_search.sh 44252 2016-12-30 23:14:30Z dv $
# **
# **  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

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

show_help()
{
cat <<EOF

Fuzzy search package database

usage:
    eisman search [OPTIONS] [SEARCH WORD]

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)

SEARCH WORD:
    a character string that may exist as substring withing a package
    name

EOF
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    # flags
    flags=""

    # read option switches
    while [ "${1:0:2}" = "--" ]
    do
        case ${1} in
        --help)
            shift
            show_help
            exit 0
            ;;
        --excl-sections)
            shift
            flags="$flags --excl-sections=$1"
            shift
            ;;
        --sections)
            shift
            flags="$flags --sections=$1"
            shift
            ;;
        --fields)
            shift
            flags="$flags --fields=$1"
            shift
            ;;
        --url)
            shift
            flags="$flags --url=$1"
            shift
            ;;
        --output)
            shift
            flags="$flags --output=$1"
            shift
            ;;
        --output=*)
            flags="$flags --output=${1:9}"
            shift
            ;;
        --installed)
            flags="$flags --installed"
            shift
            ;;
        --outdated)
            flags="$flags --outdated"
            shift
            ;;
        *)
            flags="$flags ${1}"
            shift
            ;;
        esac
    done

    if [ -n "${1}" ]
    then
        package="${1}"
        shift
    fi

    pattern1="${package}"
    pattern2="$(echo "${package}" | sed 's#-#_#g')"
    pattern3="$(echo "${package}" | sed 's#_#-#g')"

    # perform query
    ${EISMAN_BIN_PATH}/eisman_query.sh ${flags} \
        "(((*|)${pattern1}(*|))|((*|)${pattern2}(*|))|((*|)${pattern3}(*|)))"

    exit 0
}

main "${@}"
