summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xlicenser287
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
9PRGN="${0##*/}"
10LICENSER_SOURCE="https://git.sr.ht/~acdw/licenser-licenses/blob/master"
11LICENSER_CACHE="${XDG_DATA_HOME:-$HOME/.local/share}/licenser"
12LICENSER_LICENSE="${LICENSER_LICENSE:-MIT}"
13EXEC="${EXEC:-true}"
3 14
4# defaults 15usage() {
5__DEFAULT_LICENSE=MIT 16 cat <<EOF
6: "${LICENSER_LICENSE:=$__DEFAULT_LICENSE}" 17$PRGN: plop a license in your project
18
19usage: $PRGN [-h|-m]
20 $PRGN [-f] [-q] [-y YEAR] [-a AUTHOR] [-e EMAIL]
21 [-l LANG] [-o FILE] [LICENSE]
22flags:
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
27options:
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
44parameters:
45 LICENSE which license to use.
46 default: \$LICENSER_LICENSE, which
47 defaults to MIT.
48EOF
49}
7 50
8# entry point 51# entry point
9licenser() { 52licenser() {
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
59list_licenses() { 101 log "$__LICENSE written to $__OUT."
60 declare -F | awk '/licenser_/{sub(/licenser_/,"",$NF); print $NF;}'
61} 102}
62 103
63usage() { 104# download a license, or get it from the cache
64 cat <<END 105# if there's not one, return error
65licenser: plop a license in there 106get_license() {
66 107 license="$1"
67usage: 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
83END
84} 121}
85 122
86year() { date +%Y --date="$__YEAR"; } 123# download a list of the licenses, or get it from the cache
87author() { printf '%s\n' "$__AUTHOR"; } 124list_licenses() {
88 125 cached="$LICENSER_CACHE/manifest"
89licenser_MIT() { 126 source="$LICENSER_SOURCE/manifest"
90 cat <<END 127
91Copyright $(year) $(author) 128 mkdir -p "$LICENSER_CACHE"
92 129 log "Checking cache for manifest..."
93Permission is hereby granted, free of charge, to any person obtaining a copy 130 if [ ! -f "$cached" ]; then
94of this software and associated documentation files (the "Software"), to deal 131 log "Not found. Downloading from $source..."
95in the Software without restriction, including without limitation the rights 132 if ! curl -fLo "$cached" "$LICENSER_SOURCE/manifest"; then
96to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 133 log "Unable to get the manifest"
97copies of the Software, and to permit persons to whom the Software is 134 return 1
98furnished to do so, subject to the following conditions: 135 fi
99 136 fi
100The above copyright notice and this permission notice shall be included in all 137 cat "$cached"
101copies or substantial portions of the Software.
102
103THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
104IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
105FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
106AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
107LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
108OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
109SOFTWARE.
110END
111} 138}
112 139
113licenser_BSD0() { 140# template function for the copyright year
114 cat <<END 141year() {
115Copyright $(year) $(author) 142 case "$__YEAR" in
116 143 '') # figure out the year
117Permission to use, copy, modify, and/or distribute this software for any 144 date +%Y ;;
118purpose with or without fee is hereby granted. 145 *) # one was provided
119 146 put "$__YEAR" ;;
120THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 147 esac
121REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
122AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
123INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
124LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
125OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
126PERFORMANCE OF THIS SOFTWARE.
127END
128} 148}
129 149
130licenser_BSD2() { 150# template function for author/owner
131 cat <<END 151author() {
132Copyright $(year) $(author) 152 if [ -z "$__AUTHOR" ]; then
133 153 __AUTHOR="$(git config --get user.name)" # try from git
134Redistribution and use in source and binary forms, with or without 154 [ -z "$__AUTHOR" ] && __AUTHOR="$(
135modification, 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
1371. Redistributions of source code must retain the above copyright notice, this 157 [ -z "$__AUTHOR" ] && __AUTHOR="$USER" # give up, get $USER
138list of conditions and the following disclaimer. 158 fi
139 159 put "$__AUTHOR"
1402. Redistributions in binary form must reproduce the above copyright notice,
141this list of conditions and the following disclaimer in the documentation
142and/or other materials provided with the distribution.
143
144THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
145AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
146IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
147DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
148FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
149DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
150SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
151CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
152LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
153OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
154SUCH DAMAGE.
155END
156} 160}
157 161
158licenser_ISC() { 162email() {
159 cat <<END 163 if [ -z "$__EMAIL" ]; then
160Copyright $(year) $(author) 164 __EMAIL="$(git config --get user.email)" # try from git
161 165 fi
162Permission to use, copy, modify, and/or distribute this software for any 166 [ -n "$__EMAIL" ] && put "$__EMAIL"
163purpose with or without fee is hereby granted, provided that the above
164copyright notice and this permission notice appear in all copies.
165
166THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
167REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
168AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
169INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
170LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
171OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
172PERFORMANCE OF THIS SOFTWARE.
173END
174} 167}
175 168
176licenser_WTFPL() { 169# helpers
177 cat <<END 170put() { printf '%s\n' "$*"; }
178Copyright $(year) $(author) and licensed under the terms of the 171log() { $_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.
193END
194}
195 172
196# enable sourcing or executing 173# run
197if [[ ${BASH_SOURCE[0]} == "$0" ]]; then 174$EXEC && licenser "$@"
198 licenser "$@"
199fi