#!/bin/sh progname="fix-dates.sh" usage() { progname="$1" printf "Usage: %s [path-to-file [path-to-file [...]]]\n" \ "${progname}" } get_date() { file_path="$1" max_lines=$(wc -l markdown/* | \ sort -n | tail -n2 | head -n +1 | \ awk '{print $1}') date=$(grep -h -B "${max_lines}" '^---$' "${file_path}" | \ grep '^date:' | sed 's/^date: \?//') echo "${date}" } preview_file() { file_path="$1" date="$(get_date "${file_path}")" echo "${file_path}":"${date}" } fix_file() { file_path="$1" date="$(get_date "${file_path}")" new_date_format=$(date --date="${date}" '+%F %R') sed "s/${date}/${new_date_format}/g" -i "${file_path}" } if [ $# -lt 2 ] ; then usage "${progname}" exit 64 fi command="$1" shift 1 if [ "${command}" = "preview" ] ; then for file in $@ ; do preview_file "${file}" done elif [ "${command}" = "fix" ] ; then for file in $@ ; do fix_file "${file}" done else usage "${progname}" exit 64 fi