#!/usr/bin/env bash

set -e

# remember cwd for later restoration
PREV_WD=$(pwd)

# raise the limit on number of open files
ulimit -n 2048

PACKAGE_DIR=`pwd`

if [ "$1" != "Desktop" ] && [ "$1" != "Server" ]
then
   echo "error: must specify Desktop or Server as configuration"
   exit 1
fi

if [ "$2" != "DEB" ] && [ "$2" != "RPM" ]
then
   echo "error: must specify DEB or RPM as package target"
   exit 1
fi

# set build type( if necessary) and build dir
if test -z "$CMAKE_BUILD_TYPE"
then
   CMAKE_BUILD_TYPE=RelWithDebInfo
   BUILD_DIR=build-$1-$2
else
   BUILD_DIR=build-$1-$2-$CMAKE_BUILD_TYPE
fi

# make build directory absolute
BUILD_DIR=$(readlink -f "$BUILD_DIR")

# clean if requested
if [ "$3" == "clean" ]
then
   # remove existing build dir
   rm -rf $BUILD_DIR

   # clean out ant build if in source tree
   if [ -d "../../src/gwt" ]; then
      cd ../../src/gwt
      ant clean
   fi
   cd $PACKAGE_DIR
fi

if [ "$1" == "Desktop" ]
then
  INSTALL_DIR=rstudio
else
  INSTALL_DIR=rstudio-server
fi

: ${CMAKE_INSTALL_PREFIX="/usr/lib/${INSTALL_DIR}"}
: ${CMAKE_GENERATOR="Unix Makefiles"}

PKG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $PKG_DIR

mkdir -p $BUILD_DIR/gwt
cd $BUILD_DIR
rm -f CMakeCache.txt
rm -rf $BUILD_DIR/_CPack_Packages
cmake -G"${CMAKE_GENERATOR}"                           \
      -DRSTUDIO_TARGET=$1                              \
      -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE             \
      -DRSTUDIO_PACKAGE_BUILD=1                        \
      -DCMAKE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}" \
      -DGWT_BIN_DIR="$BUILD_DIR/gwt/bin"               \
      -DGWT_WWW_DIR="$BUILD_DIR/gwt/www"               \
      -DGWT_EXTRAS_DIR="$BUILD_DIR/gwt/extras"         \
      $PKG_DIR/../..

cmake --build . --target all

if [ "$2" != "DEB" ]
then
   fakeroot cpack -G "$2"
else
   cpack -G $2

   # Fix permissions for non-development builds
   if [ -z "${RSTUDIO_DEVELOPMENT_BUILD}" ]
   then
      $PKG_DIR/fix-debian-permissions `ls *.deb`
   fi

fi

cd $PREV_WD

