#!/usr/bin/env bash

#
# install-cef
#
# Copyright (C) 2022 by Posit Software, PBC
#
# Unless you have received this program directly from Posit Software pursuant
# to the terms of a commercial license agreement with Posit Software, then
# this program is licensed to you under the terms of version 3 of the
# GNU Affero General Public License. This program is distributed WITHOUT
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
#
#

set -e

source "$(dirname "${BASH_SOURCE[0]}")/../tools/rstudio-tools.sh"
section "Installing Chromium Embedded Framework (CEF)"

# install dir
INSTALL_DIR=$(pwd)

# determine platform
PLATFORM=$(uname)

# bail if not mac for now
if [ "$PLATFORM" != "Darwin" ]; then
  exit 0
fi

# download and extract cef if necessary
# TODO: Linux platform (32 vs. 64 bit)
CEF_VERSION="3.1547.1412"
if [ "$PLATFORM" = "Darwin" ]; then
  CEF_PLATFORM=macosx64
fi

CEF_DIR=CEF
if [ -d "$CEF_DIR/$CEF_VERSION" ]; then
   echo "Chromium Embedded Framework already installed at '${CEF_DIR}/${CEF_VERSION}'"
   cd "${INSTALL_DIR}"
   exit 0
fi

CEF_FILE=cef_binary_${CEF_VERSION}_$CEF_PLATFORM
CEF_ARCHIVE=$CEF_FILE.zip
download $CEF_ARCHIVE
unzip -q $CEF_ARCHIVE
rm -rf __MACOSX
mkdir -p $CEF_DIR
mv $CEF_FILE $CEF_DIR/$CEF_VERSION
rm $CEF_ARCHIVE

echo "Chromium Embedded Framework installed to '${CEF_DIR}/${CEF_VERSION}'"
cd "${INSTALL_DIR}"
