#!/bin/sh -e
#
# avoid using : in the filename as will break usage within
# $PATH etc., also in NFS automounts according to the Hickster.

usage() {
  printf '%s' "\
Usage: $(basename "$0") [-p] [-t]
Default format: YYYY-MM-DD

Options:
  -p    Path format: YYYY/MM/DD (default: YYYY-MM-DD)
  -t    Append time: -HHMMhSS (or /HHMMhSS if with -p)
"
  exit 1
}

# no match
if [ -z "$1" ]; then
  usage
fi

# equivalent to %F, the ISO 8601 date format
format="%Y-%m-%d"

while [ -n "$1" ]; do
  case "$1" in
    -p)
      format="%Y/%m/%d"
      path=y
      shift
      ;;
    -t)
      timestamp=y
      shift
      ;;
    *)
      usage
      ;;
  esac
done

if [ -n "${timestamp}" ]; then
  if [ -n "${path}" ]; then
    format="${format}/%H%Mh%Ss"
  else
    format="${format}-%H%Mh%Ss"
  fi
fi

date "+${format}"
