summary refs log tree commit diff stats
path: root/licenser
blob: 2c9303f49c28e10f7dbf9bd7fe5cd524bb2acfd2 (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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash
# licenser: do a license

# defaults
__DEFAULT_LICENSE=MIT
: "${LICENSER_LICENSE:=$__DEFAULT_LICENSE}"

# entry point
licenser() {
	_force=false

	while getopts hlfy:a:o: OPT; do
		case "$OPT" in
		h)
			usage
			exit 0
			;;
		l)
			list_licenses
			exit 0
			;;
		f) _force=true ;;
		y) __YEAR="$OPTARG-01-01" ;;
		a) __AUTHOR="$OPTARG" ;;
		o) __OUTPUT="$OPTARG" ;;
		*)
			usage
			exit 1
			;;
		esac
	done
	shift $((OPTIND - 1))
	__LICENSE="${1:-$LICENSER_LICENSE}"

	# fill in blanks
	[[ -z "$__YEAR" ]] && __YEAR="$(date +%F)"
	[[ -z "$__AUTHOR" ]] && __AUTHOR="$(git config --get user.name)"
	[[ -z "$__AUTHOR" ]] && __AUTHOR="$(
		getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}'
	)"
	[[ -z "$__AUTHOR" ]] && __AUTHOR="$USER"
	[[ -z "$__OUTPUT" ]] && __OUTPUT=LICENSE

	if ! type "licenser_$__LICENSE" >/dev/null 2>&1; then
		echo "Unknown license: $__LICENSE"
		echo "Available licenses:"
		list_licenses
		exit 2
	fi

	if [[ -e "$__OUTPUT" && ! $_force ]]; then
		echo "File exists: $__OUTPUT"
		exit 3
	fi

	"licenser_$__LICENSE" >"$__OUTPUT"
}

list_licenses() {
	declare -F | awk '/licenser_/{sub(/licenser_/,"",$NF); print $NF;}'
}

usage() {
	cat <<END
licenser: plop a license in there

usage: licenser [-h|-l] [-y YEAR] [-a AUTHOR] [-o FILE] LICENSE

	-h		show this help and exit
	-l		list available licenses and exit
	-f		overwrite existing license
	-y YEAR		set the copyright date to YEAR
	-a AUTHOR	set the copyright holder to AUTHOR
	-o FILE		output to FILE
	LICENSE		which license to use

	YEAR defaults to the current year.
	AUTHOR is guessed by the following, in order:
		- git config --get user.name
		- getent passwd \$USER
		- \$USER
	FILE defaults to LICENSE in the current directory
END
}

year() { date +%Y --date="$__YEAR"; }
author() { printf '%s\n' "$__AUTHOR"; }

licenser_MIT() {
	cat <<END
Copyright $(year) $(author)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
END
}

licenser_BSD2() {
	cat <<END
Copyright $(year) $(author)

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
END
}

licenser_ISC() {
	cat <<END
Copyright $(year) $(author)

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
END
}

licenser_WTFPL() {
	cat <<END
Copyright $(year) $(author) and licensed under the terms of the

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                    Version 2, December 2004

 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO.
END
}

# enable sourcing or executing
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
	licenser "$@"
fi