#!/bin/sh -eu

VIVALDI_VERSION=8.1.4087.53
VIVALDI_VERSION_SHORT="${VIVALDI_VERSION%\.*\.*}"
FFMPEG_VERSION=8.1-149-Z-20260709
FFMPEG_MIN_GLIBC_ZIP=36

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

available ldd && LIBC_MINOR_VERSION="$(ldd --version | head -n1 | sed -n '/^ldd .* [2-9]\./s/.*\.\([0-9]\+\)$/\1/p')"
LIBC_MINOR_VERSION="${LIBC_MINOR_VERSION:-17}"
check_glibc () {
  if [ "$LIBC_MINOR_VERSION" -lt "$1" ]; then
    echo "Your glibc version is too old to search for a replacement libffmpeg that supports proprietary media" >&2
    exit 1
  fi
}

FFMPEG_USE_SNAP=0
case x86_64 in
  amd64|x86_64)
    FFMPEG_LENGTH_SNAP=3504936
    FFMPEG_LIB_OFFSET_SNAP=27794449
    FFMPEG_MIN_GLIBC_SNAP=38
    FFMPEG_SUM_SNAP=b2318b0b534714bfae044bcd96dc771a19134e57a94f2d43f2a4adefab7db398
    FFMPEG_SUM_ZIP=6464f290a6ffb15afa93e540ded5adc1a92234fa11a1ed1f3886ca9707878c7b
    FFMPEG_URL_SNAP=https://api.snapcraft.io/api/v1/snaps/download/XXzVIXswXKHqlUATPqGCj2w2l7BxosS8_117.snap
    FFMPEG_URL_ZIP=https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download/0.112.0/0.112.0-linux-x64.zip
    FFMPEG_XZ_OFFSET_SNAP=97
    check_glibc "$FFMPEG_MIN_GLIBC_ZIP"
    if [ "$LIBC_MINOR_VERSION" -ge "$FFMPEG_MIN_GLIBC_SNAP" ]; then
      FFMPEG_USE_SNAP=1
      FFMPEG_VERSION=8.1-150-S-20260618
    fi
    ;;
  arm64|aarch64)
    FFMPEG_LENGTH_SNAP=3070288
    FFMPEG_LIB_OFFSET_SNAP=21145281
    FFMPEG_MIN_GLIBC_SNAP=38
    FFMPEG_SUM_SNAP=2f556890447b128eecb308159a30457ef675f564fad7ddec819a001fd3677c3a
    FFMPEG_SUM_ZIP=3839f68c4d722d48d91c6e0ae80cfb2fbb4795252043d104b232eca3dc87a820
    FFMPEG_URL_SNAP=https://api.snapcraft.io/api/v1/snaps/download/XXzVIXswXKHqlUATPqGCj2w2l7BxosS8_116.snap
    FFMPEG_URL_ZIP=https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download/0.112.0/0.112.0-linux-arm64.zip
    FFMPEG_XZ_OFFSET_SNAP=97
    check_glibc "$FFMPEG_MIN_GLIBC_ZIP"
    if [ "$LIBC_MINOR_VERSION" -ge "$FFMPEG_MIN_GLIBC_SNAP" ]; then
      FFMPEG_USE_SNAP=1
      FFMPEG_VERSION=8.1-150-S-20260618
    fi
    ;;
esac

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

cleanup_files () {
  # Cleanup needs to be able to handle files from earlier installs, where the
  # numbered path could be different.
  if ls "${FFMPEG_INSTALL_DIR%/media-codecs-*}"/media-codecs-*/libffmpeg.so* >/dev/null 2>&1; then
    rm -f "${FFMPEG_INSTALL_DIR%/media-codecs-*}"/media-codecs-*/libffmpeg.so* "${FFMPEG_INSTALL_DIR%/media-codecs-*}/media-codecs-$VIVALDI_VERSION_SHORT"
  fi
  if [ -d "${FFMPEG_INSTALL_DIR%/media-codecs-*}" ]; then
    find "${FFMPEG_INSTALL_DIR%/media-codecs-*}" -depth -type d -empty -exec rmdir {} \;
  fi
}

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

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

# If a suitable file already exists we do not need to do anything and
# can exit early.
check_valid_libffmpeg () {
  if [ -e "$FFMPEG_INSTALL_DIR/libffmpeg.so" ] && echo "$1  $FFMPEG_INSTALL_DIR/libffmpeg.so" | sha256sum -c >/dev/null 2>&1; then
    echo "Proprietary media codecs ($FFMPEG_VERSION) was already present"
    chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR%/media-codecs-*}"
    exit 0
  fi
}
if [ "$FFMPEG_USE_SNAP" = '1' ]; then
  check_valid_libffmpeg "$FFMPEG_SUM_SNAP"
else
  check_valid_libffmpeg "$FFMPEG_SUM_ZIP"
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 -qO- --no-check-certificate"
  CHECK_DOWNLOAD="wget --spider"
elif available curl; then
  DOWNLOAD="curl -sL --insecure"
  CHECK_DOWNLOAD="curl -LI"
else
  echo "Neither Wget nor cURL is installed; aborting" >&2
  exit 1
fi

# Remove any previous version before installing the new one
cleanup_files

# Fetch and extract libffmpeg
mkdir -p "$FFMPEG_INSTALL_DIR"
check_valid_libffmpeg_after_download () {
  chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR%/media-codecs-*}"
  if ! echo "$1  $2" | sha256sum -c >/dev/null 2>&1; then
    echo "The extracted libffmpeg.so does not match the expected sha256sum; aborting" >&2
    cleanup_files
    exit 1
  fi
  mv "$2" "$FFMPEG_INSTALL_DIR/libffmpeg.so"
  ln -fs "$FFMPEG_INSTALL_DIR" "${FFMPEG_INSTALL_DIR%/media-codecs-*}/media-codecs-$VIVALDI_VERSION_SHORT"
  echo "Proprietary media codecs ($FFMPEG_VERSION) has been installed (PLEASE RESTART VIVALDI)"
}
FFMPEG_PARTIAL="$(mktemp "$FFMPEG_INSTALL_DIR/libffmpeg.so.XXXX")"
echo "Downloading..."
if [ "$FFMPEG_USE_SNAP" = '1' ]; then
  # We hide the download errors here because wget/curl will complain since
  # they are prevented from completing their download. This may confuse
  # the user, causing them to suspect something has gone wrong.
  $DOWNLOAD "$FFMPEG_URL_SNAP" 2>/dev/null | tail -c+"$FFMPEG_XZ_OFFSET_SNAP" | xz -dqq | tail -c+"$FFMPEG_LIB_OFFSET_SNAP" | head -c"$FFMPEG_LENGTH_SNAP" > "$FFMPEG_PARTIAL" ||:
  check_valid_libffmpeg_after_download "$FFMPEG_SUM_SNAP" "$FFMPEG_PARTIAL"
else
  $DOWNLOAD "$FFMPEG_URL_ZIP" | gzip -d > "$FFMPEG_PARTIAL" ||:
  check_valid_libffmpeg_after_download "$FFMPEG_SUM_ZIP" "$FFMPEG_PARTIAL"
fi
