#!/bin/bash

set -e

# make standard package
./make-package Desktop $1 $2

# make tar.gz
BUILD_DIR=build-Desktop-$1
if ! test -z "$CMAKE_BUILD_TYPE"
then
	BUILD_DIR=$BUILD_DIR-$CMAKE_BUILD_TYPE
fi

# switch to build dir and determine name of package root
CURRENT_DIR=`pwd`
PACKAGE_DIR=$CURRENT_DIR/$BUILD_DIR/_CPack_Packages/Linux/$1
cd $PACKAGE_DIR
PACKAGE_ROOT=`ls -d rstudio-*/`
PACKAGE_ROOT=${PACKAGE_ROOT%/}

# tarball name
if [ "$1" == "DEB" ]
then
  PLATFORM=debian
else
  PLATFORM=fedora
fi
PACKAGE_TAR=$PACKAGE_DIR/$PACKAGE_ROOT-$PLATFORM.tar.gz

# copy rstudio dir to local (versioned) dir and make tarball
VERSIONED_RSTUDIO=rstudio-${RSTUDIO_VERSION_MAJOR}.${RSTUDIO_VERSION_MINOR}.${RSTUDIO_VERSION_PATCH}
rm -rf ${VERSIONED_RSTUDIO}
cp -R ${PACKAGE_ROOT}/usr/lib/rstudio ${VERSIONED_RSTUDIO}

# create tarball
tar -c -z -f $PACKAGE_TAR ${VERSIONED_RSTUDIO}

# back to current dir
cd $CURRENT_DIR





