#!/usr/bin/env python
#
# Data Quality Checker also known as DQC
#
# Read the README.md for more detailed information
#
#       Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
#       Earth Sciences Department - Computational Earth Sciences
#
#     Authors:
#       Julian Rodrigo Berlin (julian.berlin@bsc.es)
#

# This script checks if there are scheduled deletion files generated by the checker
# sample usage: only the paths where are the files are needed
# ./schedule_free_space.sh /home/Earth/jberlin/Development/dqc_results/
# previously you need to configure the config file:
# dqc_wrapper/conf/scripts_settings_conf.json
# the following keys:
#  "dqc_files_removal_enable": "False/True",
#  "dqc_files_removal_path": "/Path to store schedule files/",
# crontab configuration (under eqc user):
# 0 0 * * * /shared/QAR_dqc/bin/schedule_free_space.sh
# sample file generated
# datafiles-to-be-deleted-20201127.checked

source ~/.bashrc

# Filter
if [[ -z "$1" ]] ; then
    echo "No path supplied"
    exit 1
fi
path_results=$1

# get the current date
current_date=$(date '+%Y%m%d')
scheduled_deletion_file=${path_results}/datafiles-to-be-deleted-${current_date}.checked
echo ${scheduled_deletion_file}
if [[ -f ${scheduled_deletion_file} ]] ; then
   # read the content of the file
   # secure that we are only deleting grib files
   while IFS= read -r datafile
   do
      if [[ -f ${datafile} ]] ; then
        echo "deleting ${datafile}"
        # rm ${datafile}
      fi
   done < "${scheduled_deletion_file}"
   # remove the file after processing it without errors
   #rm ${scheduled_deletion_file}
else
  echo "nothing to delete on ${current_date}"
fi