#!/bin/sh -eu

WIDEVINE_VERSION=4.10.1582.2

# These checksums and file offsets must be updated in sync with the Widevine version (above)
# Offsets from: `zipinfo -v $WIDEVINE_PKG`
case "x86_64" in
  amd64|x86_64) echo "This script is unneeded for x86_64. Check your Widevine version via vivaldi://components" >&2; exit ;;
  i386)
    WIDEVINE_ZIP_ARCH=ia32
    WIDEVINE_ZIP_SUM=31e7f1ded497ec4edd3d4bc635de3d9191b65fb3e411eb829cb0615344872cd8
    WIDEVINE_LIBRARY=0,4288276,_platform_specific/linux_x86/libwidevinecdm.so
    WIDEVINE_MANIFEST=4288277,243,manifest.json
    WIDEVINE_LICENSE=4288517,319,LICENSE.txt
    ;;
  arm*) echo "This script does not support ARM. See https://help.vivaldi.com/article/raspberry-pi/#drm-and-flash for more information" >&2; exit ;;
esac

if [ "x${1-}" = "x--system" ]; then
  shift 1
fi
if [ "x${1-}" = "x--user" ]; then
  WIDEVINE_INSTALL_DIR="$HOME/.local/lib/vivaldi/WidevineCdm"
  shift 1
else
  WIDEVINE_INSTALL_DIR="/var/opt/vivaldi/WidevineCdm"
  if [ "${USER:-}" != "root" ]; then
    echo "You may need to be root (or rerun this command with sudo)" >&2
  fi
fi

WIDEVINE_PKG="$WIDEVINE_VERSION-linux-$WIDEVINE_ZIP_ARCH.zip"
VIVALDI_INSTALL_DIR="${0%/*}"

cleanup_files () {
  for WIDEVINE_FILE in "${WIDEVINE_LIBRARY##*,}" "${WIDEVINE_MANIFEST##*,}" "${WIDEVINE_LICENSE##*,}" "$WIDEVINE_PKG" ../libwidevinecdm.so; do
    if [ -e "$WIDEVINE_INSTALL_DIR/$WIDEVINE_FILE" ]; then
      rm "$WIDEVINE_INSTALL_DIR/$WIDEVINE_FILE"
    fi
  done
  if [ -d "$WIDEVINE_INSTALL_DIR" ]; then
    # This removes directory trees that are empty or only populated by other
    # empty directories.
    find "${WIDEVINE_INSTALL_DIR%/WidevineCdm}" -depth -type d -empty -exec rmdir {} \;
  fi
  # Recreate the default symlink included in the package
  if [ -w "$VIVALDI_INSTALL_DIR" ]; then
    rm -f "$VIVALDI_INSTALL_DIR/WidevineCdm"
    ln -fs /opt/google/chrome/WidevineCdm "$VIVALDI_INSTALL_DIR/WidevineCdm"
  fi
}

if [ "x${1-}" = "x--undo" ]; then
  cleanup_files
  exit
fi

check_ffmpeg () {
  # Suggest the user run update-ffmpeg if it is needed
  if ! ls "${WIDEVINE_INSTALL_DIR%/WidevineCdm}"/media-codecs-*/libffmpeg.so >/dev/null 2>&1; then
    printf "\nHowever, support for 'proprietary media' is not installed. Fix this by running:\n" >&2
    printf "    $VIVALDI_INSTALL_DIR/update-ffmpeg\n\n" >&2
  fi
}

available () {
  command -v "$1" >/dev/null 2>&1
}

if ! available sha256sum; then
  echo "sha256sum is not installed; aborting" >&2
  exit 1
fi

if [ -e "$WIDEVINE_INSTALL_DIR/${WIDEVINE_LIBRARY##*,}" -a -e "$WIDEVINE_INSTALL_DIR/${WIDEVINE_MANIFEST##*,}" ] && \
   [ "$WIDEVINE_VERSION" = "$(sed -rn '/"version":/s/.*(([0-9]+\.){3}[0-9]+).*/\1/p' "$WIDEVINE_INSTALL_DIR/${WIDEVINE_MANIFEST##*,}")" ]; then
  echo "Widevine ($WIDEVINE_VERSION) is already installed and does not need to be updated"
  chmod -R u+rwX,go+rX-w "${WIDEVINE_INSTALL_DIR%/WidevineCdm}"
  if [ "$(readlink -f "$VIVALDI_INSTALL_DIR/WidevineCdm")" != "$WIDEVINE_INSTALL_DIR" -a -w "$VIVALDI_INSTALL_DIR" ]; then
    echo "Redirecting \"$VIVALDI_INSTALL_DIR/WidevineCdm\" to \"$WIDEVINE_INSTALL_DIR\""
    rm -f "$VIVALDI_INSTALL_DIR/WidevineCdm"
    ln -fs "$WIDEVINE_INSTALL_DIR" "$VIVALDI_INSTALL_DIR/WidevineCdm"
  fi
  check_ffmpeg
  exit 0
fi

# We don't need to check certificates because we verify package contents with
# checksums. By avoiding the check we also allow for download on a distro that
# lacks an up to date certificate store (see: VB-68785)
if available wget; then
  DOWNLOAD="wget --no-check-certificate"
elif available curl; then
  DOWNLOAD="curl --insecure -LO"
else
  echo "Neither Wget nor cURL is installed; aborting" >&2
  exit 1
fi

# Remove any previous version before installing the new one
cleanup_files

mkdir -p "$WIDEVINE_INSTALL_DIR/$(dirname ${WIDEVINE_LIBRARY##*,})"
cd "$WIDEVINE_INSTALL_DIR"

$DOWNLOAD "https://dl.google.com/widevine-cdm/$WIDEVINE_PKG" ||:

if ! echo "$WIDEVINE_ZIP_SUM  $WIDEVINE_PKG" | sha256sum -c >/dev/null 2>&1; then
  echo "$WIDEVINE_PKG does not match the expected sha256sum; aborting" >&2
  cd - >/dev/null
  cleanup_files
  exit 1
fi

for FILE in "$WIDEVINE_LIBRARY" "$WIDEVINE_MANIFEST" "$WIDEVINE_LICENSE"; do
  RANGE="${FILE%,*}"
  if ! tail -c+"${RANGE%,*}" "$WIDEVINE_PKG" | head -c"${RANGE#*,}" | gzip -d > "${FILE##*,}"; then
    echo "Failed to extract ${FILE##*,}; aborting" >&2
    cd - >/dev/null
    cleanup_files
    exit 1
  fi
done

rm "$WIDEVINE_PKG"
chmod -R u+rwX,go+rX-w "${WIDEVINE_INSTALL_DIR%/WidevineCdm}"

# Check if the correct symlink is already present, to prevent an extra symlink being nested beneath it
cd - >/dev/null
if [ "$(readlink -f "$VIVALDI_INSTALL_DIR/WidevineCdm")" != "$WIDEVINE_INSTALL_DIR" -a -w "$VIVALDI_INSTALL_DIR" ]; then
  rm -f "$VIVALDI_INSTALL_DIR/WidevineCdm"
  ln -fs "$WIDEVINE_INSTALL_DIR" "$VIVALDI_INSTALL_DIR/WidevineCdm"
fi

echo "Widevine ($WIDEVINE_VERSION) installed successfully (PLEASE RESTART VIVALDI)"
check_ffmpeg
