diff options
Diffstat (limited to 'fix-dates.sh')
| -rwxr-xr-x | fix-dates.sh | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/fix-dates.sh b/fix-dates.sh new file mode 100755 index 0000000..74a5bd6 --- /dev/null +++ b/fix-dates.sh @@ -0,0 +1,60 @@ +#!/bin/sh + +progname="fix-dates.sh" + +usage() +{ + progname="$1" + printf "Usage: %s <preview|fix> [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 |
