#!/bin/sh -eu

FFMPEG_VERSION='81.0.4044.138-0ubuntu0.16.04.1'

case 'amd64' in
  amd64|x86_64)
    FFMPEG_URL="https://launchpadlibrarian.net/478578979/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_amd64.deb"
    FFMPEG_OFFSET='1073'
    FFMPEG_SUM='6921a964cbfba7936e42b85059b71e78ad35a981fc6e45b59dc8d2e4641cff93'
    ;;
  i386)
    FFMPEG_URL="https://launchpadlibrarian.net/478598248/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_i386.deb"
    FFMPEG_OFFSET='1077'
    FFMPEG_SUM='94315f6cb8e95caf71de2036a8d3228f66631c6b78c499941b1757c9c30a8ee6'
    ;;
  armhf)
    FFMPEG_URL="https://launchpadlibrarian.net/478683516/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_armhf.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='25355a653e6cc129cf7bcd1be83d2e9f97ff785f872f43f2033fc0ff4e785ddd'
    ;;
  arm64)
    FFMPEG_URL="https://launchpadlibrarian.net/478691243/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION}_arm64.deb"
    FFMPEG_OFFSET='1075'
    FFMPEG_SUM='a1d50737590327267aef5a68de0a47a4286be4425d393b980f195860e8a0193c'
    ;;
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

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
