spacefrogg-overlay/ledger-tools/ledger-camt-csv

43 lines
1.2 KiB
Bash

#!/bin/sh
# Sparkasse Germany CAMT CSV format
# Sparkasse Germany VISA CSV format
set -e
PATH=@binpath@:$PATH
[ $# -ge 2 ] || { printf "Usage: ledger-camt-csv [-visa | -camt] CSV ACCOUNT\n">&2; exit 1; }
CAMT_HDR=",date,posted,,note,,,,,,,payee,pIBAN,pBIC,amount,,"
VISA_HDR=",date,posted,oAmount,oCommodity,,amount,,payee,note,,,,,,"
HDR=$CAMT_HDR
case $1 in
-visa) shift
HDR=$VISA_HDR
;;
-camt) shift
HDR=$CAMT_HDR
;;
esac
DATE_FMT="%d.%m.%y"
INFILE=$1
_INDIR=$(dirname "$INFILE")
if [ "${_INDIR##/}" != "new" ]; then
printf "Error: Expecting input file in new/ of a maildir-like directory structure\n" >&2
exit 1
fi
BASEDIR=$(dirname $1)/..
BASEDIR=$(realpath "$BASEDIR")
MANGLED=$(mktemp -p $BASEDIR/tmp/ mangled-XXXXXX.csv)
OUTFILE=$BASEDIR/tmp/$(basename "${INFILE%.CSV}.ledger")
{ echo "$HDR"; tail -n+2 "$INFILE"; } |
sed -e 's/";"/","/g' |
iconv -f ISO-8859-1 -t UTF-8 >$MANGLED
ledger convert "$MANGLED" --input-date-format "$DATE_FMT" --invert --rich-data --account $2 >$OUTFILE
echo "$BASEDIR/new/"$(basename "$OUTFILE") >&2
rm -f "$MANGLED"
mv "$INFILE" "$BASEDIR/cur"
mv "$OUTFILE" "$BASEDIR/new"