summaryrefslogtreecommitdiff
path: root/posts.sh
blob: 464def85555269616e9de6bb1f9b6e2cf1ada1f5 (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
#!/usr/bin/env bash

shopt -s extglob nullglob

add_to_list() {
		list=$(basename "$1")
		file=$(basename "$2")
    last_link=$(find ./lists/"$list" -regex '.*/[0-9]*$' -printf '%f\n' | sort | tail -n1)
    new_link=$(printf '%03d\n' $(($last_link + 1)))
		ln -s ../../posts/"$file" ./lists/"$list"/"$new_link"
}

new_post() {
    filename="$1"
    shift 1
    cat >posts/$filename <<EOF
% $@
EOF
		add_to_list zz_all "$filename"
}


. ./getopts_long.sh

while getopts_long ":n:a:" opt \
                   "" "$@"
do
    case $opt in
        n)
            shift "$(($OPTLIND - 1))"
            new_post "$OPTLARG" "$@"
            exit 0;;
				a)
						shift "$(($OPTLIND - 1))"
						for f in $@; do add_to_list "$OPTLARG" $f; done
						exit 0;;
        :)
            printf >&2 '%s: %s\n' "${0##*/}" "$OPTLERR"
            exit 1;;
    esac
done