#!/bin/sh -eu

# Temporarily disable on ARM systems because there are no suitable libs (2023-05-26)
case amd64 in
  arm*|aarch64)
    echo "No suitable FFMpeg libs to enable proprietary media are currently available"
    exit 0
    ;;
esac

FFMPEG_VERSION=115541 # Chromium 125
FFMPEG_MIN_GLIBC_ZIP=17

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 amd64 in
  amd64|x86_64)
    FFMPEG_URL_ZIP=https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download/0.88.0/0.88.0-linux-x64.zip
    FFMPEG_SUM_ZIP=d6909a73fd3b2408964ac039ac8cf00bc896759293ed7975a45afab6933802b5
    FFMPEG_URL_SNAP=https://api.snapcraft.io/api/v1/snaps/download/XXzVIXswXKHqlUATPqGCj2w2l7BxosS8_41.snap
    FFMPEG_XZ_OFFSET_SNAP=97
    FFMPEG_LIB_OFFSET_SNAP=46085007
    FFMPEG_LENGTH_SNAP=22352587
    FFMPEG_SUM_SNAP=7230873809a95bbe58b8aeaf4c6ad62ee54b4e9801b4098b613ec6ed7bb789db
    FFMPEG_MIN_GLIBC_SNAP=35
    check_glibc "$FFMPEG_MIN_GLIBC_ZIP"
    [ "$LIBC_MINOR_VERSION" -ge "$FFMPEG_MIN_GLIBC_SNAP" ] && FFMPEG_USE_SNAP=1
    ;;
  armhf|armv7hl)
    # Disabled 2023-06-06 (VB-97951)
    FFMPEG_URL_ZIP=UNKNOWN
    FFMPEG_SUM_ZIP=UNKNOWN
    check_glibc "$FFMPEG_MIN_GLIBC_ZIP"
    ;;
  arm64|aarch64)
    # Disabled 2023-06-06 (VB-97951)
    FFMPEG_URL_ZIP=UNKNOWN
    FFMPEG_SUM_ZIP=UNKNOWN
    check_glibc "$FFMPEG_MIN_GLIBC_ZIP"
    ;;
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*
  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"
  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
