#!/usr/bin/env bash

#
# install-cef
#
# Copyright (C) 2009-12 by RStudio, PBC
#
# Unless you have received this program directly from RStudio pursuant
# to the terms of a commercial license agreement with RStudio, 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

# install dir
INSTALL_DIR=`pwd`

# determine platform
PLATFORM=`uname`

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

# use curl or wget as appropriate
download()
{
  if [ "$PLATFORM" == "Darwin" ]
  then
    curl -L https://s3.amazonaws.com/rstudio-buildtools/$1 > $1
  else
    wget https://s3.amazonaws.com/rstudio-buildtools/$1 -O $1
  fi
}

# download and extract cef if necessary
CEF_VERSION="3.1547.1412"
if [ "$PLATFORM" == "Darwin" ]
then
  CEF_PLATFORM=macosx64

  # TODO: Linux platform (32 vs. 64 bit)
fi

CEF_DIR=CEF
if [ -d "$CEF_DIR/$CEF_VERSION" ]
then
   echo "Chromium Embedded Framework already installed"
else
   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
fi

# back to install dir
cd $INSTALL_DIR
