#!/bin/sh
# $Id: chromium-widevine-plugin.SlackBuild,v 1.67 2025/11/22 20:44:58 root Exp root $
# Copyright 2014, 2015, 2016, 2017, 2019, 2021, 2022, 2023, 2024, 2025, 2026  Eric Hameleers, Eindhoven, NL
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:          Eric Hameleers <alien@slackware.com>
# For:         chromium-widevine-plugin
# Descr:       Google Chrome's Pepper Widevine plugin for Chromium
# URL:         http://www.google.com/chrome
# Build needs: 
# Needs:       
# Changelog:   
# 1.4.4.637-1: 31/aug/2014 by Eric Hameleers <alien@slackware.com>
#              * Initial release.
# 39.0.2171.95-1:
#              01/jan/2015 by Eric Hameleers <alien@slackware.com>
#              * Update and switch to using the chromium version.
# 40.0.2214.91-1:
#              22/jan/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 40.0.2214.111-1:
#              06/feb/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 41.0.2272.76-1:
#              06/mar/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 41.0.2272.101-1:
#              21/mar/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 41.0.2272.118-1:
#              02/apr/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 42.0.2311.90-1:
#              15/apr/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 42.0.2311.135-1:
#              30/apr/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.65-1:
#              20/may/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.81-1:
#              29/may/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.124-1:
#              11/jun/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.125-1:
#              12/jun/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.130-1:
#              23/jun/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.132-1:
#              08/jul/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 43.0.2357.134-1:
#              15/jul/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 44.0.2403.89-1:
#              22/jul/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 44.0.2403.107-1:
#              25/jul/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 44.0.2403.155-1:
#              12/aug/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 44.0.2403.157-1:
#              21/aug/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 45.0.2454.85-1:
#              03/sep/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 45.0.2454.93-1:
#              17/sep/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 45.0.2454.101-1:
#              25/sep/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 46.0.2490.71-1:
#              14/oct/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 47.0.2526.73-1:
#              04/dec/2015 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 48.0.2564.82-1:
#              24/jan/2016 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 48.0.2564.97-1:
#              31/jan/2016 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 49.0.2623.75-1:
#              06/mar/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 49.0.2623.108-1:
#              26/mar/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 50.0.2661.75-1:
#              16/apr/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 50.0.2661.94-1:
#              29/apr/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 51.0.2704.63-1:
#              26/may/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 51.0.2704.84-1:
#              08/jun/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 52.0.2743.82-1:
#              24/jul/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit). Added the version of the last 32bit
#                plugin as well.
# 52.0.2743.116-1:
#              05/aug/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 53.0.2785.92-1:
#              07/sep/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 54.0.2840.90-1:
#              03/nov/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 54.0.2840.100-1:
#              18/nov/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 55.0.2883.75-1:
#              12/dec/2016 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 56.0.2924.76-1:
#              31/jan/2017 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 57.0.2987.98-1:
#              15/mar/2017 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 58.0.3029.96-1:
#              09/may/2017 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 59.0.3071.86-1:
#              07/jun/2017 by Eric Hameleers <alien@slackware.com>
#              * Update (only 64bit).
# 1.4.8.1004-1:
#              10/jul/2017 by Eric Hameleers <alien@slackware.com>
#              * Start using the widevine version number now that we use
#                a direct download, no longer extracting from Chrome binaries.
# 1.4.8.1010-1:
#              28/jul/2017 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 1.4.8.1008-1:
#              01/nov/2017 by Eric Hameleers <alien@slackware.com>
#              * Update (downgrade apparently).
#  1.4.9.1088-1:
#              01/aug/2018 by Eric Hameleers <alien@slackware.com>
#              * Update required for Chromium 68.
#  4.10.1440.19-1:
#              14/sep/2019 by Eric Hameleers <alien@slackware.com>
#              * Update required for HBO and Amazon streams.
#  4.10.1440.19-2:
#              30/oct/2019 by Eric Hameleers <alien@slackware.com>
#              * Chromium 78 and up expect the CDM library in a
#                different location.
#                See https://chromium-review.googlesource.com/c/chromium/src/+/1808309
#  4.10.1582.1-1:
#              10/nov/2019 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.1582.2-1:
#              15/nov/2019 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.1582.2-2:
#              03/mar/2021 by Eric Hameleers <alien@slackware.com>
#              * Add support for chromium-ungoogled.
#  4.10.1582.2-3:
#              15/mar/2021 by Eric Hameleers <alien@slackware.com>
#              * Add legacy lib location support for chromium-ungoogled.
#  4.10.2209.1-1:
#              18/apr/2021 by Eric Hameleers <alien@slackware.com>
#              * Update, long time in the making.
#  4.10.2391.0-1:
#              13/jan/2022 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2449.0-1:
#              10/apr/2022 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2557.0-1:
#              10/dec/2022 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2662.3-1:
#              13/aug/2023 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2710.0-1:
#              24/jan/2024 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2830.0-1:
#              09/sep/2024 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2891.0-1:
#              13/may/2025 by Eric Hameleers <alien@slackware.com>
#              * Update.
#  4.10.2934.0-1:
#              27/feb/2026 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 
# Run 'sh chromium-widevine-plugin.SlackBuild' to build a Slackware package.
# The package (.txz) and .txt file as well as build logs are created in /tmp .
# Install it using 'installpkg'. 
#
# -----------------------------------------------------------------------------


