#!/bin/sh

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

ln -f -s "${CMAKE_INSTALL_PREFIX}/shiny-server/bin/shiny-server" /usr/bin/shiny-server
# See if "shiny" user exists
if id -u shiny >/dev/null 2>&1;
then
   echo User "shiny" already exists. Ensuring proper permissions on /home/shiny/.
   mkdir -p /home/shiny
   chown shiny:shiny /home/shiny
else
   echo Creating user "shiny"
   useradd -r -m shiny
fi

if [ ! -d "/srv/shiny-server" ];
then
   mkdir -p /srv/shiny-server
   # And seed with initial apps and index.html
   ln -s ${CMAKE_INSTALL_PREFIX}/shiny-server/samples/welcome.html /srv/shiny-server/index.html
   ln -s ${CMAKE_INSTALL_PREFIX}/shiny-server/samples/sample-apps /srv/shiny-server/sample-apps
fi

mkdir -p /etc/shiny-server
if [ ! -f "/etc/shiny-server/shiny-server.conf" ];
then
   cp ${CMAKE_INSTALL_PREFIX}/shiny-server/config/default.config /etc/shiny-server/shiny-server.conf
fi

mkdir -p /var/log/shiny-server

# Place the logrotate script, if applicable
if test -d /etc/logrotate.d
then
   cp ${CMAKE_INSTALL_PREFIX}/shiny-server/config/logrotate /etc/logrotate.d/shiny-server
fi

# Log dir must be writable by "shiny" user
chown shiny:shiny /var/log/shiny-server

mkdir -p /var/lib/shiny-server

# check lsb release
LSB_RELEASE=`lsb_release --id --short`

# # add apparmor profile
# if test $LSB_RELEASE = "Ubuntu" && test -d /etc/apparmor.d/
# then
#    cp ${CMAKE_INSTALL_PREFIX}/extras/apparmor/rstudio-server /etc/apparmor.d/
#    apparmor_parser -r /etc/apparmor.d/rstudio-server 2>/dev/null
# fi

# Ubuntu needs help setting LANG which we'll do in the Upstart script by 
# injecting it into the script here.
if [ -e /etc/default/locale ]; then
   . /etc/default/locale
fi
 if [ $LANG ] && [ "$LANG" != "C" ]; then
   # $LANG exists and is set. Just use it
   SS_LANG=$LANG
else 
   # $LANG is not set, we need to infer it.
   if (locale -a | grep -e '^C.UTF-8$' > /dev/null); then
      # We have C.UTF-8, use it.
      SS_LANG="C.UTF-8"
   else 
      SS_LANG="en_US.UTF-8"
   fi
fi

# add upstart profile or init.d script and start the server
INIT_SYSTEM=`cat /proc/1/comm`
if test $INIT_SYSTEM = "systemd"
then
   systemctl stop shiny-server.service 2>/dev/null
   systemctl disable shiny-server.service 2>/dev/null
   cp ${CMAKE_INSTALL_PREFIX}/shiny-server/config/systemd/shiny-server.service /etc/systemd/system/shiny-server.service
   if ! grep -e "^Environment=\"LANG=" /etc/systemd/system/shiny-server.service; then
      echo "Adding LANG to /etc/systemd/system/shiny-server.service, setting to $SS_LANG"
      sed -i "11 a Environment=\"LANG=$SS_LANG\"" /etc/systemd/system/shiny-server.service
   fi
   systemctl daemon-reload
   systemctl enable shiny-server.service
   systemctl start shiny-server.service
   systemctl --no-pager status shiny-server.service
elif (test $LSB_RELEASE = "Ubuntu" || test $LSB_RELEASE = "LinuxMint") && test -d /etc/init/
then
   cp ${CMAKE_INSTALL_PREFIX}/shiny-server/config/upstart/shiny-server.conf /etc/init/

   if ! grep -e "^env LANG=" /etc/init/shiny-server.conf > /dev/null; then
      echo "Adding LANG to /etc/init/shiny-server.conf, setting to $SS_LANG"
      sed -i "1 a env LANG='$SS_LANG'" /etc/init/shiny-server.conf
   fi

   initctl reload-configuration
   initctl stop shiny-server 2>/dev/null
   initctl start shiny-server
else
   cp ${CMAKE_INSTALL_PREFIX}/shiny-server/config/init.d/debian/shiny-server /etc/init.d/

   if ! grep -e "^export LANG=" /etc/init/shiny-server.conf > /dev/null; then
      echo "Adding LANG to /etc/init.d/shiny-server, setting to $SS_LANG"
      sed -i "10 a export LANG='$SS_LANG'" /etc/init.d/shiny-server
   fi

   chmod +x /etc/init.d/shiny-server
   update-rc.d shiny-server defaults
   /etc/init.d/shiny-server stop  2>/dev/null
   /etc/init.d/shiny-server start
fi

# clear error termination state
set -e
