-
Notifications
You must be signed in to change notification settings - Fork 1
/
llocate
executable file
·68 lines (54 loc) · 1.27 KB
/
llocate
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
#!/usr/bin/env bash
# Local locate, store DB in user directory.
# Update on run if older than configured.
set -euo pipefail
DEFAULT_LLOCATE_DB_PATH=$HOME/.cache/locate.db
DEFAULT_LLOCATE_DB_AGE=1day
LLOCATE_DB_PATH=${LLOCATE_DB_PATH:-$DEFAULT_LLOCATE_DB_PATH}
LLOCATE_DB_AGE=${LLOCATE_DB_AGE:-$DEFAULT_LLOCATE_DB_AGE}
usage() {
cat <<EOF
Usage: ${0##*/} [-h|-u] [locate options] term
Perform locate for given term.
Options:
-u force updatedb
-h show this help
Envirinment variances:
LLOCATE_DB_PATH path to databas file (default: $DEFAULT_LLOCATE_DB_PATH)
LLOCATE_DB_AGE period after which database need to update,
in date string format (default: $DEFAULT_LLOCATE_DB_AGE)
EOF
exit 1
}
update() {
updatedb --require-visibility 0 --output "$LLOCATE_DB_PATH"
}
is_database_old() {
local threshold db_mtime
threshold=$(date -d "-$LLOCATE_DB_AGE" +%s)
db_mtime=$(date -r "$LLOCATE_DB_PATH" +%s)
if [ "$threshold" -gt "$db_mtime" ]; then
return 0
fi
return 1
}
main() {
if [ $# -eq 0 ]; then
usage
fi
while :; do
case "$1" in
-u)
update
shift
;;
-h) usage ;;
*) break ;;
esac
done
if is_database_old; then
update
fi
locate --database "$LLOCATE_DB_PATH" "$@" | ${PAGER:-less}
}
main "$@"