#!/bin/sh
# SPDX-License-Identifier: MIT

set -e

# Set to 0 if packaged and the configs are pre-installed
: ${COPY_CONFIGS=1}
: ${DESTDIR:=/}
: ${CONFDIR:=/etc}
: ${LIBDIR:=/usr/lib64}
: ${BINDIR:=/usr/bin}
: ${LIBEXECDIR:=/usr/libexec}
: ${STATEDIR:=/var/lib}
: ${ENVIRONMENTDIR:=/usr/lib/environment.d}
: ${SCRIPT_BASE:=$LIBEXECDIR/widevine-installer}
: ${INSTALL_BASE:=$STATEDIR/widevine}
: ${DISTFILES_BASE:=https://commondatastorage.googleapis.com/chromeos-localmirror/distfiles}
: ${LACROS_NAME:=chromeos-lacros-arm64-squash-zstd}
: ${LACROS_VERSION:=120.0.6098.0}
: ${WIDEVINE_VERSION:=4.10.2662.3}
: ${CHROME_WIDEVINE_BASE:=$LIBDIR/chromium-browser}
: ${MOZ_PREF_BASE:=$LIBDIR/firefox/defaults/pref}

[ -e $(dirname "$0")/widevine_fixup.py ] && SCRIPT_BASE="$(realpath "$(dirname "$0")")"

install_configs() {
    cd "$SCRIPT_BASE"
    echo "Copying config files..."
    install -d -m 0755 "$DESTDIR/$INSTALL_BASE"
    install -p -m 0644 -t "$DESTDIR/$INSTALL_BASE" ./conf/README
    install -d -m 0755 "$DESTDIR/$ENVIRONMENTDIR/"
    install -p -m 0644 ./conf/gmpwidevine.conf "$DESTDIR/$ENVIRONMENTDIR/50-gmpwidevine.conf"
    install -d -m 0755 "$DESTDIR/$CONFDIR/profile.d/"
    install -p -m 0644 -t "$DESTDIR/$CONFDIR/profile.d/" ./conf/gmpwidevine.sh
    install -d -m 0755 "$DESTDIR/$CHROME_WIDEVINE_BASE"
    ln -sf "$INSTALL_BASE/WidevineCdm" "$DESTDIR/$CHROME_WIDEVINE_BASE/WidevineCdm"
    install -d -m 0755 "$DESTDIR/$MOZ_PREF_BASE"
    install -p -m 0644 -t "$DESTDIR/$MOZ_PREF_BASE" ./conf/gmpwidevine.js
}

if [ "$1" = "--distinstall" ]; then
    install_configs
    install -d -m 0755 "$DESTDIR/$LIBEXECDIR/widevine-installer"
    install -p -m 0644 -t "$DESTDIR/$LIBEXECDIR/widevine-installer" ./widevine_fixup.py
    install -d -m 0755 "$DESTDIR/$BINDIR"
    install -p -m 0755 -t "$DESTDIR/$BINDIR" ./widevine-installer
    exit 0
fi

if [ "$(uname -m)" != "aarch64" ]; then
    echo "This tool is only supported on aarch64 (ARM64) systems."
    exit 1
fi

if [ "$(whoami)" != "root" ]; then
    echo "This tool needs to be run as root."
    exit 1
fi

verchk() {
    (
        echo "2.36"
        ldd --version | head -1 | cut -d" " -f4
    ) | sort -CV
}

if ! verchk; then
    echo "Your glibc version is too old. Widevine requires glibc 2.36 or newer."
    exit 1
fi

echo "This script will download, adapt, and install a copy of the Widevine"
echo "Content Decryption Module for aarch64 systems."
echo
echo "Widevine is a proprietary DRM technology developed by Google."
echo "This script uses ARM64 builds intended for ChromeOS images and is"
echo "not supported nor endorsed by Google. The Asahi Linux community"
echo "also cannot provide direct support for using this proprietary"
echo "software, nor any guarantees about its security, quality,"
echo "functionality, nor privacy. You assume all responsibility for"
echo "usage of this script and of the installed CDM."
echo
echo "This installer will only adapt the binary file format of the CDM"
echo "for interoperability purposes, to make it function on vanilla"
echo "ARM64 systems (instead of just ChromeOS). The CDM software"
echo "itself will not be modified in any way."
echo
echo "Widevine version to be installed: $WIDEVINE_VERSION"
echo
echo "Press enter to proceed, or Ctrl-C to cancel."
read dummy

workdir="$(mktemp -d /tmp/widevine-installer.XXXXXXXX)"
[ -z "$workdir" ] && exit 1 # sanity check
[ ! -d "$workdir" ] && exit 1 # sanity check

cd "$workdir"

echo "Downloading LaCrOS (Chrome) image..."
URL="$DISTFILES_BASE/$LACROS_NAME-$LACROS_VERSION"
echo "URL: $URL"
curl -# -o lacros.squashfs "$URL"

echo
echo "Extracting Widevine..."
unsquashfs -q lacros.squashfs 'WidevineCdm/*'

echo
echo "The Widevine license agreement follows:"
echo
echo ===================================================================================
cat squashfs-root/WidevineCdm/LICENSE
echo ===================================================================================
echo
echo "Press enter to proceed, or Ctrl-C to cancel."
read dummy

python3 "$SCRIPT_BASE/widevine_fixup.py" squashfs-root/WidevineCdm/_platform_specific/cros_arm64/libwidevinecdm.so libwidevinecdm.so

echo
echo "Installing..."
install -d -m 0755 "$INSTALL_BASE"
install -p -m 0755 -t "$INSTALL_BASE" libwidevinecdm.so
install -p -m 0644 -t "$INSTALL_BASE" squashfs-root/WidevineCdm/manifest.json
install -p -m 0644 -t "$INSTALL_BASE" squashfs-root/WidevineCdm/LICENSE

echo "Setting up plugin for Firefox and Chromium-based browsers..."
mkdir -p "$INSTALL_BASE"/gmp-widevinecdm/system-installed
ln -sf ../../manifest.json "$INSTALL_BASE"/gmp-widevinecdm/system-installed/
ln -sf ../../libwidevinecdm.so "$INSTALL_BASE"/gmp-widevinecdm/system-installed/
mkdir -p "$INSTALL_BASE"/WidevineCdm/_platform_specific/linux_arm64
mkdir -p "$INSTALL_BASE"/WidevineCdm/_platform_specific/linux_x64
# Hack because Chromium hardcodes a check for this right now...
touch "$INSTALL_BASE"/WidevineCdm/_platform_specific/linux_x64/libwidevinecdm.so
ln -sf ../manifest.json "$INSTALL_BASE"/WidevineCdm
ln -sf ../../../libwidevinecdm.so "$INSTALL_BASE"/WidevineCdm/_platform_specific/linux_arm64/

if [ "$COPY_CONFIGS" = 1 ]; then
    install_configs
fi

echo "Cleaning up..."
cd /
rm -rf "$workdir"

echo
echo "Installation complete!"
if [ "$COPY_CONFIGS" = 1 ]; then
    echo "You may need to log out and back in for the changes to take effect."
else
    echo "Please restart your browser for the changes to take effect."
fi
