#!/bin/sh
set -e

bin_dir="$(cd "$(dirname "$0")" && pwd)"

# list Node licenses
if [ -f package.json ]; then
	if jq -e '.dependencies and (.dependencies | keys | length > 0)' package.json >/dev/null; then
		npx --yes license-checker --production --csv --excludePrivatePackages --customPath "${bin_dir}"/license-template-node.json | grep -v '^$'
		echo
	else
		echo "No dependencies found in package.json" >&2
		echo
	fi
fi

# list Go licenses
if [ -f go.mod ]; then
	# List all direct Go module dependencies, transform their paths to root module paths
	# (e.g., github.com/ory/x instead of github.com/ory/x/foo/bar), and generate a license report
	# for each unique root module. This ensures that the license report is generated for the root
	# module of a repository, where licenses are typically defined.
	go_modules=$(
		go list -f "{{if not .Indirect}}{{.Path}}{{end}}" -m ... |
			sort -u |
			awk -F/ '{ if ($1 == "github.com" && NF >= 3) { print $1"/"$2"/"$3 } else { print } }' |
			sort -u
	)
	if [ -z "$go_modules" ]; then
		echo "No Go modules found" >&2
	else
		# Workaround until https://github.com/google/go-licenses/issues/307 is fixed
		# .bin/go-licenses report "$module_name" --template .bin/license-template-go.tpl 2>/dev/null
		#
		echo "$go_modules" | xargs -I {} sh -c '.bin/go-licenses report --template .bin/license-template-go.tpl {}' | grep -v '^$'
		echo
	fi
fi
