summaryrefslogtreecommitdiffstats
path: root/helpers/aiaiai-match-keywords
blob: 54af7c5974a02fd48cb9235d9eca9fcc3023cbe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh -euf

# Copyright 2011-2012 Intel Corporation
# Author: Alexander Shishkin, Artem Bityutskiy
# License: GPLv2

srcdir="$(readlink -ev -- ${0%/*})"
PATH="$srcdir:$srcdir/libshell:$PATH"

. shell-error
. shell-args
. shell-quote
. aiaiai-sh-functions

PROG="${0##*/}"
message_time="yes"

# This is a small trick to make sure the script is portable - check if 'dash'
# is present, and if yes - use it.
if can_switch_to_dash; then
	exec dash -euf -- "$srcdir/$PROG" "$@"
	exit $?
fi

show_usage()
{
	cat <<-EOF
Usage: $PROG [options] <file>

Match various keywords - e.g., companies often prohibit some for whatever
reasons. The keywords are read from stdin (one keyword per line) and matched
against <file>.

Options:
  -v, --verbose          be verbose;
  -h, --help             show this text and exit.
EOF
}

fail_usage()
{
	[ -z "$1" ] || printf "%s\n" "$1"
	show_usage
	exit 1
}

first_match=
look_for_keyword()
{
	local kw="$1"
	local match

	verbose "look for keyword \"$kw\""

	match="$(grep -i "$(quote_sed_regexp "$kw")" "$file" ||:)"
	[ -n "$match" ] || return 0

	[ -z "$first_match" ] || echo ""

	first_match="no"
	printf "%s\n\n" "Matched keyword \"$kw\""
	printf "%s\n" "$match" | head -n8
}

TEMP=`getopt -n $PROG -o v,h --long verbose,help -- "$@"` || fail_usage ""
eval set -- "$TEMP"

mbox=
verbose=

while true; do
	case "$1" in
	-v|--verbose)
		verbose=-v
		;;
	-h|--help)
		show_usage
		exit 0
		;;
	--) shift; break
                ;;
	*) fail_usage "Unrecognized option: $1"
		;;
	esac
	shift
done

[ "$#" -eq 1 ] || fail_usage "Please, specify exactly one argument - the file with keywords"
file="$(readlink -ev -- "$1")"

while IFS= read -r kw; do
	look_for_keyword "$kw"
done