From b12ca7147f34726a5849736a35864956fe09b574 Mon Sep 17 00:00:00 2001
From: Case Duckworth
Date: Sun, 10 May 2020 18:56:02 -0500
Subject: Update to v. 0.2

---
 licenser | 287 +++++++++++++++++++++++++++++----------------------------------
 1 file changed, 131 insertions(+), 156 deletions(-)

diff --git a/licenser b/licenser
index 9e921e1..7d710d3 100755
--- a/licenser
+++ b/licenser
@@ -1,28 +1,77 @@
-#!/usr/bin/env bash
-# licenser: do a license
+#!/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}"
 
-# defaults
-__DEFAULT_LICENSE=MIT
-: "${LICENSER_LICENSE:=$__DEFAULT_LICENSE}"
+usage() {
+	cat <<EOF
+$PRGN: plop a license in your project
+
+usage:	$PRGN [-h|-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
+	-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
+	_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 hlfy:a:o: OPT; do
+	while getopts hmfqy:a:e:o:l: OPT; do
 		case "$OPT" in
 		h)
 			usage
 			exit 0
 			;;
-		l)
+		f) _force=true ;;
+		m)
 			list_licenses
-			exit 0
+			exit "$?"
 			;;
-		f) _force=true ;;
-		y) __YEAR="$OPTARG-01-01" ;;
+		q) _quiet=true ;;
+		y) __YEAR="$OPTARG" ;;
 		a) __AUTHOR="$OPTARG" ;;
-		o) __OUTPUT="$OPTARG" ;;
+		e) __EMAIL="$OPTARG" ;;
+		o) __OUT="$OPTARG" ;;
+		l) __LANG="$OPTARG" ;;
 		*)
 			usage
 			exit 1
@@ -32,168 +81,94 @@ licenser() {
 	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:"
+	if ! __CACHED=$(get_license "$__LICENSE"); then
 		list_licenses
 		exit 2
 	fi
 
-	if [[ -e "$__OUTPUT" && ! $_force ]]; then
-		echo "File exists: $__OUTPUT"
+	if [ -e "$__OUT" ] && ! $_force; then
+		log "File exists: $__OUT"
 		exit 3
 	fi
 
-	"licenser_$__LICENSE" >"$__OUTPUT"
-}
+	eval "$(
+		put "cat<<_END_LICENSE_"
+		cat "$__CACHED"
+		put
+		put "_END_LICENSE_"
+	)" > "$__OUT"
 
-list_licenses() {
-	declare -F | awk '/licenser_/{sub(/licenser_/,"",$NF); print $NF;}'
+	log "$__LICENSE written to $__OUT."
 }
 
-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
+# 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"
 }
 
-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
+# download a list of the licenses, or get it from the cache
+list_licenses() {
+	cached="$LICENSER_CACHE/manifest"
+	source="$LICENSER_SOURCE/manifest"
+
+	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"
 }
 
-licenser_BSD0() {
-	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.
-
-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
+# template function for the copyright year
+year() {
+	case "$__YEAR" in
+	'') # figure out the year
+		date +%Y ;;
+	*) # one was provided
+		put "$__YEAR" ;;
+	esac
 }
 
-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
+# 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"
 }
 
-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
+email() {
+	if [ -z "$__EMAIL" ]; then
+		__EMAIL="$(git config --get user.email)" # try from git
+	fi
+	[ -n "$__EMAIL" ] && put "$__EMAIL"
 }
 
-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
-}
+# helpers
+put() { printf '%s\n' "$*"; }
+log() { $_quiet || put "$PRGN: $*" >&2; }
 
-# enable sourcing or executing
-if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
-	licenser "$@"
-fi
+# run
+$EXEC && licenser "$@"
-- 
cgit 1.4.1-21-gabe81