~mil/sxmo-utils

9f930e742a8e5443d6ecce4252aaa78f668a50cb — Miles Alan 1 year, 6 months ago f95cb51
Simplify sxmo_contacts.sh - use less tempfiles and join logic, output w/ colon
1 files changed, 24 insertions(+), 30 deletions(-)

M scripts/core/sxmo_contacts.sh
M scripts/core/sxmo_contacts.sh => scripts/core/sxmo_contacts.sh +24 -30
@@ 1,35 1,29 @@
#!/usr/bin/env sh

# This script is a helper script for sxmo_modemtext.sh and sxmo_modemcall.sh
# When this script is called from a terminal, it prints the phone's known
# contacts in reverse chronological order of contact.
# 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
# 
# Prints in output format: "number: contact"

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

SORTED_CONTACTS="$(mktemp)"
sort -k2 "$CONTACTSFILE" > "$SORTED_CONTACTS"

# Add names to numbers called/texted in modemlog
tab=$(printf '\t')
CALLED="$(sed 's/ +1//' "$LOGFILE" | sort -u -k3,3 |
	  join -t "$tab" -1 2 -2 3 -o 2.1,1.1,2.3 -a 2 "$SORTED_CONTACTS" - |
	  sort -rk1 | cut -f2,3 | sed 's/^\t//')"

# add all known contacts
ALL_DATA=$(printf %b "$CALLED\n$(cat "$CONTACTSFILE")" | nl)

## We must now remove contacts that have called/texted from the ALL_DATA list

# If there is a name for the contact, the row will have 3 columns
# if there is no name for the contact, the row will only have 2 columns.
# To make sure data lines up, remove contacts called and texted duplicate named contacts seperately
NAMED_ORDER="$(echo "$ALL_DATA" | awk 'NF==3{print}{}' | sort -uk3)"
NONAME="$(echo "$ALL_DATA" | awk 'NF==2{print}{}')"

RES=$(printf %b "$NAMED_ORDER\n$NONAME" | sort -k1 | cut -f2,3)

echo "$RES"
printf %b "$RES" | grep -q 8042221111 || echo "Test Number 8042221111"

rm "$SORTED_CONTACTS"
\ No newline at end of file
contacts() {
	RECENTCONTACTEDNUMBERSREVCHRON="$(
		cut -f3 "$LOGFILE" |
		tac |
		awk '!($0 in a){a[$0];print}' |
		sed '/^[[:space:]]*$/d'
	)"
	RECENTCONTACTEDNUMBERSREVCHRONF="$(mktemp)"
	echo "$RECENTCONTACTEDNUMBERSREVCHRON" > "$RECENTCONTACTEDNUMBERSREVCHRONF"
	printf %b "$(
		join -t"$(printf '\t')" -o1.1,2.2 -a1 -e"Unknown Number" \
		"$RECENTCONTACTEDNUMBERSREVCHRONF" "$CONTACTSFILE" |
		sed 's#\t#: #g'
	)"
	rm "$RECENTCONTACTEDNUMBERSREVCHRONF" &
}

contacts