#!/bin/bash

BUCKET_NAME=$(cat /s3_bucket)
export BUCKET_NAME

EXPIRE_AGE_DAYS=$(cat /s3_expire_days)
export EXPIRE_AGE_DAYS

echo '======'
date

s3cmd --config=/s3_config ls s3://"${BUCKET_NAME}" | while read -r LINE;
	do
		CREATE_DATE=$(echo "$LINE" | awk '{print $1" "$2}')
		CREATE_DATE=$(date -d"$CREATE_DATE" +%s)
		OLDER_THAN=$(date -d"-${EXPIRE_AGE_DAYS} days" +%s)

		if [[ $CREATE_DATE -lt $OLDER_THAN ]]
			then
				FILE_NAME=$(echo "$LINE" | awk '{print $4}')
				echo "$FILE_NAME"
				if [[ $FILE_NAME != "" ]]
					then
						s3cmd --config=/s3_config del "$FILE_NAME"
				fi
		fi
	done;

echo '---'
