#!/bin/bash
# This script performs the analysis from the input shaping data

KLIPPER_NAME="$1"
if [[ "${REPETIER_DATA_DIR}" == "" ]]; then
  REPETIER_DATA_DIR="/var/lib/Repetier-Server/"
fi
KLIPPER_DIR="klipper"
ROOT_DIR="/opt/${KLIPPER_DIR}"
SRC_DIR="${ROOT_DIR}/${KLIPPER_NAME}"
DATE=$(date +%s)

start_analysis() {
  if test -f "/tmp/calibration_data_x_data.csv"; then
    echo "Input Shaping Analysis: X Axis"
    ${SRC_DIR}/klippy-env/bin/python ${SRC_DIR}/scripts/calibrate_shaper.py /tmp/calibration_data_x_data.csv -o "${REPETIER_DATA_DIR}printer/${KLIPPER_NAME}/data/${DATE}_x_input_shaping.png"
    chown repetierserver "${REPETIER_DATA_DIR}printer/${KLIPPER_NAME}/data/${DATE}_x_input_shaping.png"
  else
    echo "No data for x axis found - skipping axis."
  fi
  if test -f "/tmp/calibration_data_y_data.csv"; then
    echo "Input Shaping Analysis: Y Axis"
    ${SRC_DIR}/klippy-env/bin/python ${SRC_DIR}/scripts/calibrate_shaper.py /tmp/calibration_data_y_data.csv -o "/var/lib/Repetier-Server/printer/${KLIPPER_NAME}/data/${DATE}_y_input_shaping.png"
    chown repetierserver "${REPETIER_DATA_DIR}printer/${KLIPPER_NAME}/data/${DATE}_y_input_shaping.png"
  else
    echo "No data for y axis found - skipping axis."
  fi
}

start_analysis
