From 4308699484e91053b471f64fdf29149087d8cd75 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 30 Jun 2022 17:28:12 -0500 Subject: Update to version 1.0 --- licensor | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100755 licensor (limited to 'licensor') diff --git a/licensor b/licensor new file mode 100755 index 0000000..47da938 --- /dev/null +++ b/licensor @@ -0,0 +1,251 @@ +#!/bin/sh + +# Constants +PRGN="${0##*/}" +LICENSOR_CACHE="${XDG_CACHE_HOME:-$HOME/.cache/licensor" +# Configuration +: "${LICENSE_REPO_VERSION:=3.17}" +: "${LICENSE_REPO_URL:=https://github.com/spdx/license-list-data/archive/refs/tags/v$LICENSE_REPO_VERSION.tar.gz}" +: "${LICENSE_REPO_PATH:=license-list-data-$LICENSE_REPO_VERSION/template}" +: "${LICENSOR_LICENSE:=MIT}" +: "${LICENSOR_OUTPUT_FILE:=COPYING}" + +usage() { + cat <"$__output" + [ "$__output" != /dev/stdout ] && log "$__license license written to $__output." +} + +get_licenses() { + # Get licenses from cache, or download them + if ! [ -d "$LICENSOR_CACHE" ] || [ "x$1" = "x-f" ]; then + log "Downlading licenses from $LICENSE_REPO_URL..." + mkdir -p "$LICENSOR_CACHE" + tmpfile="/tmp/licenses.tar.gz" + if ! [ -f "$tmpfile" ]; then + curl -o "$tmpfile" -L "$LICENSE_REPO_URL" >/dev/null 2>&1 || + return 1 + fi + log "Extracting licenses to $LICENSOR_CACHE..." + if tar -C "$LICENSOR_CACHE" \ + -xvf "$tmpfile" --strip-components=2 \ + "$LICENSE_REPO_PATH" >/dev/null 2>&1; then + rm "$tmpfile" + else + return 1 + fi + fi +} + +get_license() { + list_licenses | grep -iE '^'"$1"'$' || { + log "Can't find license \"$1\"." + exit 1 + } +} + +list_licenses() { + get_licenses "$1" || exit 1 + + find "$LICENSOR_CACHE" -iname '*.template.txt' | + sort | + xargs basename -s .template.txt +} + +search_licenses() { + list_licenses | grep -iE "$1" + exit $? +} + +guess_author() { + author="$(git config --get user.name)" + [ -z "$author" ] && + author="$(getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}')" + [ -z "$author" ] && + author="$USER" + put "$author" +} +guess_email() { + email="$(git config --get user.email)" + put "$email" +} + +put() { printf '%s\n' "$*"; } +log() { $_quiet || put "$PRGN: $*" >&2; } + +license_convert() { + copyright="$1" # "Copyright (C) 2022 Case Duckworth " + show_optional="${2:-0}" # 0 + fold_output="${3:-1}" # 1 + fold_width="${4:-70}" # 70 + awk ' +BEGIN { + foldOutput = '"$fold_output"' + foldWidth = '"$fold_width"' + showOptional = '"$show_optional"' + copyright = "'"$copyright"'" +} +{ buf = buf "\n" $0 } +END { + split(buf, b, "") + c = 1 + out = "" + optstr = "" + opt = 0 + while (c <= length(b)) { + if (b[c] == "<" && b[c + 1] == "<") { + tok = "" + c += 2 + closed = 0 + while (! closed) { + tok = tok b[c++] + if (b[c] == ">" && b[c + 1] == ">") { + closed++ + c += 2 + } + } + if (tok ~ /^beginOptional/) { + opt = 1 + } else if (tok == "endOptional") { + if (showOptional) { + out = out optstr + } + opt = 0 + optstr = "" + } else if (tok ~ /^var/) { + split(tok, ta, ";") + name = "" + original = "" + for (a in ta) { + if (ta[a] ~ "^name") { + match(ta[a], /=".*/) + name = substr(ta[a], RSTART + 2, RLENGTH - 3) + } else if (ta[a] ~ "^original") { + match(ta[a], /=".*/) + original = substr(ta[a], RSTART + 2, RLENGTH - 3) + } else if (ta[a] ~ "^match") { + # currently not used + } + } + if (name == "copyright") { + out = out copyright + } else { + out = out original + } + } + continue + } + if (opt) { + optstr = optstr b[c++] + } else { + out = out b[c++] + } + } + # "Fold" the output + if (foldOutput) { + split(out, oa, "\n") + out = "" + for (l in oa) { + split(oa[l], la, FS) + lc = 0 + for (w in la) { + lc += length(la[w]) + 1 + if (lc >= foldWidth) { + out = out "\n" la[w] " " + lc = length(la[w]) + 1 + } else { + out = out la[w] " " + } + } + out = out "\n" + } + } + print out +} +' +} + +main "$@" -- cgit 1.4.1-21-gabe81