#!/bin/bash
#M convert DKB-CSV-File to GNUCASH MT940 with converted Description for DKB
# Author: Bruno Brueckmann 2018
if [ -z $1 ]; then
 echo "Error No Input"
 echo "calling: conv-dkbkredit-csv-mt940.sh sourcefile destfile"
 exit 
fi
if [ -z $2 ]; then
 echo "Error No Input"
 echo "calling: conv-dkbkredit-csv-mt940.sh sourcefile destfile"
 exit 
fi
# find decimal seperator
dec_sep=`locale -ck decimal_point|grep decimal_point|sed "s/decimal_point=\"//g"`
dec_sep=`echo $dec_sep|sed "s/\"//g"`
dec_sep=","
dsnout=$2
temp=`env|grep "^HOME="`"/gnucash-dkb_desc.txt"
parms=${temp:5}
if [ ! -f ${parms} ]; then
  echo "Parm-File $parms does not exists"
  echo "I will create, please edit it for your needs "
  echo "BERNMOBIL BUS BERN;Fahrkarten" >$parms
  echo "TREIBSTOFF;Benzin" >>$parms
  echo "BARGELDBEZUG;Bar" >>$parms
  echo "LOHNE BV;Gehalt" >>$parms
  echo "SUNRISE COMMUNICATIONS AG ZURICH TELEFON;Telefon" >>$parms
  echo "VALIANT BANK AG BERN MIETE;Miete" >>$parms
  echo "KONTOFUHRUNG PRIVATKONTO;Bankgebühren" >>$parms
  echo "GIRO INTERNATIONAL (SEPA) ANZAHL TRANSAKTIONEN;Bankgebühren" >>$parms
  echo "AVANEX;Krankenversicherung" >>$parms
  echo "BKB;Strom" >>$parms
  echo "EUR 133.00;Strom Kurt" >>$parms
  echo "STADTWERKE HANAU;Stadtwerke_Hanau" >>$parms
  echo "ROLAND RECHTSSCHUTZ;ROLAND RECHTSSCHUTZ" >>$parms
  echo "WOHNGEBAE;Wohngebäudeversicherung" >>$parms
  echo "HAUSBESITZER-VERS.;HAUSBESITZER-VERS." >>$parms
  echo "GRUNDS TEUER;Grundsteuer" >>$parms
  echo "HEINIGER;Heizungswartung" >>$parms
  echo "" >>$parms
  exit 8
fi
`rm $dsnout`
tempfile="/tmp/gnucashdkb.temp"
`rm $tempfile`
temp=""
# read parameterfile for Description to convert
# Syntax: oldstring;newstring
# line with oldstring is compete replace with newstring
# File sample:
# BERNMOBIL BUS BERN;Fahrkarten
# TREIBSTOFF;Benzin
# 
# !last line must be empty
desc=()
index=0
while IFS=$'\n' read  LINE; do 
 desc[index]="$LINE"
 index=$(expr $index + 1)
done < "$parms"
# concat following lines
echo -n "" >$2   #empty file
#cat $1 | 
n=0  #record -counter
sep=""  # date seperator
while IFS='' read -r LINE
     do 
       if [[ ${#LINE} -gt 0 ]]  # check if Date-Line
         then
          sep=${LINE:3:1}
         fi 
       if [ $n -eq 0 ]  &&  [ "${LINE:1:11}" = "Kreditkarte" ]; then # check if Kreditkarte Input
          sdata=8
         fi
       case "${LINE}" in
           *Kreditkarte:*)                        #Kontonummer
                   Kto=`echo ${LINE} |awk 'BEGIN {FS=";"}{print $2}'|sed "s/\"//g"`
                ;;
            * )                               # Buchungen
                   if [ $n -gt 6 ]
                    then
                     echo ":20:conv-dkbkredit-csv-mt940.sh" >>$2
                     echo ":25:$Kto" >>$2
                     out=""
                     Belegdatum=`echo ${LINE} |awk 'BEGIN {FS=";"}{print $3}'|sed "s/\"//g"`
                     y=`echo $Belegdatum | awk 'BEGIN {FS="."}{print $3}'`
                     m=`echo $Belegdatum | awk 'BEGIN {FS="."}{print $2}'`
                     d=`echo $Belegdatum | awk 'BEGIN {FS="."}{print $1}'`
                     Belegdatum=$m$d
                     Wertstellung=`echo ${LINE} |awk 'BEGIN {FS=";"}{print $2}'|sed "s/\"//g"`
                     y=`echo $Wertstellung | awk 'BEGIN {FS="."}{print $3}'`
                     m=`echo $Wertstellung | awk 'BEGIN {FS="."}{print $2}'`
                     d=`echo $Wertstellung | awk 'BEGIN {FS="."}{print $1}'`
                     Wertstellung=${y:2}$m$d
                     Text=`echo ${LINE} |awk 'BEGIN {FS=";"}{print $4}'|sed "s/\"//g"`
                     Betrag=`echo ${LINE} |awk 'BEGIN {FS=";"}{print $5}'|sed "s/\"//g"`
                     echo "Line "$n ${LINE} "Betrag="$Betrag
                     if [[ "$dec_sep" == "," ]]; then
                        Betrag=`echo $Betrag|sed 's/\.//g'`
                     fi
                     if [[ "$dec_sep" == "." ]]; then
                        Betrag=`echo $Betrag|sed 's/\,//g'`
                     fi
                     out=":61:$Wertstellung$Belegdatum"
                     if [[ $(echo "0 > $Betrag"|sed 's/,/./g'|bc -l) -eq 1 ]] 
                      then
                       out=$out"DR"${Betrag:1}
                      else
                       out=$out"CR"$Betrag
                      fi
                      echo $out"NTRFNONREF" >>$2 
                      echo ":86:020?00$Text?109392?20$Text" >>$2
                     fi
                    ;;
              esac 
     n=`echo $n+1|bc`
done < <(tail -n +2 $1)
#echo $temp >>$dsnout
#echo $LINE >>$dsnout
echo "Input converted to $dsnout"
exit
