#!/bin/sh dir="$HOME/projects/tx" tagfile="$dir/txtags" txfile="$dir/transactions.csv" balfile="$dir/balances" pinfile="$HOME/.aqbanking/pin" auto_tag(){ while IFS="|" read -r tag find do sed -i '/'"$find"'/I s/$/;"'"$tag"'"/' "$1" done < "$tagfile" } tag_tx(){ nr="$2" shift 2 sed -i ''"$nr"'s/$/;"'"$*"'"/' "$txfile" } print_tx(){ cat "$txfile" | tr -d '"' | awk -v var="$1" -v var2="$2" -F";" ' $8 ~ var && tolower($0) ~ var2{ total += $8 tags="" for(i=33;i<=NF;i++){ tags=tags" "$i } printf "%-5s %s %9.2f %s\n", NR, $7, $8, tags } END { if(total){ printf "\t\tΣ %9+.2f\n", total }else{ exit 1 } } ' } pull_tx(){ tmpfile=$(mktemp) tmpfile2=$(mktemp) tmpfile3=$(mktemp) aqbanking-cli -n -P "$pinfile" request --transactions > "$tmpfile" 2> /dev/null aqbanking-cli -n listtrans -c "$tmpfile" | tail -n +2 > "$tmpfile2" awk ' FNR==NR{ a[FNR]=$0 last=FNR next } { found=0 for(i=1;i<=last;i++){ if(index(a[i],$0) > 0){ found=1 break } } if(found==0){print $0} } ' "$txfile" "$tmpfile2" > "$tmpfile3" auto_tag "$tmpfile3" sort -t";" -k 6,6 "$tmpfile3" "$txfile" -o "$txfile" bal="$(aqbanking-cli listbal -c "$tmpfile" | awk '{print $8}')" printf "%s\\t%s\\n" "$(date +"%m_%d_%Y")" "$bal" >> "$balfile" rm "$tmpfile" "$tmpfile2" "$tmpfile3" } case "$1" in p|pull) pull_tx;; b|balance) tail -n1 "$balfile" | cut -f2;; e|expenses) print_tx "^-" "$2";; r|revenue) print_tx "^[0-9]" "$2";; t|tag) tag_tx "$@";; s|show) sed "$2q;d" "$txfile";; push) (cd $dir && git add . && git commit -m "tx push" && git push);; *) print_tx "^" "$1" | tail -100;; esac