#!/bin/bash

DIR=/tmp/ledgerd
PFX=$DIR/$(id -un)
COSI=$PFX.cosi
CISO=$PFX.ciso

trap "rm -f $PFX.*" EXIT

if [ -e "$COSI" ]; then
  rm -f $PFX.*
fi

mkdir -p $DIR
chgrp www-data $DIR
mkfifo -m 0660 $COSI
mkfifo -m 0660 $CISO
chgrp www-data $PFX.*

while true
do
  if read line <$COSI; then
    date
    echo "$line"
    if [[ "$line" == 'shutdown' ]]; then
      break
    fi
    rclone sync onedrive:Dokumenty/Buchalteria/ ~/Buchalteria
    ledger balance -f ~/Buchalteria/main.txt $line -F "%A;%t\n" >$CISO
    echo
  fi
done

