#!/bin/bash
# *******************************************************************
# **  @file eisman_search.sh - fuzzy search in package database
# **
# **  Creation:     2013-01-13 dv
# **  Last update:  $Id: eisman_search.sh 41954 2016-06-18 18:47:30Z hbfl $
# **
# **  Copyright (c) 2013-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
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
   --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
            ;;
        *)
            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 "${@}"
