#!/bin/sh -e
#
# mdtime_compare - compare file last-access and modification date
# with contrived date values

# stat command
stat_cmd=$(command -v gstat || command -v stat)
date_cmd=$(command -v gdate || command -v date)

# file to be verified
file="$1"

if [ -e "${file}" ]; then
  mod_time=$(${stat_cmd} --format="%Y" "${file}")
  acc_time=$(${stat_cmd} --format="%X" "${file}")

  # shellcheck disable=SC2312
  if [ "${mod_time}" -ge "$(${date_cmd} -d "00:00" +"%s")" ]; then
    echo "${file} was modified today"
  fi

  # shellcheck disable=SC2312
  if [ "${acc_time}" -ge "$(${date_cmd} -d "last day" +"%s")" ]; then
    echo "${file} was accessed at least yesterday"
  fi

  # shellcheck disable=SC2312
  if [ "${mod_time}" -ge "$(${date_cmd} -d "2014-11-09 19:00" +"%s")" ] \
    && [ "${mod_time}" -le "$(${date_cmd} -d "2014-11-09 20:00" +"%s")" ]; then
    echo "${file} was modified 2014-11-09 between 19:00 and 20:00"
  fi

  # shellcheck disable=SC2312
  if [ "${acc_time}" -lt "$(${date_cmd} -d "-3 days" +"%s")" ]; then
    echo "${file} was accessed more then three days ago"
  fi
else
  printf '%s\n' "Error: No input file provided or file does not exist."
fi
