#!/bin/sh
# SPDX-FileCopyrightText: 2020-2026  Jonas Smedegaard <dr@jones.dk
#
# SPDX-FileCopyrightText: 2020  Purism, SPC
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Description: helper script to update copyright_hints
#
# Depends:
#  licensecheck,
#  libimage-exiftool-perl,

set -eu

EXT_skip='ods zip'
EXT_meta='gif jpg pdf png'
#MERGE_LICENSES=1

ext2re() { perl -sE 'printf ".*\.(%s)", join "|", split " ", $e' -- -e="$1"; }
ext2opt() { perl -sE 'say map {" $o $_"} split " ", $e' -- -o="$1" -e="$2"; }

RE_skip=$(ext2re "$EXT_skip")
RE_meta=$(ext2re "$EXT_meta")
RE_debian='debian/(changelog|copyright(_hints)?|source/lintian-overrides)'
RE_hint='skip|meta'

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

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

echo 'extract metadata from binary files ...' 1>&2
exiftool '-textOut!' %d%f.%e:meta -short -short -recurse -extractEmbedded \
 $(ext2opt -ext "$EXT_meta") \
 -- ./*

# check for copyright and licensing statements
licensecheck --copyright --deb-machine --recursive --lines 0 --check '.*' \
 --ignore "^($RE_skip|$RE_meta|$RE_debian)$" ${MERGE_LICENSES:+--merge-licenses} \
 -- * > debian/copyright_hints

# strip hint suffix
sed -i -e 's/:meta$//' -e 's/:skip$//' debian/copyright_hints

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