cfg/bin/mklicense
Knut Ahlers d18d43f6d6
Add more replacements, improve script UX
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-06-10 17:39:23 +02:00

43 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
set -euo pipefail
source ${HOME}/bin/script_framework.sh
[ -z "${1:-}" ] && {
LICENSES=$(curl -sSLf -H 'Accept: application/vnd.github.drax-preview+json' https://api.github.com/licenses | jq -r '.[] | .key' | sort | xargs)
info "Supported licenses: ${LICENSES}"
info "No license was choosen, falling back to apache-2.0"
echo
}
LICENSE=${1:-apache-2.0}
# Download LICENSE template for selected license
curl -sSLf -H 'Accept: application/vnd.github.drax-preview+json' \
https://api.github.com/licenses/${LICENSE} | jq -r '.body' >LICENSE
# Replace copyright stub with real data
NAME=$(git config --global user.name)
MAIL=$(git config --global user.email)
# Look for the first commit as the start year for the license
start_year=$(date +%Y)
first_commit=$(git rev-list --max-parents=0 HEAD 2>/dev/null || echo "")
[ -n "${first_commit}" ] && start_year=$(git log --format="%cd" --date="format:%Y" "${first_commit}")
case ${LICENSE} in
"apache-2.0")
sed -i "s/Copyright \[yyyy\] \[name of copyright owner\]/Copyright ${start_year}- ${NAME} <${MAIL}>/" LICENSE
;;
"bsd-2-clause") ;&
"bsd-3-clause")
sed -i "s/Copyright (c) \[year\], \[fullname\]/Copyright (c) ${start_year}-, ${NAME} <${MAIL}>/" LICENSE
;;
"mit")
sed -i "s/Copyright (c) \[year\] \[fullname\]/Copyright (c) ${start_year}- ${NAME} <${MAIL}>/" LICENSE
;;
esac
info "Generated license \"${LICENSE}\" with copyright information \"${start_year}- ${NAME} <${MAIL}>\" if applicable"