about summary refs log tree commit diff stats
path: root/pkgs/by-name/st/stamp/stamp.sh
blob: 0fced95692676c3e39c17552a688dba7467a4820 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env dash

# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH

help() {
    cat <<EOF
A simple file copyright management system.

USAGE:
    stamp [OPTIONS] FILES_OR_DIRECTORIES

OPTIONS:
    --help          | -h
                            Display this help and exit.

    --license L     | -l L
                            Specify a license.
                            This defaults to the GPL-3.0-or-later.

    --copyright C   | -c C
                            Specify a copyright string (e.g. name and email).
                            By default this is constructed from the git user.name and user.email
ARGUMENTS:
    FILES_OR_DIRECTORIES := [[ fd . --max-depth 3 ]]
                            Possible files to stamp.

    L                    := GPL-3.0-or-later|CC-BY-SA-4.0|AGPL-3.0-or-later
                            A possible license identifier. These above are only suggestions.

    C                    := [[ true ]]
                            This can be anything, which represents a copyright string.
                            Commenly that is a combination of a name and an email.
EOF
}

reuse_run() {
    root="$(git rev-parse --show-toplevel)"

    template=""
    if [ -e "$root/.reuse/templates/default.jinja2" ]; then
        template="--template=default"
    fi

    reuse annotate \
        --copyright="$copyright" \
        --copyright-prefix=string-c $template \
        --fallback-dot-license \
        --license="$license" -- "$1"
}

for arg in "$@"; do
    case "$arg" in
    "--help" | "-h")
        help
        exit 0
        ;;
    esac
done

[ "$(git rev-parse --is-inside-work-tree)" = "true" ] || die "You need to be in a git directory"

run=true
license="GPL-3.0-or-later"
copyright="$(git config --get user.name) <$(git config --get user.email)>"
while [ "$#" -gt 0 ] && [ "$run" = true ]; do
    case "$1" in
    "--license" | "-l")
        shift 1
        license="$1"
        [ "$license" ] || die "No license specified. See --help for usage."
        shift 1
        ;;
    "--copyright" | "-c")
        shift 1
        copyright="$1"
        [ "$copyright" ] || die "No copyright string specified. See --help for usage."
        shift 1
        ;;
    "--")
        run=false
        shift 1
        ;;
    -*)
        die "Unknown option: '$1' (if you intented this to be a path use '--' before it.)"
        ;;
    *)
        # We reached the end of the options.
        run=false
        ;;
    esac
done

for file in "$@"; do
    if [ -d "$file" ]; then
        fd . "$file" --type file --hidden | while read -r path_file; do
            reuse_run "$path_file"
        done
    else
        reuse_run "$file"
    fi
done

# vim: ft=sh