# Maintainer: David P. <megver83@parabola.nu>
# Maintainer (arch): David Runge <dvzrv@archlinux.org>
# Contributor: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Dan McGee <dan@archlinux.org>
# Contributor: bill-auger <bill-auger@programmer.net>


# Parabola changes and rationale:
# libre:
# - Patch the licenses list to mark more licenses as FSDG-compliant
# - Do not install (potentially) non-free licenses
# - Comment doubtful but possibly free licenses from standard_licenses
# technical:
# - retain the legacy license file locations as symlinks
#   this is most important currently for libretools <=20240707


_upstream_name=license-list-data
_upstream_version=3.24.0
pkgname=licenses
pkgver=20240728
pkgrel=1
pkgrel+=.parabola2
pkgdesc="A set of common license files"
arch=(any)
license=(LicenseRef-None)
url=https://www.parabola.nu/
makedepends=(
  jq
)
source=(
  $_upstream_name-$_upstream_version.tar.gz::https://github.com/spdx/$_upstream_name/archive/refs/tags/v$_upstream_version.tar.gz
)
sha512sums=('6eb81424e1b3173dbd8e17859b2e15dfe1c2af8f4da59b7dad0729c038a4cbdba95c34c2c546285fcdbbd41562e39575e6a6ebb9463b70121e0a12b99d1307e7')
b2sums=('b3b7e1d1b3610b079b133bd2d2682b9c8042a164a9cad8e43ee4f6fc3e6abe313285ea8b7ddce08789a574588691f6b1b67c3440a0d8526f5168d7ec8ca8cf41')

# parabola
source+=(known_libre_license_exceptions
         mark-unspecified-isFsfLibre-licenses.patch)
sha512sums+=('c92e9d038633d6f90a43b3be6f1adfd98e899ebea0237cd579822e9e0a032c02956fdf12361da5baac039c50159664f987a84799e3aa4d28b74a531d8036018d'
             '3745d20df91d1c2b1e2a161f594da4c7d62598cb5faa4518c9626221955f90cb3f62ca40f59d22dc3a9f5c2f64b296cb1bca63d2a2dbe16be748c8ee3e0131b3')
b2sums+=('8ef3ce46e490a0d45f55dab84252c7dc0bee43cbb16685ff5b063dfd700a9413387929ced195a99798c78b70a2db385cdbe0635bc84ffa261f3861fc7ba17674'
         'b124b04eb881b73afcb721465d1844e1e5ef6bfd24da7371b5b767284582872e2ec9efe782e374b3e1b0318ef74124150434478932fd26467ccae4d6e60b6802')


prepare() {
  local license
  touch known_spdx_license_exceptions.txt known_spdx_license_identifiers.txt

  # create list of known, non-deprecated SPDX license identifiers
  for license in $(jq -r '.licenses[] | select(.isDeprecatedLicenseId == false) | .licenseId' $_upstream_name-$_upstream_version/json/licenses.json); do
    printf "%s\n" "$license" >> known_spdx_license_identifiers.txt
  done

  # create list of known SPDX exception identifiers
  for license in $(jq -r '.exceptions[] | .licenseExceptionId' $_upstream_name-$_upstream_version/json/exceptions.json); do
    printf "%s\n" "$license" >> known_spdx_license_exceptions.txt
  done


  ## parabola patching ##

  # Set 'isFsfLibre' for some more known FSDG-compatible licenses in json/licenses.json.
  # The 'scrape-GNU-licenses.py' script can be used as a guide for maintaining this patch.
  # NOTE: SPDX 'FSFAP' is the 'GNU All-Permissive License (GNUAllPermissive)'
  #       The following licenses were removed from the initial patch
  #       because they are not present on the GNU License List.
  #       We should find a reference for these.
  #       * LGPLLR
  #       * libpng-2.0
  #       * libselinux-1.0
  #       * libtiff
  #       * libutil-David-Nugent
  echo "applying mark-unspecified-isFsfLibre-licenses.patch"
  patch -Np1 -i $srcdir/mark-unspecified-isFsfLibre-licenses.patch -d $_upstream_name-$_upstream_version

  # remove from the identifiers list, SPDX license identifiers of non-FSDG-fit licenses
  for license in $(jq -r '.licenses[] | select(.isDeprecatedLicenseId == false) | select(.isFsfLibre != true) | .licenseId' $_upstream_name-$_upstream_version/json/licenses.json); do
    sed -i "/^${license}$/d" ./known_spdx_license_identifiers.txt
  done

  # append to the identifiers list, SPDX license identifiers of other known FSDG-fit licenses
  for license in $(jq -r '.licenses[] | select(.isDeprecatedLicenseId == false) | select(.isFsfLibre == true) | .licenseId' $_upstream_name-$_upstream_version/json/licenses.json); do
    grep -Eq ^${license}$ ./known_spdx_license_identifiers.txt ||
    printf "%s\n" "$license" >> known_spdx_license_identifiers.txt
  done

  # remove from the exceptions list, SPDX license identifiers of non-FSDG-fit licenses
  for license in $(jq -r '.exceptions[] | .licenseExceptionId' $_upstream_name-$_upstream_version/json/exceptions.json); do
    grep -Eq ^${license}$ ./known_libre_license_exceptions ||
    sed -i "/^${license}$/d" ./known_spdx_license_exceptions.txt
  done
}

