# The download URL/ filename does not change with version update. Need to check filenames inside downloaded ZIP file, or front webpage, for version.

_upstreamlistingurl='https://www.kyoceradocumentsolutions.eu/en/support/downloads.name-L2V1L2VuL21mcC9FQ09TWVNNNDEzMklETg==.html'
_upstreamsourcedownloadpath='https://www.kyoceradocumentsolutions.eu/content/dam/download-center-cf/eu/drivers/all'
_upstreamsourcefilename='SANE_Driver_zip.download.zip'
_get_pkgver() {
  local _ver='' _chunk

  while IFS= read -r -d '' _chunk || [[ -n $_chunk ]]; do
    if [[ $_chunk =~ SANE[[:space:]]Driver[[:space:]]\(([0-9.]+)\) ]]; then
      _ver="${BASH_REMATCH[1]}"
    fi
  done < <(curl -fsSL "${_upstreamlistingurl}")

  if [[ -z $_ver ]]; then
    printf '%s\n' 'Failed to determine upstream version.' >&2
    return 1
  fi

  printf '%s\n' "$_ver"
}

pkgname=kyocera-sane
pkgver="$(_get_pkgver)"
pkgrel=2
pkgdesc='Kyocera sane scanner support package for many Kyocera printers.'
arch=(
  'i386'
  'i486'
  'i586'
  'i686'
  'x86_64'
)
url='https://www.kyocera.com/'
license=('LicenseRef-proprietary')

install="${pkgname}.install"

options=('!debug')

source=(
  "${pkgname}-${pkgver}_SANE-Driver.zip::${_upstreamsourcedownloadpath}/${_upstreamsourcefilename}"
  'README.txt'
)
sha256sums=(
  '3076b7ae5e48564568ab7c660da3c77d6059b3fe96056f4df2a38544b579f8ab'
  '945f922065e8278d2ec6e0c1e8ce6851834359cd02ff85534fa3d7baccec6ffe'
)

prepare() {
  cd "${srcdir}"
  install -d build

  local _debarch

  case "${CARCH}" in
  i386 | i486 | i586 | i686)
    _debarch='i386'
    ;;
  x86_64)
    _debarch='amd64'
    ;;
  *)
    printf 'Unsupported architecture: %s\n' "${CARCH}" >&2
    return 1
    ;;
  esac

  bsdtar -xf "${pkgname}_${pkgver}_${_debarch}.deb"
  bsdtar -xf "${srcdir}/data.tar.xz" -C "${srcdir}/build"
}

build() {
  cd "${srcdir}/build"

  # From .deb postinst
  install -dvm755 "etc/sane.d/dll.d"
  printf '%s\n' \
    'kyocera' \
    'kyocera_gdi_a3' \
    'kyocera_wc3' \
    'kyocera_wc3_usb' \
    >"etc/sane.d/dll.d/kyocera.conf"

  chmod 644 "etc/sane.d"/*.conf
  chmod 644 "etc/sane.d/dll.d"/*.conf

  # Prevent usblp from claiming the USB printer interface.
  install -dvm755 "usr/lib/modprobe.d"
  printf '%s\n' \
    '# Kyocera SANE backend requires direct libusb access.' \
    'blacklist usblp' \
    >"usr/lib/modprobe.d/${pkgname}.conf"
  chmod 644 "usr/lib/modprobe.d/${pkgname}.conf"

  # Grant the active user access to Kyocera FS-1020MFP.
  install -dvm755 "usr/lib/udev/rules.d"
  printf '%s\n' \
    'ACTION!="remove", SUBSYSTEM=="usb", ATTRS{idVendor}=="0482", ATTRS{idProduct}=="0495", MODE="0660", TAG+="uaccess"' \
    >"usr/lib/udev/rules.d/71-${pkgname}.rules"
  chmod 644 "usr/lib/udev/rules.d/71-${pkgname}.rules"

  # The proprietary Kyocera backend creates temporary files directly
  # in this hardcoded directory (e.g. scannerdata.bin, raster_1.bmp).
  chmod 1777 "usr/local/kyocera/scanner"

  # Move the vendor UDEV rule to documentation because it grants
  # overly permissive permissions (0666) to all USB devices.
  mv -v \
    "etc/udev/rules.d/40-scanner-permissions.rules" \
    "usr/share/doc/${pkgname}/"
  chmod 644 \
    "usr/share/doc/${pkgname}/40-scanner-permissions.rules"
  rmdir -v "etc/udev/rules.d"

  # Move the license into place.
  install -Dvm644 \
    "usr/share/doc/${pkgname}/copyright" \
    "usr/share/licenses/${pkgname}/COPYRIGHT"
  rm "usr/share/doc/${pkgname}/copyright"

  # Clean unnecessary files.
  case "${CARCH}" in
  i386 | i486 | i586 | i686)
    rm -r "usr/lib/i386-linux-gnu"
    ;;
  x86_64)
    rm -r "usr/lib/x86_64-linux-gnu"
    ;;
  *)
    printf 'Unsupported architecture: %s\n' "${CARCH}" >&2
    return 1
    ;;
  esac
}

package() {
  cd "${srcdir}/build"

  # Move files into place.
  cp -rv * "${pkgdir}"/

  # Install documentation.
  install -Dvm644 \
    "${srcdir}/README.txt" \
    "${pkgdir}/usr/share/doc/${pkgname}/README.txt"
}
