# INSTALL

checksum=aae7efee6ee243cc97e9678d6a34db3c42c299186be061816bc5b3cbe88c1618

_baseUrl="https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable"
_filename="google-chrome-stable_${VERSION%_*}-1_amd64.deb"
_distfile="${_baseUrl}/${_filename}"

case "$ACTION" in
post)
    # Actions to execute before the package files are unpacked.
    _builddir="$(mktemp -d "${TMPDIR:-/tmp}/$PKGNAME.XXXXXX")"
    mkdir -p "${_builddir}"

    xbps-uhelper fetch "${_distfile}>${_builddir}/${PKGNAME}.deb"

    # Verify that the archive is as expected
    if ! _digest="$(xbps-digest "${_builddir}/${PKGNAME}.deb")"; then
        echo "failed to compute checksum for file ${_filename}"
        exit 1
    fi

    if [ "${_digest}" != "${checksum}" ]; then
        echo "checksum mismatch for file ${_filename}"
	echo "  expected ${checksum}"
	echo "  found ${_digest}"
        exit 1
    fi

    # Attempt to unpack the archive
    (
        cd "${_builddir}" && ar x "${PKGNAME}.deb" && \
            tar xf data.tar.xz --wildcards './opt/google/chrome/WidevineCdm/'
    ) || exit 1

    # Remove previous components
    mkdir -p usr/lib/chromium
    rm -rf usr/lib/chromium/WidevineCdm
    rm -f usr/lib/chromium-dev/WidevineCdm usr/lib/chromium/libwidevinecdm.so

    # Install new components
    mv "${_builddir}/opt/google/chrome/WidevineCdm" usr/lib/chromium
    ln -Ts WidevineCdm/_platform_specific/linux_x64/libwidevinecdm.so usr/lib/chromium/libwidevinecdm.so

    mkdir -p usr/lib/chromium-dev
    ln -Ts ../chromium/WidevineCdm usr/lib/chromium-dev/WidevineCdm

    # Cleanup
    rm -r "${_builddir}"
    ;;
esac
