#!/bin/sh
# @description read log of old agent runs
# @man This command will output historic logs of agent runs.
# @man +
# @man *Options*:
# @man +
# @man *-c*: show history without color output
# @man +
# @man *-n*: show maximum n lines of history
# @man +
# @man *-a*: show all available lines of history (long)
# @man +

. "${BASEDIR}/../lib/common.sh"
. "${BASEDIR}/../lib/cfengine_parsing.sh"

BASE_PATH="${RUDDER_VAR}/cfengine-community/outputs"
# Display 25 lines by default
max=25

while getopts "cn:" opt; do
  case $opt in
    c)
      clear_colors
      ;;
    n)
      max="${OPTARG}"
      ;;
    a)
      all="yes"
  esac
done

if [ "${all}" = "yes" ]
then
  list="cat"
else
  list="tail -n ${max}"
fi

for file in $(ls -1tr ${BASE_PATH}/cf* | ${list})
do
  endtime=$(perl -e'@a=stat $ARGV[0]; print $a[9]' "${file}")
  eval ${PRETTY} -v no_report=1 -v short_summary=1 -v endtime=${endtime} < "${file}"
done
