#!/bin/sh

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

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

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

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

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

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

# alias libcrypto.so.0.9.8 to libcrypto.so.6 for compatiblity with
# rpms built on redhat 5
if test -f /etc/SuSE-release
then
  if test -d /usr/lib64
  then
     ln -s /usr/lib64/libssl.so.0.9.8 ${CMAKE_INSTALL_PREFIX}/bin/libssl.so.6
     ln -s /usr/lib64/libcrypto.so.0.9.8 ${CMAKE_INSTALL_PREFIX}/bin/libcrypto.so.6
  else
     ln -s -f /usr/lib/libssl.so.0.9.8 ${CMAKE_INSTALL_PREFIX}/bin/libssl.so.6
     ln -s -f /usr/lib/libcrypto.so.0.9.8 ${CMAKE_INSTALL_PREFIX}/bin/libcrypto.so.6
  fi
fi

# add systemd, upstart, or init.d script and start the server
INIT_SYSTEM=`cat /proc/1/comm 2>/dev/null`
if test "$INIT_SYSTEM" = "systemd"
then
   # remove any previously existing init.d based scheme
   service rstudio-server stop 2>/dev/null
   rm -f /etc/init.d/rstudio-server
   
   systemctl stop rstudio-server.service 2>/dev/null
   systemctl disable rstudio-server.service 2>/dev/null
   cp ${CMAKE_INSTALL_PREFIX}/extras/systemd/rstudio-server.redhat.service /etc/systemd/system/rstudio-server.service
   systemctl daemon-reload
   systemctl enable rstudio-server.service
   systemctl start rstudio-server.service
   sleep 1
   systemctl --no-pager status rstudio-server.service
elif test -d /etc/init/
then
   # remove any previously existing init.d based scheme
   service rstudio-server stop 2>/dev/null
   rm -f /etc/init.d/rstudio-server

   cp ${CMAKE_INSTALL_PREFIX}/extras/upstart/rstudio-server.redhat.conf /etc/init/rstudio-server.conf
   initctl reload-configuration
   initctl stop rstudio-server 2>/dev/null
   initctl start rstudio-server
else
   if test -f /etc/SuSE-release
   then
      cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/suse/rstudio-server /etc/init.d/
   else
      cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/redhat/rstudio-server /etc/init.d/
   fi
   chmod +x /etc/init.d/rstudio-server
   chkconfig --add rstudio-server
   service rstudio-server stop 2>/dev/null
   service rstudio-server start
fi

# add pam profile
if [ ! -e /etc/pam.d/rstudio ]
then
   cp ${CMAKE_INSTALL_PREFIX}/extras/pam/rstudio /etc/pam.d/
fi

# clear error termination state
set -e
