#!/bin/bash # Copyright (c) 2008 by Emin Gabrielyan, Switzernet verbose=0 xml_suffix="range.xml" merge_suffix="merge-xml.txt" htm_suffix="postfinance-xml.htm" doc_suffix="postfinance-xml.doc" perl_script="a47.pl.txt" if [ ! -f $perl_script ] then echo "the perl script \"$perl_script\" is not found" exit 1 fi ls *.tar.gz > /dev/null 2>&1 if [ ! $? -eq 0 ] then echo "No *.tar.gz file found" exit 1 fi if [ ! "`perl -e 'print 1+2' 2>/dev/null`" = "3" ] then echo "Perl is not installed" exit 1 fi tar --version > /dev/null 2>&1 if [ $? -ne 0 ] then echo "tar program is not installed" exit 1 fi awk --version > /dev/null 2>&1 if [ $? -ne 0 ] then echo "awk program is not installed" exit 1 fi convert --version 2> /dev/null | grep -q ImageMagick if [ $? -ne 0 ] then echo "ImageMagick (http://www.imagemagick.org) is not installed" exit 1 fi if [ "$verbose" = "1" ] then echo "Removing the \"scans\" folder and all XML files, if any, in 5 seconds..." echo "[Press Ctrl-C to Stop]" sleep 5 fi rm --recursive scans 2> /dev/null rm *.xml 2> /dev/null mkdir scans 2>/dev/null if [ $? -ne 0 ] then echo "Cannot create folder \"scans\"" exit 1 fi for f in *.tar.gz do tar --wildcards -z -x "*.xml" -f $f 2> /dev/null if [ $? -ne 0 ] then echo "No XML file is found in $f" exit 1 fi tar --wildcards --directory=scans -z -x "???????????????????????.png" -f $f 2> /dev/null done for f in *.xml do params=`./$perl_script xml=$f` if [ $? -ne 0 ] then echo "the XML file $f contains error" exit 1 fi eval $params mv $f $opening_date-$closing_date-$xml_suffix done ls ??????-??????-$xml_suffix > /dev/null 2>&1 if [ $? -ne 0 ] then echo "No ??????-??????-$xml_suffix file found" exit 1 fi ls *-$xml_suffix | sort | awk -v FS=- ' BEGIN{err=0} closing_date!=""{if($1!=closing_date){ print "Error: The range " $1 "-" $2 " does not follow the range " opening_date "-" closing_date "."; err=1 }} {closing_date=$2; opening_date=$1} END{exit err} ' if [ $? -ne 0 ] then echo "The files [tar.gz] are not consecutive. Check for gaps or overlaps." echo "You probably missed a file or downloaded a weekly file together with daily files." exit 1 fi open_date=`ls ??????-??????-$xml_suffix | head -1 | cut -d- -f1` close_date=`ls ??????-??????-$xml_suffix | tail -1 | cut -d- -f2` merge_xml=$open_date-$close_date-$merge_suffix cat ??????-??????-$xml_suffix > $merge_xml rm ??????-??????-$xml_suffix params=`./$perl_script xml=$merge_xml` if [ $? -ne 0 ] then echo "the merged XML file $merge_xml contains error" exit 1 fi eval "$params" perl -e "if(\"$start_date\" le \"$end_date\"){exit 0}else{exit 1}" if [ $? -ne 0 ] then echo "The entries of the merged XML file ($opening_date-$closing_date) do not cover one full day." echo "The balance is opened on $opening_date and is closed on $closing_date." echo "This file contains the end of $end_date day, but you need the preceding [tar.gz] file for the beginning." echo "It contains also the beginning of $start_date day, but you need the following [tar.gz] file for completing the day." exit 1 fi echo -ne "Cropping." for f in scans/???????????????????????.png do if [ -f $f ] then ref=`basename $f .png` convert -crop 670x620+475+28 $f scans/crop.$ref.png echo -ne "." fi done echo "Done" file_htm=$start_date-$end_date-$htm_suffix file_doc=$start_date-$end_date-$doc_suffix watch="(telekurs\s+multipay|ordre\s+debit\s+direct|online-shopping\s+deferred)" params=`./$perl_script xml=$merge_xml save=$start_date-$end_date htm=$file_htm watch=$watch exclude=yes` if [ $? -ne 0 ] then echo "error generating the HTML file $file_htm from $merge_xml" exit 1 fi rm ??????-??????-$merge_suffix date1=$opening_date date2=$start_date date3=$end_date date4=$closing_date echo "The file $file_htm contains only the transactions" echo "of complete days from $date2 thru $date3:" echo echo " opening | start end | closing" echo " balance | date date | balance" echo " --------+----------------+--------" echo " $date1 | $date2 $date3 | $date4 " echo " --------+----------------+--------" echo " YYMMDD | yymmdd yymmdd | YYMMDD " echo echo "Copy the full content of" echo "HTML file $file_htm (Ctrl-A, Ctrl-C)" if [ ! -f empty.doc ] then echo the file empty.doc is not found exit 1 fi cp empty.doc $file_doc echo "and paste the content into" echo "DOC file $file_doc (Ctrl-V)" programsdir=`ls -d /cygdrive/*/Program\ Files 2>/dev/null | head -1` if [ -z "$programsdir" ] then echo Program Files folder is not found exit 1 fi officedir=`ls -d "$programsdir/Microsoft Office" 2>/dev/null | head -1` if [ -z "$officedir" ] then echo Microsoft Office is not found exit 1 fi msword=`find "$officedir" -iname winword.exe 2>/dev/null | head -1` if [ -z "$msword" ] then echo Microsoft Word is not found exit 1 fi iedir=`ls -d "$programsdir/Internet Explorer" 2>/dev/null | head -1` if [ -z "$iedir" ] then echo Internet Explorer folder is not found exit 1 fi iexplore=`find "$iedir" -iname iexplore.exe 2>/dev/null | head -1` if [ -z "$iexplore" ] then echo Internet Explorer program is not found exit 1 fi pwddos=`pwd | perl -ne '/^\/cygdrive\/(\w+)\/(.*)$/; print "$1:/$2"'` "$iexplore" "file://$pwddos/$file_htm" & "$msword" $file_doc & echo "" echo "Insert the =SUM(ABOVE) formula" echo "at the end of the DOC file (Alt-A-O-Enter)" echo "Save DOC file $file_doc (Ctrl-S)" echo "and post it to the "