~mil/sxmo-utils

ref: dbc3b1ab509aab5f1db72c5cece2ee2a6eb521e8 sxmo-utils/scripts/core/sxmo_contacts.sh -rwxr-xr-x 1.2 KiB
dbc3b1abStacy Harper Allow the sendnewtextmenu to be used with a number as arg 1 year, 4 months ago
                                                                                
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
#!/usr/bin/env sh
# This script prints in reverse chronological order unique entries from the
# modem log merged with contact names defined in contacts file tsv.
#   Wherein $CONTACTSFILE is tsv with two fields: number\tcontact name
#   Wherein $LOGFILE is *sorted* tsv with three fields: date\tevt\tnumber
#
#   Most normal numbers should be a full phone number starting with + and the country number
#   Some special numbers (ie. 2222, "CR AGRICOLE") can ignore this pattern
#
# Prints in output format: "number: contact"

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

CONTACTSFILE="$XDG_CONFIG_HOME"/sxmo/contacts.tsv
LOGFILE="$XDG_DATA_HOME"/sxmo/modem/modemlog.tsv

contacts() {
	grep -q . "$CONTACTSFILE" || echo " " > "$CONTACTSFILE"
	RECENTCONTACTEDNUMBERSREVCHRON="$(
		cut -f3 "$LOGFILE" |
		tac |
		awk '!($0 in a){a[$0]; print}' |
		sed '/^[[:space:]]*$/d'
	)"
	printf %b "$RECENTCONTACTEDNUMBERSREVCHRON" | awk -F'\t' '
		FNR==NR{a[$1]=$2; next}
		{
			if (!a[$1]) a[$1] = "Unknown Number";
			print $0 ": " a[$1]
		}
	' "$CONTACTSFILE" -
}

all_contacts() {
	awk -F'\t' '{
		print $1 ": " $2
	}' "$CONTACTSFILE" | sort -f -k 2
}

if [ "$1" = "--all" ]; then
	all_contacts
else
	contacts
fi