#!/bin/bash
set -euo pipefail

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}")

# Copyright replacement for Apache 2.0
sed -i "s/Copyright \[yyyy\] \[name of copyright owner\]/Copyright ${start_year}- ${NAME} <${MAIL}>/" LICENSE

# Copyright replacement for MIT
sed -i "s/Copyright (c) \[year\] \[fullname\]/Copyright (c) ${start_year}- ${NAME} <${MAIL}>/" LICENSE