#!/bin/sh

# Environment variable PKG_INSTALL_OPTIONS can be exported before
# installing this package. It's a string of words as follows:
# no-shortcut : Don't install a desktop shortcut file.
# say-yes : Answer yes to all questions, consequently do not block on GUI
#   prompts, terminals, etc.  This is probably what you want for a full
#   non-blocking install. The default install is normally full and blocking.
# say-no : Answer no to all prompts, no blocking. This sets up a minimal
#   non-blocking install.
# Example: export PKG_INSTALL_OPTIONS="no-shortcut say-yes"

# errors shouldn't cause script to exit
set +e

RAPPDIR=/usr/local/apps/RStudio
RAPPDESKTOPFILE=/usr/share/applications/rstudio.desktop
PKGNAME=rstudio
PKGVER=1.0.153
PKGARCH=x86_64
PKGBUILD=1
package=$PKGNAME-$PKGVER-$PKGARCH-$PKGBUILD
readme=README-$package.txt
tmpreadme=${TMP:-/tmp}/$readme

get_highest_installed_version_string() { # {{{ # $PKGVER excluded
  2>/dev/null head -qn 1 \
  /usr/lib/rstudio/VERSION /usr/lib/rstudio-*/VERSION |
  sort -rV | grep -v ^$PKGVER\$ -m 1
}
#}}}

backup_installed_version() { # $1-version, return < 0(fail) >=0(counter) {{{
  # TODO: backup older icons.
  local f dst ts counter
  for f in \
    "$RAPPDIR" \
    "$RAPPDESKTOPFILE" \
    /usr/bin/rstudio \
    /usr/lib/rstudio
  do
    # Change existing $f but let links be.
    if [ -e "$f" -a ! -L "$f" ]; then
      dst=
      case $f in
        *.*) dst="${f%.*}-$1.${f##*.}" ;;
          *) dst="$f-$1" ;;
      esac
      # Handle rare case when $dst already exists.
      if [ -e "$dst" ]; then
       [ -z "$ts" ] && ts=`date +%Y%m%d%H%M%S`
       # Append a timestamp, which will not revert cleanly on uninstall
       # but at least the current version gets saved.
       dst="$dst-$ts"
      fi
      mv -vf "$f" "$dst"
      counter=$((++counter))
    fi
  done
  return $counter
}
#}}}

swap_in_new_version() { # $1-version [ $2-options:"name name ..." ] {{{
  # Make links that match renames in section 'side-by-side' of file 'recipe'.
  # Undo the links in puninstall.sh.
  # Options names: dry-run, return-error
  local r t
  t=`mktemp`
  tee "$t" << EOF
ln -vsfT "${RAPPDIR##*/}-$1" "$RAPPDIR" &&
ln -vsfT "$(basename "$RAPPDESKTOPFILE" .desktop)-$1.desktop" "$RAPPDESKTOPFILE" &&
ln -vsfT rstudio-$1 /usr/bin/rstudio &&
ln -vsfT rstudio-$1 /usr/lib/rstudio &&
: Results :
EOF
  if [ "$2" = "${2##dry-run}" ]; then
    chmod +x "$t" && . "$t"; r=$?
  fi
  rm -f "$t"
  [ "$2" != "${2##return-error}" ] && return 1
  return $r
}
#}}}

# Update mime database.
update-mime-database /usr/share/mime

# Refresh icon cache.
gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor

# Display installation summary Part 1.
> "$tmpreadme" echo \
"------------------------------------------------------------------------------
This file: $tmpreadme
Package:   $package"

# Backup installed version, if any, and display summary Part 2.
installed_version=`get_highest_installed_version_string`
if [ -n "$installed_version" ]; then
  printf "%s\n" "" "Installed version $installed_version found." \
    "Conflicting files, if any, renamed:"
  backup_installed_version "$installed_version"
  r=$?
  [ $r = 0 ] && echo "No conflicts found."
  [ $r -lt 0 ] && backup_error=$r || backup_error=
fi >> "$tmpreadme"

# Swap in new version.
if true; then
  printf "%s\n" "" "Linking new version files from package:"
  swap_in_new_version $PKGVER 2>&1 ${backup_error:+dry-run return-error} &&
  echo "Symbolic links are OK." ||
  echo "*** Error. Please create symbolic links manually."
fi >> "$tmpreadme"

# Add-ons ($PKG_INSTALL_OPTIONS applies).
"$RAPPDIR-$PKGVER/install.sh"

# Display summary Part 3.
cat << 'EOF' >> "$tmpreadme"

RStudio 1.0.153 stable version, a Desktop IDE for R.
RStudio requires a recent version of the R package.

Start RStudio from system menu 'Programming' or from ROX-App 'RStudio'.
To fully uninstall RStudio right-click the ROX-App and select 'Uninstall...'

Notable paths:
  binary        /usr/bin/rstudio            (Link)
  ROX-app       /usr/local/apps/RStudio     (link)
 $RSTUDIO_HOME  /usr/lib/rstudio-1.0.153

Adding R packages
-----------------

To fully use all RStudio document types you need to install some R packages.
Visit http://murga-linux.com/puppy/viewtopic.php?t=95980 to find out which
additional, commonly-used, pre-compiled R and RStudio packages are available.
To install less-common R packages you will need to load the devx SFS for R
to automatically compile new packages from source code.
EOF
cat << EOF >> "$tmpreadme"

******************************************************************************
To save this file to your home folder start a shell prompt and enter:

    cp "$tmpreadme" "\$HOME/"

  then type 'q' to close this window...
EOF

{
  printf "===== %s =====\n" "`date`"
  cat "$tmpreadme"
  echo ""
} >> "$RAPPDIR-$PKGVER/install.log"

less=`which less 2>/dev/null`
[ "$less" ] && less="$less -m" || less=more

if [ "$PKG_INSTALL_OPTIONS" != "${PKG_INSTALL_OPTIONS#*say-}" ]; then
  term=
else
  term=`which defaultterm 2>/dev/null`
  [ "$term" ] || term=`which xterm 2>/dev/null`
fi
if [ "$term" ]; then
  ( "$term" -fg \#FFFFFF -bg \#006600 -T "$package" -e ${less:-cat} "$tmpreadme" & )
else
  cp "$tmpreadme" /var/log/
fi

# clear error termination state
set -e

true