SRCNAM="chromium"
SRCEXT=""        # "" is stable, "-dev" is development
PRGNAM="${SRCNAM}${SRCEXT}-widevine-plugin"

VERSION=${VERSION:-"4.10.2934.0"}
BUILD=${BUILD:-1}
TAG=${TAG:-alien}

# Where do we look for sources?
SRCDIR=$(cd $(dirname $0); pwd)

DOCS="LICENSE.txt manifest.json verified_contents.json"

# Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Automatically determine the architecture we're building on:
MARCH=${ARCH:-$( uname -m )}
case "$MARCH" in
  x86_64)
    LIBDIRSUFFIX="64"
    SRCARCH=x64
    SO_ARCH=x64
    export ARCH=x86_64
    ;;
  i?86)
    LIBDIRSUFFIX=""
    SRCARCH=ia32
    SO_ARCH=x86
    export ARCH=i386
    ;;
  *)
    echo "Unsupported architecture '$ARCH'"
    exit 1
    ;;
esac

# Each zip contains a CDM binary, manifest.json file, and LICENSE.txt file.
# The place where Mozilla Firefox downloads these ZIP files is documented here:
# https://github.com/mozilla/gecko-dev/blob/master/toolkit/content/gmp-sources/widevinecdm.json

SOURCE[0]="$SRCDIR/widevinecdm-${VERSION}.json"
SRCURL[0]="https://raw.githubusercontent.com/mozilla-firefox/firefox/refs/heads/main/toolkit/content/gmp-sources/widevinecdm.json"

SOURCE[1]="$SRCDIR/${VERSION}-linux-${SRCARCH}.zip"
SRCURL[1]="https://www.google.com/dl/release2/chrome_component/accssjtqfpf5qicscrptql4jyyxa_4.10.2934.0/oimompecagnajdejgnnjijobebaeigek_4.10.2934.0_linux_ph722a3wl2goebkpserszm6bde.crx3"

##
## --- with a little luck, you won't have to edit below this point --- ##
##

# Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $OUTPUT/error-${PRGNAM}.log' ERR
# Catch unitialized variables:
set -u
P1=${1:-1}

# Save old umask and set to 0022:
_UMASK_=$(umask)
umask 0022

