#!/bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/package-search - Search package
#
# Creation:     2012-01-31  hbfl
# Last Update:  $Id$
#
# Copyright (c) 2012-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org
#
# Usage: package-search
#        or
#        package-search package-name
#
# 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.
#----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

# set ask
ask=/var/install/bin/ask

#debug=true
if ${debug:-false}
then
    exec 2>/tmp/$(basename ${0})-trace$$.log
    set -x
    ask_debug=true
    export ask_debug
fi

# check for CUI menu
if $(grep -qE "^MENU=['\"]/var/install/bin/show-menu.cui['\"]" /etc/config.d/setup)
then
    eisman_browser="/var/install/bin/browse-packages.cui.sh"
else
    eisman_browser="/var/install/bin/browse-packages"
fi

# ---------------------------------------------------------------------------
# show question
# ---------------------------------------------------------------------------
show_question()
{

    clrhome

    mecho --info  'Package search'

cat <<EOF

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

EOF

    _ask_tmpfile=$(mktemp -t XXXXXXXXXXXXX)
    ${ask} "Enter your searchstring (ENTER=Return, 0=Exit)" '' "*" >${_ask_tmpfile}
    rc=${?}
    read search < ${_ask_tmpfile}
    rm -f ${_ask_tmpfile}

    if [ ${rc} = 255 ]
    then
        search=0
    fi

    case ${search} in
    '')
        umount_cdrom
        umount_usb
        exit 0
        ;;
    0)
        umount_cdrom
        umount_usb
        exit 127
        ;;
    *)
        eisman search --unstable "${search}" > /tmp/query.txt
        ${eisman_browser} --install /tmp/query.txt
        _inst_ret="${?}"
        rm -f /tmp/query.txt
        ;;
    esac

    # if browse-packages end with 0=Exit
    # then return 127
    if [ ${_inst_ret} -eq 127 ]
    then
         exit 127
    fi
}


# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    if [ -n "${1}" ]
    then
        eisman search --unstable "${1}"  > /tmp/query.txt
        ${eisman_browser} --install /tmp/query.txt
        _inst_ret="${?}"
        rm -f /tmp/query.txt

        # if browse-packages end with 0=Exit
        # then return 127
        if [ ${_inst_ret} -eq 127 ]
        then
            exit 127
        fi
        exit 0
    fi

    show_question
}

# ---------------------------------------------------------------------------
# call function main
# ---------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------