#!/bin/bash

if [ $# -lt 2 ]; then
  echo "usage: $0 [options] outfile host fromdate todate"
  echo "options: -a all (default)"
  echo "         -x xml files only"
  exit 1
fi

DOWNLOAD=zip

while [ "${1:0:1}" = "-" ]; do
  case "${1:0:2}" in
    -x) DOWNLOAD=xml;;
  esac
  shift
done

OUTPUT="$1"
HOST="$2"
if [ "$HOST" = localhost ]; then HOST=127.0.0.1; fi
FROM="${3/ /+}"
TO="${4/ /+}"
curl http://$HOST/stat/index.php -d 'report_name=Elenco+fatture' -d download=$DOWNLOAD -d "_FormFromDate_=$FROM" -d "_FormToDate_=$TO" --output $OUTPUT 

XMLCOUNT=`unzip -l $OUTPUT | grep xml | wc -l`
echo "Fatture XML esportate: $XMLCOUNT" > /tmp/download_invoices_report
if [ "$DOWNLOAD" == "zip" ]
then
  PDFCOUNT=`unzip -l $OUTPUT | grep pdf | wc -l`
  echo "Fatture PDF esportate: $PDFCOUNT" >> /tmp/download_invoices_report
fi

