#!/bin/sh
# Copyright 2020  Jonas Smedegaard <dr@jones.dk>
# Copyright 2020  Purism, SPC
# Description: helper script to update copyright_hints
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Depends:
#  licensecheck,
#  libimage-exiftool-perl,
#  libregexp-assemble-perl,
#  perl,

set -eu

SKIPFILES='skip|meta'

# cleanup stray hint files from a previous run
find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete

1>&2 echo 'skip binary files without parsable metadata ...'
RE_skip='.*\.(ico)'
find ./* -type f -regextype posix-egrep -regex "^($RE_skip)$" -exec sh -c 'echo "License: UNKNOWN" > {}:skip' ';'

1>&2 echo 'extract metadata from binary files ...'
RE_meta='.*\.(jpg|png)'
exiftool '-textOut!' %d%f.%e:meta -short -short -recurse -ext jpg -ext png .

RE_SKIP="$RE_skip|$RE_meta"

# left-truncated file wildcards
RE_rocksdb='Xrocksdb/.*'

# files undetected/misdetected by licensecheck
RX_isc_is_present=$(grep --files-with-matches --recursive --null-data --perl-regexp \
 --regexp='Permission\W+to\W+use\W+copy\W+modify\W+and/or\W+distribute\W+this\W+software\W+for\W+any\W+purpose\W+with\W+or\W+without\W+fee\W+is\W+hereby\W+granted\W+provided\W+that\W+the\W+above\W+copyright\W+notice\W+and\W+this\W+permission\W+notice\W+is\W+present\W+in\W+all\W+copies\.' \
 * | regexp-assemble -n)
RX_apache_and_gpl=$(grep --files-with-matches --recursive --null-data --perl-regexp \
 --regexp='This\W+source\W+code\W+is\W+licensed\W+under\W+both\W+the\W+GPLv2\W+\(found\W+in\W+the\W+COPYING\W+file\W+in\W+the\W+root\W+directory\)\W+and\W+Apache\W+2\.0\W+License\W+\(found\W+in\W+the\W+LICENSE\.Apache\W+file\W+in\W+the\W+root\W+directory\)\.' \
 * | regexp-assemble -n)

RX_COMMON="$RX_isc_is_present"

# TODO: automate more of this manual cleanup:
#  * strip garbage copyright holders
#  * optionally merge equally licensed Files sections
#  * do "sort -k2 -k1,1 -u" on copyright holders
#  * merge copyright years for each copyright holder
# TODO: strip files matching glob in current (only, no later) section
_licensecheck() {
	GLOB=$1
	shift
	case "$GLOB" in
		'*') 1>&2 echo "check default section(s) ...";;
		'') 1>&2 echo "check remaining upstream section(s) ...";;
		*) 1>&2 echo "check section(s) $GLOB ...";;
	esac
	licensecheck --copyright --deb-machine --recursive --lines 0 "$@" -- * \
		| GLOB=$GLOB SKIPFILES=$SKIPFILES perl -0777 -p \
		-e 'BEGIN { our $GLOB = join "\n ", split(" ",$ENV{GLOB}) }' \
		-e 's/^.*?\n\nFiles: \K/$GLOB\n /s if $GLOB;' \
		-e 's/^.*?\n\nFiles: \K.*?(?=\n\w)/$GLOB/s if $GLOB and $GLOB =~ /^[*]\//;' \
		-e 's/^.*?\n\n//s unless $GLOB and $GLOB =~ /^[*]$/m;' \
		-e 's/^Files:\K /\n /mg;' \
		-e 's/^Copyright:\K /\n  /mg;' \
		-e 's/(?:(?<=^  )|(?<=\d{4})),\K (?=\d{4})//mg;' \
		-e 's/:(?:$ENV{SKIPFILES})$//mg;' \
		>> debian/copyright_hints
}

rm -f debian/copyright_hints

# initially, check all to know roughly what to group and in which order
#rm -f debian/copyright_hints
#_licensecheck '' --check '.*' --ignore "^($RE_SKIP|$RE_meta|debian/.*)$"
#exit 0

# check default licensed files first, except within embedded projects
_licensecheck '*' --check "^$RX_COMMON$" --ignore "^($RE_SKIP|$RE_rocksdb|debian/.*)$"

# check generally
#  * omit non-copyright-protected Debian files
_licensecheck '?Apache_and_GPL2 Xrocksdb/*' --check "^$RX_apache_and_gpl$" --ignore "^$RX_COMMON|($RE_SKIP|debian/.*)$"
_licensecheck 'Xrocksdb/*' --check "^($RE_rocksdb)$" --ignore "^$RX_COMMON|$RX_apache_and_gpl|($RE_SKIP|debian/.*)$"
_licensecheck '' --check '.*' --ignore "^$RX_COMMON|$RX_apache_and_gpl|($RE_SKIP|$RE_rocksdb|debian/.*)$"
_licensecheck '*/debian' --check '^debian/' --ignore "^($RE_SKIP|debian/(changelog|copyright(_hints)?|source/lintian-overrides))$"

# cleanup hint files
find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete
