aboutsummaryrefslogtreecommitdiffstats
path: root/fix-dates.sh
blob: 74a5bd6451d973cffc4652de4752afff73e068dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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