#!{{dash}}
# BEWARE: This file is operated by slapgrid

set -e

SLOW_QUERY_PATH='{{slow_query_path}}'
OUTPUT_FOLDER='{{output_folder}}'
PT_QUERY_EXEC='{{pt_query_exec}}'

if [ ! -d "$OUTPUT_FOLDER" ]; then
  echo "ERROR: output_folder don't exists"
  exit 1
fi

dashed_today=$(date +%Y-%m-%d)
today=$(date -d "$dashed_today" +%Y%m%d)

SLOW_LOG="$SLOW_QUERY_PATH-$today"
OUTPUT_FILE="$OUTPUT_FOLDER/slowquery_digest.txt-$dashed_today"

if [ ! -f "$SLOW_LOG" ]; then
  echo "ERROR: cannot read mysql slow query log file $SLOW_LOG. Exiting."
  exit 1
fi

"$PT_QUERY_EXEC" "$SLOW_LOG" > "$OUTPUT_FILE"

{{ xz }} -9 "$OUTPUT_FILE"
