#!/bin/sh -eu

FFMPEG_VERSION='78.0.3904.70-0ubuntu0.16.04.2'

case 'amd64' in
  amd64|x86_64)
    FFMPEG_URL="https://launchpadlibrarian.net/449403909/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_amd64.deb"
    FFMPEG_OFFSET='1077'
    FFMPEG_SUM='76082b49201e14e05f95d77be1f7797a9ec536447bab512e3c55dcfefed58703'
    ;;
  i386)
    FFMPEG_URL="https://launchpadlibrarian.net/449418163/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_i386.deb"
    FFMPEG_OFFSET='1073'
    FFMPEG_SUM='395c70a659d46a9734e0df1fbadcb7e993852e380b34adcc169c03d8fb11b5ab'
    ;;
  armhf)
    FFMPEG_URL="https://launchpadlibrarian.net/449479886/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_armhf.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='25d7d378c8013449d50a73be7dd063ed49544981f23ac9b167855d152fcbd113'
    ;;
  arm64)
    FFMPEG_URL="https://launchpadlibrarian.net/449521951/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_arm64.deb"
    FFMPEG_OFFSET='1077'
    FFMPEG_SUM='3ab53903f72972b322b2651aa59c70b18725e2aef5303ffc07588d2579368c1c'
    ;;
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 [ ! -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

if available wget; then
  DOWNLOAD="wget -O-"
elif available curl; then
  DOWNLOAD="curl"
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
