#!/bin/sh -eu

FFMPEG_VERSION='86.0.4240.75-0ubuntu0.16.04.1'

case 'x86_64' in
  amd64|x86_64)
    FFMPEG_URL="https://launchpadlibrarian.net/501258521/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_amd64.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='759aeef124e8fb754712f4b6dbc17d0463280da18208ed1edcb3d7871a999175'
    ;;
  i386)
    FFMPEG_URL="https://launchpadlibrarian.net/501166678/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_i386.deb"
    FFMPEG_OFFSET='1077'
    FFMPEG_SUM='74905ae6ee0ab8c53bd2f2d145a33e6d110a7364226e03de1072546a69a06738'
    ;;
  armhf)
    FFMPEG_URL="https://launchpadlibrarian.net/501298713/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_armhf.deb"
    FFMPEG_OFFSET='1071'
    FFMPEG_SUM='ef40b4fd3682d810e572fef5ee90271cc31a24147ab9c454eab25d1ee8af4fdc'
    ;;
  arm64)
    FFMPEG_URL="https://launchpadlibrarian.net/501307781/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_arm64.deb"
    FFMPEG_OFFSET='1077'
    FFMPEG_SUM='4e99f9ddd5e65c732011b79c94660c7730dd0259d96841282b46f572a6b0b4c3'
    ;;
esac

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

VIVALDI_INSTALL_DIR="${0%/*}"

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
    # This removes directory trees that are empty or only populated by other
    # empty directories.
    find "${FFMPEG_INSTALL_DIR%/media-codecs-*}" -depth -type d -empty -exec rmdir {} \;
  fi
}

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

check_widevine () {
  # Suggest the user run update-widevine if it is needed
  if [ "x86_64" = "i386" ] && [ ! -e "$VIVALDI_INSTALL_DIR/WidevineCdm" ]; then
    printf "\nHowever, the Widevine CDM is not installed. Fix this by running:\n" >&2
    printf "    $VIVALDI_INSTALL_DIR/update-widevine\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 "$FFMPEG_INSTALL_DIR/libffmpeg.so" ] && echo "$FFMPEG_SUM  $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-*}"
  check_widevine
  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 -O-"
elif available curl; then
  DOWNLOAD="curl --insecure"
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 "$FFMPEG_INSTALL_DIR"

$DOWNLOAD "$FFMPEG_URL" | tail -c+"$FFMPEG_OFFSET" | xz -d | tar fOx - ./usr/lib/chromium-browser/libffmpeg.so > "$FFMPEG_INSTALL_DIR/libffmpeg.so" ||:
chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR%/media-codecs-*}"

if ! echo "$FFMPEG_SUM  $FFMPEG_INSTALL_DIR/libffmpeg.so" | 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

echo "Proprietary media codecs (${FFMPEG_VERSION%-*}) has been installed (PLEASE RESTART VIVALDI)"
check_widevine
