summaryrefslogtreecommitdiff
path: root/lists.sh
blob: c07870a86de88330940d0d0693e3716b353c79f0 (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
61
62
63
64
65
66
#!/usr/bin/env bash

shopt -s extglob nullglob

. ./getopts_long.sh

find_lists() {
    LISTS=()
    while read -r -d $'\0'; do
        local list=$(realpath --relative-to=. "$REPLY")
        LISTS+=("$list")
    done < <(find lists -maxdepth 1 -mindepth 1 -type d -not -name '.*' -print0 | sort -z)
}

find_posts() {
    local list="$1"
    shift 1

    POSTS=()
    for p in "$list"/!(*[!0-9]*); do
        POSTS+=( "$p" )
    done
}

print_lists() {
    find_lists
    for x in "${LISTS[@]}"; do
        printf "%s: %s\n" "$x" "$(<$x/title)"
    done
}

print_posts() {
    find_posts "$1"
    for p in "${POSTS[@]}"; do
        local post=$(readlink "$p")
        printf "%s: %s\n" "${p##*/}" "${post##*/}"
    done
}

add_list() {
    local list="$1"
    mkdir lists/"$list"
    shift 1
    echo "$@" >lists/"$list"/title
}

while getopts_long ":la:" opt \
                   posts required_argument \
                   "" "$@"
do
    case $opt in
        l)
            print_lists
            exit 0;;
        a)
            shift "$(($OPTLIND - 1))"
            add_list "$OPTLARG" "$@"
            exit 0;;
        posts)
            print_posts "$OPTLARG"
            ;;
        :)
            printf >&2 '%s: %s\n' "${0##*/}" "$OPTLERR"
            exit 1;;
    esac
done