#!/usr/bin/env bash

#
# mac-package
#
# Copyright (C) 2022 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.
#
#

cd $(dirname "${BASH_SOURCE[0]}")/..
source "../../../dependencies/tools/rstudio-tools.sh"

function help() {
    cat <<EOF
usage: mac-package [options]

By default creates an Electron-based RStudio.app using binary components from 
/Applications/RStudio.app.

Examples
  ./mac-package
  ./mac-package --start 

Options
  --app=<directory>
        Location of RStudio.app as a source for native components during packaging
        Default is /Applications

  --start
        Start previously packaged RStudio.app
        as the source for non-Electron components

EOF
}

function package() {
  if [ ! -f "${RSTUDIO_APP}/RStudio.app/Contents/MacOS/RStudio" ]; then
    error RStudio.app/Contents/MacOS/RStudio not found at "${RSTUDIO_APP}"
    exit 1
  fi

  DEST_ROOT=./out/RStudio-darwin-x64
  DEST_BASE=${DEST_ROOT}/RStudio.app/Contents

  section Erasing previous Electron package build
  rm -rf ${DEST_ROOT}

  section Performing Electron package build
  yarn package
  if [ ! -d ${DEST_BASE} ]; then
    error Package build output not found at ${DEST_BASE}
    exit 1
  fi

  section Copying external bits from ${RSTUDIO_APP} into Electron app

  # executables
  SOURCE=${RSTUDIO_APP}/RStudio.app/Contents/MacOS
  DEST=${DEST_BASE}/Resources/app/bin
  mkdir ${DEST}
  cp -n ${SOURCE}/diagnostics ${DEST}
  cp -n ${SOURCE}/mac-terminal ${DEST}
  cp -n ${SOURCE}/r-ldpath ${DEST}
  cp -n ${SOURCE}/rpostback ${DEST}
  cp -n ${SOURCE}/rsession ${DEST}
  cp -n ${SOURCE}/rsession-arm64 ${DEST}
  cp -nR ${SOURCE}/pandoc ${DEST}
  cp -nR ${SOURCE}/postback ${DEST}

  # libraries
  SOURCE=${RSTUDIO_APP}/RStudio.app/Contents/Frameworks
  DEST=${DEST_BASE}/Resources/app/Frameworks
  mkdir ${DEST}
  cp -n ${SOURCE}/*.dylib ${DEST}

  # support files including GWT output
  SOURCE=${RSTUDIO_APP}/RStudio.app/Contents/Resources
  DEST=${DEST_BASE}/Resources/app
  cp -nR ${SOURCE}/* ${DEST}
}

function start() {
  APP=${DEST_BASE}/Contents/MacOS/RStudio
  if [ ! -f ${APP} ]; then
    package
  fi

  section Starting application...

  ${APP}
}

# default settings
RSTUDIO_APP=/Applications
START=false

# read command line arguments
for arg in "$@"; do

   case "$arg" in
   --app=*)
      RSTUDIO_APP=${arg#*=}
      ;;
   --start)
      START=true
      ;;
   --help|-h)
      help
      exit 0
      ;;
   *)
      help
      exit 0
      ;;
   esac

done

if [ "$(arch)" = "arm64" ]; then
  error "Running on ARM64 not (yet) supported by this script"
  exit 1
fi

if ${START}; then
  start 
else
  package
  open ${DEST_ROOT}
fi
