#!/bin/sh -e
#
# mdsize - print file size

# stat command
case "$(uname)" in
  Darwin)
    stat_cmd="gstat"
    ;;
  *)
    stat_cmd="stat"
    ;;
esac

# file to be verified
file="$1"

if [ -f "${file}" ]; then
  size=$(${stat_cmd} --format="%s" "${file}") # bytes
  units="B"

  if [ "${size}" -gt "1024" ]; then
    size=$((size / 1024)) # kilobytes
    units="KB"
  fi

  if [ "${size}" -gt "1024" ]; then
    size=$((size / 1024)) # megabytes
    units="MB"
  fi

  if [ "${size}" -gt "1024" ]; then
    size=$((size / 1024)) # gigabytes
    units="GB"
  fi

  if [ "${size}" -gt "1024" ]; then
    size=$((size / 1024)) # terabytes
    units="TB"
  fi

  echo "${file} size is ${size} ${units}"
else
  printf '%s\n' "Error: No input file provided or file does not exist."
  exit 1
fi
