#!/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 group "shiny"
   groupadd shiny

   echo Creating user "shiny"
   useradd -r -m shiny -g shiny -s /bin/sh
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


# Do not remove!
%posttrans
${RPM_POSTTRANS_SCRIPT}