#!/bin/bash
set -euo pipefail

# Compress and expire Starfish job logs, they can grow really big
# Starfish-compress-job-logs does the following:
#   Compresses files older than 2 days.
#   Deletes directories with contents older than 90 days.


[ -x /opt/starfish/bin/compress_logs ] || exit 0

JOBS_LOG_DIR="/opt/starfish/log/jobs"
[ -d ${JOBS_LOG_DIR} ] || exit 0

SFHOME="${SFHOME:-/opt/starfish}"
PROMTAIL_MONITORED_LOGS_DIR="${SFHOME}/grafana/promtail/monitored_logs"
RUN_DIR="${SFHOME}/run"

NUM_DAYS_SINCE_MODIFY=2
NUM_DAYS_TO_KEEP=90
NUM_DAYS_TO_KEEP_PROMTAIL_JSON=1
MIN_FILE_SIZE_TO_COMPRESS="$(stat --file-system ${JOBS_LOG_DIR} --printf=%s)"


multi_gzipped_files_number() {
    find "${JOBS_LOG_DIR}" -name "*.gz.gz" | wc -l
}

ungzip_logs_that_are_gzipped_multiple_times() {
    previous_multi_gzpipped_files_number="$(multi_gzipped_files_number)"

    if [ "${previous_multi_gzpipped_files_number}" = '0' ]; then
        return 0
    fi

    current_files_ungzipped=1   # initial value is a stub guard
    while [ "${current_files_ungzipped}" -gt 0 ]; do
        find "${JOBS_LOG_DIR}" -name "*.gz.gz" -exec gunzip {} \;  2>/dev/null
        current_multi_gzpipped_files_number=$(multi_gzipped_files_number)
        current_files_ungzipped=$((${previous_multi_gzpipped_files_number}-${current_multi_gzpipped_files_number}))
        previous_multi_gzpipped_files_number="${current_multi_gzpipped_files_number}"
    done
}

# NOTE: this command should be a noop, but unfortunately there was a bug introduced in compress_logs
# that compressed all logs, even if they were already compressed. So this should unscrew the problem.
ungzip_logs_that_are_gzipped_multiple_times

/opt/starfish/bin/compress_logs --logs-path "${JOBS_LOG_DIR}" --num-days-to-keep "${NUM_DAYS_TO_KEEP}" --promtail-jsons-path "${PROMTAIL_MONITORED_LOGS_DIR}" --num-days-to-keep-promtail-json "${NUM_DAYS_TO_KEEP_PROMTAIL_JSON}" --min-file-size-to-compress "${MIN_FILE_SIZE_TO_COMPRESS}" --num-days-since-modify "${NUM_DAYS_SINCE_MODIFY}" --run-path "${RUN_DIR}"
