#!/usr/bin/bash

centreon_bi_home=/usr/share/centreon-bi/
etl_home=$centreon_bi_home/etl
importData=$etl_home/importData.pl
dimensionsBuilder=$etl_home/dimensionsBuilder.pl
eventStatisticsBuilder=$etl_home/eventStatisticsBuilder.pl
perfdataStatisticsBuilder=$etl_home/perfdataStatisticsBuilder.pl
centreon_bi_log=/var/log/centreon-bi/
progname=$0

function print_help {
    echo ""
    echo "$progname: Usage:"
    echo ""
    echo "Execution modes"
    echo "    -c  Create the reporting database model"
    echo "    -d  Daily execution to calculate statistics on yesterday"
    echo "    -r  Rebuild mode to calculate statitics on a historical period. Can be used with:"
    echo "        Extra arguments for options -d and -r (if none of the following is specified, these one are selected by default: -IDEP):"
    echo "        -I  Extract data from the monitoring server"
    echo "            Extra arguments for option -I:"
    echo "            -C  Extract only Centreon configuration database only. Works with option -I."
    echo "            -i  Ignore perfdata extraction from monitoring server"
    echo "            -o  Extract only perfdata from monitoring server"

    echo "        -D  Calculate dimensions"
    echo "        -E  Calculate event and availability statistics"
    echo "        -P  Calculate perfdata statistics"
    echo "            Common options for -rIDEP:"
    echo "            -s  Start date in format YYYY-MM-DD."
    echo "                By default, the program uses the data retention period from Centreon BI configuration"
    echo "            -e  End date in format YYYY-MM-DD."
    echo "                By default, the program uses the data retention period from Centreon BI configuration"
    echo "            -p  Do not empty statistic tables, delete only entries for the processed period."
    echo "                Does not work on raw data tables, only on Centreon BI statistics tables."
    echo "Other option:"
    echo "    -h  prints help"
    echo ""
}

import=0
dimensions=0
event=0
perfdata=0
start=""
end=""
rebuild=""
daily=""
nopurge=""
create_tables=""
ignore_databin=""
databin_only=""
centreon_only=""

# getting command line arguments
while getopts :rdhIDEPs:e:cioCp flag; do
  case $flag in
    r)
      rebuild="-r";
      ;;
    d)
      daily="-d";
      ;;
    h)
      print_help;
      exit 1;;

    I)
      import=1;
      ;;

    D)
      dimensions=1;
      ;;

    E)
      event=1;
      ;;

    P)
      perfdata=1;
      ;;

    s)
      start="-s $OPTARG";
      ;;

    e)
      end="-e $OPTARG";
      ;;

    c)
      create_tables="--create-tables";
      ;;

    i)
      ignore_databin="--ignore-databin";
      ;;

    o)
      databin_only="--databin-only";
      ;;

    C)
      centreon_only="--centreon-only";
      ;;

    p)
      nopurge="--no-purge";
      ;;

    ?)
      print_help;
      exit 1;;

  esac
done

shift


if [ "$create_tables" != "" ]; then
    perl $importData $create_tables
    exit $?
fi

if [ $import -eq 0 ] && [ $dimensions -eq 0 ] && [ $event -eq 0 ] && [ $perfdata -eq 0 ]; then
    import=1
    dimensions=1
    event=1
    perfdata=1
fi

arguments="$daily $rebuild"
optionnal_arguments="$start $end $nopurge"
import_arguments="$create_tables $ignore_databin $databin_only $centreon_only"
if [ $import -eq 1 ]; then
    echo ""
    echo "STEP : Data extraction from monitoring server"
    echo ""
    perl -w $importData $arguments $optionnal_arguments $import_arguments
    if [ $? -eq 3 ]; then
    	exit 3
    fi
fi
if [ $dimensions -eq 1 ]; then
    echo ""
    echo "STEP : Processing dimensions"
    echo ""
    perl -w $dimensionsBuilder $arguments
    if [ $? -eq 3 ]; then
        exit 3
    fi
fi
if [ $event -eq 1 ]; then
    echo ""
    echo "STEP : Processing incident and availability statistics"
    echo ""
    perl -w $eventStatisticsBuilder $arguments $optionnal_arguments
    if [ $? -eq 3 ]; then
        exit 3
    fi
fi
if [ $perfdata -eq 1 ]; then
    echo ""
    echo "STEP : Processing performance data"
    echo ""
    perl -w $perfdataStatisticsBuilder $arguments $optionnal_arguments
    if [ $? -eq 3 ]; then
        exit 3
    fi
fi
exit 0
