#!/bin/sh

DAYS_THRESHOLD=14
TARGET_DIR="/var/log/ed-qmi/"

now_timestamp=$(date +%s)
cd "$TARGET_DIR" || exit 0

for logfile in *-*-*.log; do
    file_date=$(echo ${logfile} | grep -E -o '([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})')
    file_timestamp=$(date -d "$file_date" +%s)
    difference=$((now_timestamp - file_timestamp))
    # 1 day = 86400 second = 24 * 60 * 60
    days_old=$((difference / 86400))
    if [ "$days_old" -ge "$DAYS_THRESHOLD" ]; then
        # echo "rm -f ${TARGET_DIR}/${logfile}"
        rm -f "${TARGET_DIR}/${logfile}"
    fi
done
