#!/bin/sh

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

# add rserver user account
sudo useradd -r rstudio-server
sudo groupadd -r rstudio-server

# create softlink to admin script in /usr/sbin
sudo ln -f -s ${CMAKE_INSTALL_PREFIX}/bin/rstudio-server /usr/sbin/rstudio-server

# create config directory and default config files
sudo mkdir -p /etc/rstudio
if ! test -f /etc/rstudio/rserver.conf
then
  sudo sh -c "printf '# Server Configuration File\n\n' > /etc/rstudio/rserver.conf"
fi
if ! test -f /etc/rstudio/rsession.conf
then
  sudo sh -c "echo '# R Session Configuration File\n\n' > /etc/rstudio/rsession.conf"
fi

# create var directories
sudo mkdir -p /var/run/rstudio-server
sudo mkdir -p /var/lock/rstudio-server
sudo mkdir -p /var/log/rstudio-server
sudo mkdir -p /var/lib/rstudio-server
sudo mkdir -p /var/lib/rstudio-server/conf
sudo mkdir -p /var/lib/rstudio-server/body
sudo mkdir -p /var/lib/rstudio-server/proxy

# suspend all sessions
sudo rstudio-server force-suspend-all

# check lsb release and init system
LSB_RELEASE=`lsb_release --id --short`
INIT_SYSTEM=`cat /proc/1/comm 2>/dev/null`

# add apparmor profile (but don't for systemd as this borks up process management)
if test $LSB_RELEASE = "Ubuntu" && test -d /etc/apparmor.d/ && ! test $INIT_SYSTEM = "systemd"
then
   sudo cp ${CMAKE_INSTALL_PREFIX}/extras/apparmor/rstudio-server /etc/apparmor.d/
   sudo apparmor_parser -r /etc/apparmor.d/rstudio-server 2>/dev/null
elif test -e /etc/apparmor.d/rstudio-server
then
   sudo rm -f /etc/apparmor.d/rstudio-server
   sudo invoke-rc.d apparmor reload 2>/dev/null
fi

# add systemd, upstart, or init.d script and start the server
if test "$INIT_SYSTEM" = "systemd"
then
   sudo systemctl stop rstudio-server.service 2>/dev/null
   sudo systemctl disable rstudio-server.service 2>/dev/null
   sudo cp ${CMAKE_INSTALL_PREFIX}/extras/systemd/rstudio-server.service /etc/systemd/system/rstudio-server.service
   sudo systemctl daemon-reload
   sudo systemctl enable rstudio-server.service
   sudo systemctl start rstudio-server.service
   sleep 1
   sudo systemctl --no-pager status rstudio-server.service
elif test $LSB_RELEASE = "Ubuntu" && test -d /etc/init/
then
   sudo cp ${CMAKE_INSTALL_PREFIX}/extras/upstart/rstudio-server.conf /etc/init/
   sudo initctl reload-configuration
   sudo initctl stop rstudio-server 2>/dev/null
   sudo initctl start rstudio-server
else
   sudo cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/debian/rstudio-server /etc/init.d/
   sudo chmod +x /etc/init.d/rstudio-server
   sudo update-rc.d rstudio-server defaults
   sudo /etc/init.d/rstudio-server stop  2>/dev/null
   sudo /etc/init.d/rstudio-server start
fi

# clear error termination state
set -e
