#!/bin/bash

ffoo import core
ffoo import ini


filter_exprs() {
    #
    # Hack expressions like bug = 123 out of the text
    #
    sed -e 's/(\d)\</\n/;'\
      | perl -ne '
            next unless m/\b([a-zA-Z]{2,}\s*=\s*\d+)\b/;
            print "$1\n";
        ' \
      | debug_pipe id_lookalikes
}


filter_ids() {
    #
    # Hack doer-like id's (ID#123) out of the text
    #
    tr ' ' '\n' \
      | perl -ne '
            next unless m/\b([a-zA-Z]{2,}#\d+)\b/;
            print "$1\n";
        ' \
      | debug_pipe id_lookalikes
}


filter_kws() {
    #
    # Hack out lines that look like kw expressions
    #
    local kwbase="[a-z][a-z0-9]*"
    grep -e "^$kwbase " -e "^$kwbase\$" \
        | debug_pipe kw_lookalikes
}


filter_uris() {
    #
    # Hack URIs out of the text.
    #
    tr ' ' '\n' \
      | perl -ne '
            next unless m|(\bhttps?://[[:alnum:]_:/.?&+%@=-]+)\b|;
            print "$1\n";
        ' \
      | debug_pipe uri_lookalikes
}


find_uri() {
    #
    # Scan stdin for what looks like URI, ID or keyword
    #
    # Output URIS: first what were apparent uris, then IDs converted to
    # URIs, then equal sign expressions, then things that looked like
    # kw expressions, converted to URIs.
    #
    # Apply this filter to args or clipboard, and either use head -1 or
    # if you are brave, open all URIs.
    #
    local d=$(mktemp -d)
    pushd $d >&/dev/null
        ##
        # heat up and fill pipes
        #
        mkfifo maybe_uris maybe_ids maybe_exprs maybe_kws \
               uris uris_from_ids uris_from_exprs uris_from_kws
        cat | tee maybe_uris maybe_ids maybe_exprs maybe_kws >/dev/null &
        ##
        # process each pipe *async* by different filter
        #
        cat maybe_uris  | filter_uris                     > uris &
        cat maybe_ids   | filter_ids   | id2kw   | kw2uri > uris_from_ids &
        cat maybe_exprs | filter_exprs | expr2kw | kw2uri > uris_from_exprs &
        cat maybe_kws   | filter_kws   |           kw2uri > uris_from_kws &
        ##
        # print result *sync* in correct order
        #
        {
            cat uris
            cat uris_from_ids
            cat uris_from_exprs
            cat uris_from_kws
        } | grep .  # throw away empties; add missing LF
    popd >&/dev/null
    rm -rf $d
}


id2kw() {
    #
    # Convert doer-like ID to kw expression
    #
    tr '#' ' '
}


expr2kw() {
    #
    # Convert equal sign expression to kw expression
    #
    sed -re 's/\s*=\s*/ /' | tr '[:upper:]' '[:lower:]'
}


kw2uri() {
    #
    # Turn keyword or query (like "g hello" for google) to URI
    #
    local line
    declare -l line
    while read line;
    do
        line="$line "
        local kw=${line%% *}
        local query=${line#$kw }
        debug -v kw query
        if test -n "$query";
        then
            local fmt=$(iniread -1 -p www.query.$kw)
            debug -v fmt
            printf "$fmt\n" "$query"
        else
            iniread -1 -p www.bookmark.$kw
        fi
    done
}