summary refs log tree commit diff stats
path: root/licenser
blob: 23374703e3f7e227397ab81a1a7566e56df74dea (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
#!/bin/sh
# licenser: drop a little ol license in there
# Author: Case Duckworth <acdw@acdw.net>
# License: MIT
# Version: 0.2

# Constants
## where to download licenses from
PRGN="${0##*/}"
: "${LICENSER_SOURCE:=https://git.sr.ht/~acdw/licenser-licenses/blob/master}"
LICENSER_CACHE="${XDG_DATA_HOME:-$HOME/.local/share}/licenser"
LICENSER_LICENSE="${LICENSER_LICENSE:-MIT}"
EXEC="${EXEC:-true}"

usage() {
	cat <<EOF
$PRGN: plop a license in your project

usage:	$PRGN [-h|-m|-M]
	$PRGN [-f] [-q] [-y YEAR] [-a AUTHOR] [-e EMAIL] 
	      [-l LANG] [-o FILE] [LICENSE]
flags:
	-h	show this help and exit
	-m	list available licenses and exit
	-M	list licenses, disregarding cached manifest
	-f	overwrite existing license
	-q	quiet: don't log anything
options:
	-y YEAR		set copyright date(s) to YEAR.
			default: \$(date +%Y).
			this option accepts arbitrary input.
	-a AUTHOR	set copyright holder(s) to AUTHOR.
			default:
				- git config --get user.name
				- getent password \$USER
				- \$USER
	-e EMAIL	set AUTHOR's email address to EMAIL.
			default:
				- git config --get user.email
				- [blank]
	-l LANG		set license language to LANG.
			default: 'en' (subject to change)
	-o FILE		set output file to FILE.
			default: LICENSE in \$PWD
parameters:
	LICENSE		which license to use.
			default: \$LICENSER_LICENSE, which
			defaults to MIT.
EOF
}

# entry point
licenser() {
	_force=false  # don't replace licenses
	_quiet=false  # log stuff
	__OUT=LICENSE # default output file
	__LANG=en     # default to en because it's got the most licenses XXX

	while getopts hmMfqy:a:e:o:l:s: OPT; do
		case "$OPT" in
		h)
			usage
			exit 0
			;;
		f) _force=true ;;
		m)
			list_licenses
			exit "$?"
			;;
		M)
			list_licenses -f
			exit "$?"
			;;
		q) _quiet=true ;;
		y) __YEAR="$OPTARG" ;;
		a) __AUTHOR="$OPTARG" ;;
		e) __EMAIL="$OPTARG" ;;
		o) __OUT="$OPTARG" ;;
		l) __LANG="$OPTARG" ;;
		*)
			usage
			exit 1
			;;
		esac
	done
	shift $((OPTIND - 1))
	__LICENSE="${1:-$LICENSER_LICENSE}"

	if ! __CACHED=$(get_license "$__LICENSE"); then
		list_licenses
		exit 2
	fi

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

	eval "$(
		put "cat<<_END_LICENSE_"
		cat "$__CACHED"
		put
		put "_END_LICENSE_"
	)" >"$__OUT"

	log "$__LICENSE written to $__OUT."
}

# download a license, or get it from the cache
# if there's not one, return error
get_license() {
	license="$1"
	cached="$LICENSER_CACHE/$license/$__LANG"
	source="$LICENSER_SOURCE/$license/$__LANG"

	mkdir -p "$LICENSER_CACHE/$license"
	log "Checking cache for $license..."
	if [ ! -f "$cached" ]; then
		log "Not found. Downloading from $source..."
		if ! curl -fLo "$cached" "$source"; then
			log "License $license unavailable in language: $__LANG"
			return 1
		fi
	fi
	put "$cached"
}

# download a list of the licenses, or get it from the cache
list_licenses() {
	cached="$LICENSER_CACHE/manifest"
	source="$LICENSER_SOURCE/manifest"

	[ "x$1" = "x-r" ] && rm "$cached"

	mkdir -p "$LICENSER_CACHE"
	log "Checking cache for manifest..."
	if [ ! -f "$cached" ]; then
		log "Not found. Downloading from $source..."
		if ! curl -fLo "$cached" "$LICENSER_SOURCE/manifest"; then
			log "Unable to get the manifest"
			return 1
		fi
	fi
	cat "$cached"
}

# template function for the copyright year
year() {
	case "$__YEAR" in
	'') # figure out the year
		date +%Y ;;
	*) # one was provided
		put "$__YEAR" ;;
	esac
}

# template function for author/owner
author() {
	if [ -z "$__AUTHOR" ]; then
		__AUTHOR="$(git config --get user.name)" # try from git
		[ -z "$__AUTHOR" ] && __AUTHOR="$(
			getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}'
		)"                                     # or, try from getent
		[ -z "$__AUTHOR" ] && __AUTHOR="$USER" # give up, get $USER
	fi
	put "$__AUTHOR"
}

email() {
	if [ -z "$__EMAIL" ]; then
		__EMAIL="$(git config --get user.email)" # try from git
	fi
	[ -n "$__EMAIL" ] && put "<$__EMAIL>"
}

# helpers
put() { printf '%s\n' "$*"; }
log() { $_quiet || put "$PRGN: $*" >&2; }

# run
$EXEC && licenser "$@"