#!/bin/sh # licenser: drop a little ol license in there # Author: Case Duckworth # 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 <"$__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 "$@"