package() {
  local license
  local standard_licenses=(
    AGPL-3.0-only
    AGPL-3.0-or-later
    Apache-2.0
    Artistic-1.0-Perl
    Artistic-2.0
    BSL-1.0
    CC0-1.0
    CC-BY-1.0
    CC-BY-2.0
    CC-BY-2.5
    CC-BY-3.0-AT
    CC-BY-3.0
    CC-BY-3.0-US
    CC-BY-4.0
    CC-BY-NC-1.0
    CC-BY-NC-2.0
    CC-BY-NC-2.5
    CC-BY-NC-3.0
    CC-BY-NC-4.0
    CC-BY-NC-ND-1.0
    CC-BY-NC-ND-2.0
    CC-BY-NC-ND-2.5
    CC-BY-NC-ND-3.0-IGO
    CC-BY-NC-ND-3.0
    CC-BY-NC-ND-4.0
    CC-BY-NC-SA-1.0
    CC-BY-NC-SA-2.0
    CC-BY-NC-SA-2.5
    CC-BY-NC-SA-3.0
    CC-BY-NC-SA-4.0
    CC-BY-ND-1.0
    CC-BY-ND-2.0
    CC-BY-ND-2.5
    CC-BY-ND-3.0
    CC-BY-ND-4.0
    CC-BY-SA-1.0
    CC-BY-SA-2.0
    CC-BY-SA-2.0-UK
    CC-BY-SA-2.1-JP
    CC-BY-SA-2.5
    CC-BY-SA-3.0-AT
    CC-BY-SA-3.0
    CC-BY-SA-4.0
    CC-PDDC
    CDDL-1.0
    CDDL-1.1
    CPL-1.0
    EPL-1.0
    EPL-2.0
    FSFAP
    GFDL-1.1-invariants-only
    GFDL-1.1-invariants-or-later
    GFDL-1.1-no-invariants-only
    GFDL-1.1-no-invariants-or-later
    GFDL-1.1-only
    GFDL-1.1-or-later
    GFDL-1.2-invariants-only
    GFDL-1.2-invariants-or-later
    GFDL-1.2-no-invariants-only
    GFDL-1.2-no-invariants-or-later
    GFDL-1.2-only
    GFDL-1.2-or-later
    GFDL-1.3-invariants-only
    GFDL-1.3-invariants-or-later
    GFDL-1.3-no-invariants-only
    GFDL-1.3-no-invariants-or-later
    GFDL-1.3-only
    GFDL-1.3-or-later
    GPL-1.0-only
    GPL-1.0-or-later
    GPL-2.0-only
    GPL-2.0-or-later
    GPL-3.0-only
    GPL-3.0-or-later
    GPL-CC-1.0
    LGPL-2.0-only
    LGPL-2.0-or-later
    LGPL-2.1-only
    LGPL-2.1-or-later
    LGPL-3.0-only
    LGPL-3.0-or-later
    LGPLLR
    LPPL-1.0
    LPPL-1.1
    LPPL-1.2
    LPPL-1.3a
    LPPL-1.3c
    MPL-1.0
    MPL-1.1
    MPL-2.0
    PHP-3.01
    PHP-3.0
    PSF-2.0
    Ruby
    Unlicense
    WTFPL
    W3C
    ZPL-1.1
    ZPL-2.0
    ZPL-2.1
  )
  standard_licenses=( ${standard_licenses[*]/CC-BY-NC-*/} ) # non-free
  standard_licenses=( ${standard_licenses[*]/CC-BY-ND-*/} ) # non-free
  standard_licenses=( ${standard_licenses[*]/LPPL-1.0/}   ) # non-free
  standard_licenses=( ${standard_licenses[*]/LPPL-1.1/}   ) # non-free
  for i in "${!standard_licenses[@]}"; do
    [[ -n "${standard_licenses[$i]}" ]] || unset "standard_licenses[$i]"
  done
  local standard_exceptions=(
    GPL-3.0-linking-exception
    GPL-3.0-linking-source-exception
    LGPL-3.0-linking-exception
    LLGPL
    Linux-syscall-note
    MPL-2.0-no-copyleft-exception
  )

  for license in "${standard_licenses[@]}"; do
    install -vDm 644 "$_upstream_name-$_upstream_version/text/$license.txt" -t "$pkgdir/usr/share/licenses/spdx/"
  done
  for license in "${standard_exceptions[@]}"; do
    install -vDm 644 "$_upstream_name-$_upstream_version/text/$license.txt" -t "$pkgdir/usr/share/licenses/spdx/exceptions/"
  done

  install -vDm 644 known_spdx_license_identifiers.txt -t "$pkgdir/usr/share/licenses/"
  install -vDm 644 known_spdx_license_exceptions.txt -t "$pkgdir/usr/share/licenses/"

  # legacy support
  local known_licenses
  local legacy_dir="${pkgdir}"/usr/share/licenses/common
  mapfile -t known_licenses < known_spdx_license_identifiers.txt
  for license in "${known_licenses[@]}"; do
    install -dm755 "${legacy_dir}"/${license}
    ln -s ../../spdx/${license}.txt "${legacy_dir}"/${license}/${license}.txt
  done
}
