### this file is sourced not run

# Dependencies: rsync.

PKGNAME=rstudio
PKGVER=1.0.136
PKGARCH=x86_64
PKGBUILD=1
RAPPDIR=/usr/local/apps/RStudio
RAPPDESKTOPFILE=/usr/share/applications/rstudio.desktop
RAPPDESC="IDE for R"
MIMEPACKAGE=/usr/share/mime/packages/rstudio.xml # if any, adds rox SendTos and MIME-types

SRC="https://github.com/rstudio/rstudio/archive/v$PKGVER.tar.gz|f33a5dd5630973f36fc9770f87302e73"
DEB="https://download1.rstudio.org/rstudio-$PKGVER-amd64.deb|2a73b88a12a9fbaf96251cecf8b41340"
split_vars() { # $1-"URL|md5"
  MD5SUM=${1#*|}
  SRC_URL=${1%|*}
  TARBALL=${SRC_URL##*/}
}
split_vars "$DEB"
BUNDLE=

SLACKREQ='libjpeg
R
gstreamer >= 0.10.36-x86_64-1
gst-plugins-base >= 0.10.36-x86_64-1'
SLACKDESC="rstudio: RStudio Desktop $PKGVER (IDE)
rstudio:  
rstudio: RStudio $PKGVER - $RAPPDESC.
rstudio: 
rstudio: RStudio is a set of integrated tools designed to help you be 
rstudio: more productive with R. It includes a console, a
rstudio: syntax-highlighting editor that supports direct code execution, 
rstudio: as well as tools for plotting, history, and workspace 
rstudio: management. 
rstudio: http://www.rstudio.com
rstudio: http://murga-linux.com/puppy/viewtopic.php?t=95980
"

#PKG_NOBUILD=yes # no building, extract and package only
#PKG_NOSTRIP=yes

at_subst() { # $@-file-list
  sed -i \
    -e "s|@RAPPDIR|$RAPPDIR|g" \
    -e "s|@RAPPDESKTOPFILE|$RAPPDESKTOPFILE|g" \
    -e "s|@RAPPDESC|$RAPPDESC|g" \
    -e "s|@MIMEPACKAGE|$MIMEPACKAGE|g" \
    -e "s/@PKGNAME/$PKGNAME/g" \
    -e "s/@PKGVER/$PKGVER/g" \
    -e "s/@PKGARCH/$PKGARCH/g" \
    -e "s/@PKGBUILD/$PKGBUILD/g" \
    $(readlink -f "$@")
}

rename() { # $1-source $2-target:is-removed-first
  rm -fr "$2" && mv "$1" "$2"
}

### pkg_download
pkg_download() {
  # get source archive (won't build it)
  split_vars "$SRC"
  TARBALL=rstudio-${TARBALL#v}
  if ! [ -e "$SRC_DIR/$TARBALL" ]; then
    pkg_download_default $SRC_DIR $TARBALL "$SRC_URL"
  fi &&
    pkg_integrity $SRC_DIR/$TARBALL $MD5SUM || return 1
  # get bin archive (will package it)
  split_vars "$DEB" &&
  if ! [ -e "$SRC_DIR/$TARBALL" ]; then
    pkg_download_default $SRC_DIR $TARBALL "$SRC_URL"
  fi &&
    pkg_integrity $SRC_DIR/$TARBALL $MD5SUM || return 1
  : leaving "$TARBALL ~= $DEB"
  return 0
}

### pkg_prepare
pkg_prepare() {
  local pkgname
  if split_vars "$DEB" &&
    pkg_check $SRC_DIR $TARBALL $MD5SUM &&
    echo extracting $TARBALL ... &&
    pkgname=${TARBALL%%-*} &&
    mkdir tmp/$pkgname &&
    ar x $SRC_DIR/$TARBALL data.tar.gz &&
    tar -xzf data.tar.gz -C tmp/$pkgname
  then
    rm -f data.tar.gz
    return 0
  else
    rm -f data.tar.gz
    return 1
  fi
}

### build
pkg_build() {

  # Build from source.
  cd /tmp/rstudio* &&
  #chown -R 0:0 * &&
  patch -Np1 -i ../pkg/desktop-file.diff &&

  # Build slackdesk and install/uninstall files.
  cd / &&
  if pkg_build_slackdesc tmp/pkg/pkg; then
  (
    cd tmp/pkg/pkg/install &&      
    pi=$(readlink pinstall.sh) &&
    pu=$(readlink puninstall.sh) &&
    at_subst doinst.sh "$pi" "$pu" "../$RAPPDIR/createshortcut" &&
    cp "$pu" slack-uninstall.sh &&
    sed -i "s/removepkg/: removepkg/" slack-uninstall.sh &&
    rm -f pinstall.sh puninstall.sh &&
    true
  )
  fi &&
  # "cp --remove-destination -a x/* ." won't copy over existing dirs and links
  # while rsync does all that
  rsync -a tmp/rstudio*/* tmp/pkg/pkg/* . &&

  # Enable installing side-by-side with earlier versions.
  # doinst.sh will take further the moves marked #doinst#
  if true; then
    cd / &&
    at_subst "./$RAPPDIR/AppRun" usr/bin/rstudio &&
    rename "./$RAPPDIR" "./$RAPPDIR-$PKGVER" && #doinst#
    true > "./$RAPPDIR-$PKGVER/install.log" &&
    rename "./$RAPPDESKTOPFILE" "./${RAPPDESKTOPFILE%.desktop}-$PKGVER.desktop" && #doinst#
    rename usr/bin/rstudio usr/bin/rstudio-$PKGVER && #doinst#
    rename usr/lib/rstudio usr/lib/rstudio-$PKGVER && #doinst#
    true
  fi
}

# vim: ft=sh:
