summaryrefslogtreecommitdiff
path: root/.local/bin/pick
blob: 6dc905fe29e85e926693b4347639f1d3e7e795da (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
#!/bin/bash

cmd=${0##*/}

if [[ $1 = -h ]]; then
	echo "Usage: $cmd PROG1 PROG2 PROG3..."
	echo "       $cmd -s PROG1 PROG2 PROG3..."
	echo ""
	echo "If \`-s' ISN'T given, print the first program name given that is"
	echo "found in PATH."
	echo ""
	echo "If \`-s' IS given, print the first program name given that is"
	echo "currently running.  If no match is found, fall back to default"
	echo "behavior."
	exit 0
fi

if [[ $1 = -s ]]; then
	shift
	# Scan to find a running instance
	for prog in "$@"; do
		if [[ -n "$(pgrep "${prog%% *}")" ]]; then
			printf '%s\n' "$prog"
			exit 0
		fi
	done
fi

# Scan to find one that is installed
for prog in "$@"; do
    if [[ -x "$(which "${prog%% *}" 2>/dev/null)" ]]; then
		printf '%s\n' "$prog"
		exit 0
    fi
done

printf '%s\n' "$cmd: no suitable program found"
exit 1