diff options
Diffstat (limited to 'licenser')
-rwxr-xr-x | licenser | 287 |
1 files changed, 131 insertions, 156 deletions
diff --git a/licenser b/licenser index 9e921e1..7d710d3 100755 --- a/licenser +++ b/licenser | |||
@@ -1,28 +1,77 @@ | |||
1 | #!/usr/bin/env bash | 1 | #!/bin/sh |
2 | # licenser: do a license | 2 | # licenser: drop a little ol license in there |
3 | # Author: Case Duckworth <acdw@acdw.net> | ||
4 | # License: MIT | ||
5 | # Version: 0.2 | ||
6 | |||
7 | # Constants | ||
8 | ## where to download licenses from | ||
9 | PRGN="${0##*/}" | ||
10 | LICENSER_SOURCE="https://git.sr.ht/~acdw/licenser-licenses/blob/master" | ||
11 | LICENSER_CACHE="${XDG_DATA_HOME:-$HOME/.local/share}/licenser" | ||
12 | LICENSER_LICENSE="${LICENSER_LICENSE:-MIT}" | ||
13 | EXEC="${EXEC:-true}" | ||
3 | 14 | ||
4 | # defaults | 15 | usage() { |
5 | __DEFAULT_LICENSE=MIT | 16 | cat <<EOF |
6 | : "${LICENSER_LICENSE:=$__DEFAULT_LICENSE}" | 17 | $PRGN: plop a license in your project |
18 | |||
19 | usage: $PRGN [-h|-m] | ||
20 | $PRGN [-f] [-q] [-y YEAR] [-a AUTHOR] [-e EMAIL] | ||
21 | [-l LANG] [-o FILE] [LICENSE] | ||
22 | flags: | ||
23 | -h show this help and exit | ||
24 | -m list available licenses and exit | ||
25 | -f overwrite existing license | ||
26 | -q quiet: don't log anything | ||
27 | options: | ||
28 | -y YEAR set copyright date(s) to YEAR. | ||
29 | default: \$(date +%Y). | ||
30 | this option accepts arbitrary input. | ||
31 | -a AUTHOR set copyright holder(s) to AUTHOR. | ||
32 | default: | ||
33 | - git config --get user.name | ||
34 | - getent password \$USER | ||
35 | - \$USER | ||
36 | -e EMAIL set AUTHOR's email address to EMAIL. | ||
37 | default: | ||
38 | - git config --get user.email | ||
39 | - [blank] | ||
40 | -l LANG set license language to LANG. | ||
41 | default: 'en' (subject to change) | ||
42 | -o FILE set output file to FILE. | ||
43 | default: LICENSE in \$PWD | ||
44 | parameters: | ||
45 | LICENSE which license to use. | ||
46 | default: \$LICENSER_LICENSE, which | ||
47 | defaults to MIT. | ||
48 | EOF | ||
49 | } | ||
7 | 50 | ||
8 | # entry point | 51 | # entry point |
9 | licenser() { | 52 | licenser() { |
10 | _force=false | 53 | _force=false # don't replace licenses |
54 | _quiet=false # log stuff | ||
55 | __OUT=LICENSE # default output file | ||
56 | __LANG=en # default to en because it's got the most licenses XXX | ||
11 | 57 | ||
12 | while getopts hlfy:a:o: OPT; do | 58 | while getopts hmfqy:a:e:o:l: OPT; do |
13 | case "$OPT" in | 59 | case "$OPT" in |
14 | h) | 60 | h) |
15 | usage | 61 | usage |
16 | exit 0 | 62 | exit 0 |
17 | ;; | 63 | ;; |
18 | l) | 64 | f) _force=true ;; |
65 | m) | ||
19 | list_licenses | 66 | list_licenses |
20 | exit 0 | 67 | exit "$?" |
21 | ;; | 68 | ;; |
22 | f) _force=true ;; | 69 | q) _quiet=true ;; |
23 | y) __YEAR="$OPTARG-01-01" ;; | 70 | y) __YEAR="$OPTARG" ;; |
24 | a) __AUTHOR="$OPTARG" ;; | 71 | a) __AUTHOR="$OPTARG" ;; |
25 | o) __OUTPUT="$OPTARG" ;; | 72 | e) __EMAIL="$OPTARG" ;; |
73 | o) __OUT="$OPTARG" ;; | ||
74 | l) __LANG="$OPTARG" ;; | ||
26 | *) | 75 | *) |
27 | usage | 76 | usage |
28 | exit 1 | 77 | exit 1 |
@@ -32,168 +81,94 @@ licenser() { | |||
32 | shift $((OPTIND - 1)) | 81 | shift $((OPTIND - 1)) |
33 | __LICENSE="${1:-$LICENSER_LICENSE}" | 82 | __LICENSE="${1:-$LICENSER_LICENSE}" |
34 | 83 | ||
35 | # fill in blanks | 84 | if ! __CACHED=$(get_license "$__LICENSE"); then |
36 | [[ -z "$__YEAR" ]] && __YEAR="$(date +%F)" | ||
37 | [[ -z "$__AUTHOR" ]] && __AUTHOR="$(git config --get user.name)" | ||
38 | [[ -z "$__AUTHOR" ]] && __AUTHOR="$( | ||
39 | getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}' | ||
40 | )" | ||
41 | [[ -z "$__AUTHOR" ]] && __AUTHOR="$USER" | ||
42 | [[ -z "$__OUTPUT" ]] && __OUTPUT=LICENSE | ||
43 | |||
44 | if ! type "licenser_$__LICENSE" >/dev/null 2>&1; then | ||
45 | echo "Unknown license: $__LICENSE" | ||
46 | echo "Available licenses:" | ||
47 | list_licenses | 85 | list_licenses |
48 | exit 2 | 86 | exit 2 |
49 | fi | 87 | fi |
50 | 88 | ||
51 | if [[ -e "$__OUTPUT" && ! $_force ]]; then | 89 | if [ -e "$__OUT" ] && ! $_force; then |
52 | echo "File exists: $__OUTPUT" | 90 | log "File exists: $__OUT" |
53 | exit 3 | 91 | exit 3 |
54 | fi | 92 | fi |
55 | 93 | ||
56 | "licenser_$__LICENSE" >"$__OUTPUT" | 94 | eval "$( |
57 | } | 95 | put "cat<<_END_LICENSE_" |
96 | cat "$__CACHED" | ||
97 | put | ||
98 | put "_END_LICENSE_" | ||
99 | )" > "$__OUT" | ||
58 | 100 | ||
59 | list_licenses() { | 101 | log "$__LICENSE written to $__OUT." |
60 | declare -F | awk '/licenser_/{sub(/licenser_/,"",$NF); print $NF;}' | ||
61 | } | 102 | } |
62 | 103 | ||
63 | usage() { | 104 | # download a license, or get it from the cache |
64 | cat <<END | 105 | # if there's not one, return error |
65 | licenser: plop a license in there | 106 | get_license() { |
66 | 107 | license="$1" | |
67 | usage: licenser [-h|-l] [-y YEAR] [-a AUTHOR] [-o FILE] LICENSE | 108 | cached="$LICENSER_CACHE/$license/$__LANG" |
68 | 109 | source="$LICENSER_SOURCE/$license/$__LANG" | |
69 | -h show this help and exit | 110 | |
70 | -l list available licenses and exit | 111 | mkdir -p "$LICENSER_CACHE/$license" |
71 | -f overwrite existing license | 112 | log "Checking cache for $license..." |
72 | -y YEAR set the copyright date to YEAR | 113 | if [ ! -f "$cached" ]; then |
73 | -a AUTHOR set the copyright holder to AUTHOR | 114 | log "Not found. Downloading from $source..." |
74 | -o FILE output to FILE | 115 | if ! curl -fLo "$cached" "$source"; then |
75 | LICENSE which license to use | 116 | log "License $license unavailable in language: $__LANG" |
76 | 117 | return 1 | |
77 | YEAR defaults to the current year. | 118 | fi |
78 | AUTHOR is guessed by the following, in order: | 119 | fi |
79 | - git config --get user.name | 120 | put "$cached" |
80 | - getent passwd \$USER | ||
81 | - \$USER | ||
82 | FILE defaults to LICENSE in the current directory | ||
83 | END | ||
84 | } | 121 | } |
85 | 122 | ||
86 | year() { date +%Y --date="$__YEAR"; } | 123 | # download a list of the licenses, or get it from the cache |
87 | author() { printf '%s\n' "$__AUTHOR"; } | 124 | list_licenses() { |
88 | 125 | cached="$LICENSER_CACHE/manifest" | |
89 | licenser_MIT() { | 126 | source="$LICENSER_SOURCE/manifest" |
90 | cat <<END | 127 | |
91 | Copyright $(year) $(author) | 128 | mkdir -p "$LICENSER_CACHE" |
92 | 129 | log "Checking cache for manifest..." | |
93 | Permission is hereby granted, free of charge, to any person obtaining a copy | 130 | if [ ! -f "$cached" ]; then |
94 | of this software and associated documentation files (the "Software"), to deal | 131 | log "Not found. Downloading from $source..." |
95 | in the Software without restriction, including without limitation the rights | 132 | if ! curl -fLo "$cached" "$LICENSER_SOURCE/manifest"; then |
96 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 133 | log "Unable to get the manifest" |
97 | copies of the Software, and to permit persons to whom the Software is | 134 | return 1 |
98 | furnished to do so, subject to the following conditions: | 135 | fi |
99 | 136 | fi | |
100 | The above copyright notice and this permission notice shall be included in all | 137 | cat "$cached" |
101 | copies or substantial portions of the Software. | ||
102 | |||
103 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
104 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
105 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
106 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
107 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
108 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
109 | SOFTWARE. | ||
110 | END | ||
111 | } | 138 | } |
112 | 139 | ||
113 | licenser_BSD0() { | 140 | # template function for the copyright year |
114 | cat <<END | 141 | year() { |
115 | Copyright $(year) $(author) | 142 | case "$__YEAR" in |
116 | 143 | '') # figure out the year | |
117 | Permission to use, copy, modify, and/or distribute this software for any | 144 | date +%Y ;; |
118 | purpose with or without fee is hereby granted. | 145 | *) # one was provided |
119 | 146 | put "$__YEAR" ;; | |
120 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | 147 | esac |
121 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
122 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
123 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
124 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
125 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
126 | PERFORMANCE OF THIS SOFTWARE. | ||
127 | END | ||
128 | } | 148 | } |
129 | 149 | ||
130 | licenser_BSD2() { | 150 | # template function for author/owner |
131 | cat <<END | 151 | author() { |
132 | Copyright $(year) $(author) | 152 | if [ -z "$__AUTHOR" ]; then |
133 | 153 | __AUTHOR="$(git config --get user.name)" # try from git | |
134 | Redistribution and use in source and binary forms, with or without | 154 | [ -z "$__AUTHOR" ] && __AUTHOR="$( |
135 | modification, are permitted provided that the following conditions are met: | 155 | getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}' |
136 | 156 | )" # or, try from getent | |
137 | 1. Redistributions of source code must retain the above copyright notice, this | 157 | [ -z "$__AUTHOR" ] && __AUTHOR="$USER" # give up, get $USER |
138 | list of conditions and the following disclaimer. | 158 | fi |
139 | 159 | put "$__AUTHOR" | |
140 | 2. Redistributions in binary form must reproduce the above copyright notice, | ||
141 | this list of conditions and the following disclaimer in the documentation | ||
142 | and/or other materials provided with the distribution. | ||
143 | |||
144 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
145 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
146 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
147 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
148 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
149 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
150 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
151 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
152 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
153 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
154 | SUCH DAMAGE. | ||
155 | END | ||
156 | } | 160 | } |
157 | 161 | ||
158 | licenser_ISC() { | 162 | email() { |
159 | cat <<END | 163 | if [ -z "$__EMAIL" ]; then |
160 | Copyright $(year) $(author) | 164 | __EMAIL="$(git config --get user.email)" # try from git |
161 | 165 | fi | |
162 | Permission to use, copy, modify, and/or distribute this software for any | 166 | [ -n "$__EMAIL" ] && put "$__EMAIL" |
163 | purpose with or without fee is hereby granted, provided that the above | ||
164 | copyright notice and this permission notice appear in all copies. | ||
165 | |||
166 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
167 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
168 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
169 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
170 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
171 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
172 | PERFORMANCE OF THIS SOFTWARE. | ||
173 | END | ||
174 | } | 167 | } |
175 | 168 | ||
176 | licenser_WTFPL() { | 169 | # helpers |
177 | cat <<END | 170 | put() { printf '%s\n' "$*"; } |
178 | Copyright $(year) $(author) and licensed under the terms of the | 171 | log() { $_quiet || put "$PRGN: $*" >&2; } |
179 | |||
180 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
181 | Version 2, December 2004 | ||
182 | |||
183 | Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> | ||
184 | |||
185 | Everyone is permitted to copy and distribute verbatim or modified | ||
186 | copies of this license document, and changing it is allowed as long | ||
187 | as the name is changed. | ||
188 | |||
189 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
190 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
191 | |||
192 | 0. You just DO WHAT THE FUCK YOU WANT TO. | ||
193 | END | ||
194 | } | ||
195 | 172 | ||
196 | # enable sourcing or executing | 173 | # run |
197 | if [[ ${BASH_SOURCE[0]} == "$0" ]]; then | 174 | $EXEC && licenser "$@" |
198 | licenser "$@" | ||
199 | fi | ||