#!/bin/bash

BUCKET_NAME=$(cat /s3_bucket)
export BUCKET_NAME

EXPIRE_AGE_DAYS=$(cat /s3_expire_days)
export EXPIRE_AGE_DAYS

s4cmd --config=/s3_config ls s3://"${BUCKET_NAME}" | while read -r line;
  do
    createDate=$(echo "$line"|awk '{print $1" "$2}')
    createDate=$(date -d"$createDate" +%s)
    olderThan=$(date -d"-$EXPIRE_AGE_DAYS" +%s)
    if [[ $createDate -lt $olderThan ]]
      then
        fileName=$(echo "$line"|awk '{print $4}')
        echo "$fileName"
        if [[ $fileName != "" ]]
          then
            s4cmd --config=/s3_config del "$fileName"
        fi
    fi
  done;
