#!/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 < 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