#!/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 "$@"