#!/bin/sh -eu

FFMPEG_VERSION='90.0.4430.72-0ubuntu0.16.04.1'

case 'amd64' in
  amd64|x86_64)
    FFMPEG_URL="https://launchpadlibrarian.net/534151129/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_amd64.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='65c517fe6609592e160d417f0375e959843e2188734a85fc86b3ae551fd23373'
    ;;
  i386)
    FFMPEG_URL="https://launchpadlibrarian.net/533822856/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_i386.deb"
    FFMPEG_OFFSET='1073'
    FFMPEG_SUM='83a21741611cf71ae542d2f9cf0dba20ea66bb82f07eddd671404260b706579c'
    ;;
  armhf)
    FFMPEG_URL="https://launchpadlibrarian.net/533931622/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_armhf.deb"
    FFMPEG_OFFSET='1073'
    FFMPEG_SUM='5150f3fb4aa5b3382cb0834e4fdcf3db4aaf04580d103d45c58f9055f6556093'
    ;;
  arm64)
    FFMPEG_URL="https://launchpadlibrarian.net/533923223/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_arm64.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='cd374ac5ebe8653fb62d8ba01652b3e9c0cd229494cd2146ffc1059b2fb44df6'
    ;;
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 [ "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
