#!/bin/sh -eu

FFMPEG_VERSION='87.0.4280.66-0ubuntu0.16.04.1'

case 'amd64' in
  amd64|x86_64)
    FFMPEG_URL="https://launchpadlibrarian.net/507640811/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_amd64.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='ff1517d207eed657ded067d2c1ef9a48a603ccff4846b538ec63f2903e5d2552'
    ;;
  i386)
    FFMPEG_URL="https://launchpadlibrarian.net/507638697/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_i386.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='ba12acacf83dfe596a3428351230434726cbac93327759f8f0b32eb3f11ce536'
    ;;
  armhf)
    FFMPEG_URL="https://launchpadlibrarian.net/507792523/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_armhf.deb"
    FFMPEG_OFFSET='1077'
    FFMPEG_SUM='65c7c540965af4de3429d72672592cf3d43ef4b81f21b68e0e59f2cbf8ddda24'
    ;;
  arm64)
    FFMPEG_URL="https://launchpadlibrarian.net/507817203/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_arm64.deb"
    FFMPEG_OFFSET='1079'
    FFMPEG_SUM='6520b49d7e0b8debf942f1e1076a40e10619424c2bc72e0c0e910092bf957387'
    ;;
esac

if [ "x${1-}" = "x--system" ]; then
  shift 1
fi
if [ "x${1-}" = "x--user" ]; then
  FFMPEG_INSTALL_DIR="$HOME/.local/lib/vivaldi-snapshot/media-codecs-${FFMPEG_VERSION%-*}"
  shift 1
else
  FFMPEG_INSTALL_DIR="/var/opt/vivaldi-snapshot/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 [ "amd64" = "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
