#!/bin/bash
#
# bee-query - query bee metadata
#
# Copyright (C) 2009-2016
#       Marius Tolzmann <m@rius.berlin>
#       Tobias Dreyer <dreyer@molgen.mpg.de>
#       and other bee developers
#
# This file is part of bee.
#
# bee 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

if [ -z "${BEE_VERSION}" ] ; then
    echo >&2 "BEE-ERROR: please call $0 from bee .."
    exit 1
fi

VERSION=${BEE_VERSION}

: ${BEE_BINDIR:=@BINDIR@}
: ${BEE_LIBEXECDIR:=@LIBEXECDIR@}

function bee-list() {
    ${BEE_LIBEXECDIR}/bee/bee.d/bee-list "${@}"
}

BEESEP=${BEE_BINDIR}/beesep

##### usage ###################################################################
usage() {
    cat <<-EOF
	bee-query v${VERSION} 2009-2016
	  by Marius Tolzmann <m@rius.berlin> and Tobias Dreyer <dreyer@molgen.mpg.de>

	Usage: bee query [package|pattern|file]

	EOF
}

function query() {
    list="$@"

    for f in "${list[@]}" ; do
        # check if $f is pkg, list related files
        # otherwise list pkg
        base=${f##*/}
        if [ ! -z "${base}" -a -d "${BEE_METADIR}/${base}" ] ; then
            eval $(${BEE_BINDIR}/beeversion $base)
            get_files "${PKGALLPKG}"
        else
            get_pkgs "${f}"
        fi
    done
}

function get_files() {
    pkg=${1}

    for s in "${BEE_METADIR}" ; do
        ff="${s}/${pkg}/CONTENT"
        if [ -e "${ff}" ] ; then
            while read line ; do
                eval $(${BEESEP} "${line}")
                echo ${file}
            done < "${ff}"
        fi
    done
}

function get_pkgs() {
    f=$1

    for pkg in $(bee-list --installed) ; do

        if egrep -q "file=.*${f}" "${BEE_METADIR}/${pkg}/CONTENT" ; then
            echo ${pkg}
            while read line ; do
                eval $(${BEESEP} "${line}")
                echo "  ${file}"
            done < <(egrep "file=.*${f}" "${BEE_METADIR}/${pkg}/CONTENT")
        fi
    done
}


###############################################################################
###############################################################################
###############################################################################

options=$(${BEE_BINDIR}/beegetopt --name bee-query \
                 --option help/h \
                 -- "$@")
if [ $? != 0 ] ; then
    usage
    exit 1
fi
eval set -- "${options}"

while true ; do
  case "$1" in
    --help)
      usage
      exit 0
    ;;
    *)
      shift
      if [ -z "${1}" ] ; then
           usage
           exit 1
      fi
      query "${@}"
      exit 0;
      ;;
  esac
done