#!/bin/bash
# Managed by puppet
# This file manages the atop binary files. It will keep binary files for last
# 7 days instead of 30 days provided by atop.
PATH=/sbin:/bin:/usr/sbin:/usr/bin

# remove files older than 7 days
find /var/log/atop -type f -name 'atop_*' -mtime +7 -delete

# remove atop_current if it is a broken symlink
test -L /var/log/atop/atop_current -a ! -e /var/log/atop/atop_current && \
  rm /var/log/atop/atop_current

# link current to most recent file if it exists
recent=$(find /var/log/atop/atop_* -type f | tail -1)
if [ -n "$recent" ]; then
  ln -f -s "$recent" /var/log/atop/atop_current
fi
