#!/bin/bash
# Add ' -x' above to debug
#

###########################################################################
# Configuration
###########################################################################
# Maximum age, in days, of log files to keep
MAX_LOGFILE_AGE=28
# Minimum size of live log before rotating, see find(1) -size for format
MIN_ROTATE_SIZE="100M"
###########################################################################

###########################################################################
# Helper functions
###########################################################################
##################################################
# Print program usage information.
##################################################
usage() {
  echo "Usage: $(basename $1) ( live | beta | dev ) [ --quiet ]" >&2
}
##################################################

##################################################
# Output depending on 'quiet' flag
##################################################
log() {
  if [ -z "${QUIET}" ];
  then
    echo $@
  fi
}
##################################################
###########################################################################

###########################################################################
# Check command line arguments
###########################################################################
EXIT_ARG_NO_ENV=1
EXIT_BAD_ARG=2
EXIT_NO_LOGS_DIR=3
EXIT_NO_CD_LOGS_DIR=4
EXIT_FAILED_COPY=5

EDDN_ENV="$1"
if [ -z "${EDDN_ENV}" ];
then
  usage $0
  exit ${EXIT_ARG_NO_ENV}
fi

if [ ! -z "${2}" ];
then
  if [ "${2}" == "--quiet" ];
  then
    QUIET="1"
  else
    usage
    exit ${EXIT_BAD_ARG}
  fi
fi
###########################################################################

###########################################################################
# Perform rotation
###########################################################################
LOGS_DIR="${HOME}/${EDDN_ENV}/logs"
if [ ! -d "${LOGS_DIR}" ];
then
  echo "$(dirname): Logs directory doesn't exist: ${LOGS_DIR}" >&2
  exit ${EXIT_NO_LOGS_DIR}
fi

cd ${LOGS_DIR} || exit ${EXIT_NO_CD_LOGS_DIR}

for service in gateway monitor relay ;
do
  log "Service: ${service}"
  log "         Expiring old logs..."
  find . -name "${service}.log.*.gz" -a -atime +${MAX_LOGFILE_AGE} -exec rm -f {} \;
  log "         DONE"

  log "         Checking if current logfile needs archiving..."
  if [ ! -z "$(find . -name ${service}.log -a -size +${MIN_ROTATE_SIZE})" ];
  then
    log "         Archiving ${service}.log ..."
    # We have no means to tell the service to close and re-open output, it's
    # to stdout/err anyway.  So we copy it.
    TIMESTAMP="$(date --iso-8601=seconds)"
    ARCHIVED_NAME="${service}.log.${TIMESTAMP}"
    cp ${service}.log "${ARCHIVED_NAME}"
    if [ $? -ne 0 ];
    then
      echo "         FAILED copying live log file to new archive!!!" >&2
      echo "         Exiting from any further processing." >&2
      exit ${EXIT_FAILED_COPY}
    fi
    # Truncate the live file.
    :> ${service}.log

    if [ "${service}" == "gateway" ];
    then
      # Produce a report of interesting errors
      ${HOME}/.local/bin/eddn-report-log-errors "${ARCHIVED_NAME}" > "${HOME}/reports/eddn-errors/by-log-rotation/eddn-errors-${TIMESTAMP}.txt"
    fi

    # Now compress the newly archived log
    gzip -9 "${ARCHIVED_NAME}"
    log "         DONE"
  else
    log "         No"
  fi
done
###########################################################################

# vim: tabstop=2 shiftwidth=2 expandtab wrapmargin=0 textwidth=0