# Create working directories:
mkdir -p $OUTPUT          # place for the package to be saved
mkdir -p $TMP/tmp-$PRGNAM # location to build the source
mkdir -p $PKG             # place for the package to be built
rm -rf $PKG/*             # always erase old package's contents
rm -rf $TMP/tmp-$PRGNAM/* # remove the remnants of previous build
rm -rf $OUTPUT/{configure,make,install,error,makepkg,patch}-$PRGNAM.log
                          # remove old log files

# Source file availability:
for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
  if ! [ -f ${SOURCE[$i]} ]; then
    echo "Source '$(basename ${SOURCE[$i]})' not available yet..."
    # Check if the $SRCDIR is writable at all - if not, download to $OUTPUT
    [ -w "$SRCDIR" ] || SOURCE[$i]="$OUTPUT/$(basename ${SOURCE[$i]})"
    if [ -f ${SOURCE[$i]} ]; then echo "Ah, found it!"; continue; fi
    if ! [ "x${SRCURL[$i]}" == "x" ]; then
      echo "Will download file to $(dirname $SOURCE[$i])"
      curl -L -s -k -o "${SOURCE[$i]}" "${SRCURL[$i]}" || true
      if [ $? -ne 0 -o ! -s "${SOURCE[$i]}" ]; then
        echo "Fail to download '$(basename ${SOURCE[$i]})'. Aborting the build."
        mv -f "${SOURCE[$i]}" "${SOURCE[$i]}".FAIL
        exit 1
      fi
    else
      echo "File '$(basename ${SOURCE[$i]})' not available. Aborting the build."
      exit 1
    fi
  fi
done

if [ "$P1" == "--download" ]; then
  echo "Download complete."
  echo "Check that the following match your expectations:"
  echo "-----"
  echo ">> Current online version; $(curl -L -s -k -o - ${SRCURL[0]} |grep '\"version\":' |cut -d\" -f4)"
  echo ">> Widevine version as reported by downloaded files: $(unzip -p ${SOURCE[1]} manifest.json |grep '\"version\":' |cut -d\" -f4)"
  echo ">> Widevine package version: $VERSION"
  echo "-----"
  echo -e ">> Widevine download URL used in this script:\n${SRCURL[1]}"
  echo -e ">> Widevine download URL in widevinecdm-${VERSION}.json:\n$(python3 <<EOT
import json
with open("widevinecdm-${VERSION}.json", mode="r", encoding="utf-8") as read_file:
  widevine_data = json.load(read_file)
print(widevine_data["vendors"]["gmp-widevinecdm"]["platforms"]["Linux_x86_64-gcc3"]["mirrorUrls"][0])
EOT
)"
  echo "-----"
  exit 0
fi

# --- PACKAGE BUILDING ---

cd $TMP/tmp-$PRGNAM
echo "Extracting the source archive(s) for $PRGNAM..."
# We do not extract with pathnames - the paths are recreated in the SlackBuild.
# Note: 'unzip' throws an error because of the 1060 extra bytes at the
# beginning of the archive (it is actually a .crx3 file aka a Chrome extension)
# so we need to ignore that error:
unzip -j ${SOURCE[1]} || true
chmod -R u+w,go+r-w,a+X-s .

# Move the extracted files over to the package location:
install -d  $PKG/usr/lib${LIBDIRSUFFIX}/$SRCNAM$SRCEXT/WidevineCdm/_platform_specific/linux_${SO_ARCH}
install -m0755 libwidevinecdm.so \
  $PKG/usr/lib${LIBDIRSUFFIX}/$SRCNAM$SRCEXT/WidevineCdm/_platform_specific/linux_${SO_ARCH}/
# The manifest.json file is now used by Chromium to determine the features
# that are supported by the Widevine CDM:
install -m0755 manifest.json \
  $PKG/usr/lib${LIBDIRSUFFIX}/$SRCNAM$SRCEXT/WidevineCdm/
# Compatibility link:
ln -s /usr/lib${LIBDIRSUFFIX}/$SRCNAM$SRCEXT/WidevineCdm/_platform_specific/linux_${SO_ARCH}/libwidevinecdm.so \
  $PKG/usr/lib${LIBDIRSUFFIX}/$SRCNAM$SRCEXT/
# Also support the 'ungoogled' version:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/${SRCNAM}${SRCEXT}-ungoogled/
ln -s ../${SRCNAM}${SRCEXT}/WidevineCdm $PKG/usr/lib${LIBDIRSUFFIX}/${SRCNAM}${SRCEXT}-ungoogled/
ln -s ../${SRCNAM}${SRCEXT}/WidevineCdm/_platform_specific/linux_${SO_ARCH}/libwidevinecdm.so \
  $PKG/usr/lib${LIBDIRSUFFIX}/${SRCNAM}${SRCEXT}-ungoogled/

# Add documentation:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION || true
cat $SRCDIR/$(basename $0) > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION
find $PKG/usr/doc -type f -exec chmod 644 {} \;

# Add a package description:
mkdir -p $PKG/install
cat $SRCDIR/slack-desc > $PKG/install/slack-desc
cat $SRCDIR/slack-required > $PKG/install/slack-required
cat $SRCDIR/doinst.sh \
  | sed -e "s/@PRGNAM@/${PRGNAM}/g" -e "s/@CHROMIUM@/${SRCNAM}${SRCEXT}/g" \
  > $PKG/install/doinst.sh

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} 2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
cd $OUTPUT
md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz}.md5
cd -
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.txt
cat $PKG/install/slack-required > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.dep

# Restore the original umask:
umask ${_UMASK_}

