#!/bin/sh

daemonCmd() {
  INIT_SYSTEM=`cat /proc/1/comm 2>/dev/null`
  if test "$INIT_SYSTEM" = "systemd"
  then
     eval "systemctl $1 rstudio-server.service"
  elif test -e /etc/init/rstudio-server.conf
  then
     eval "initctl $1 rstudio-server"
  else
     eval "/etc/init.d/rstudio-server $1"
  fi
  return $?
}

testConfig() {
 `${CMAKE_INSTALL_PREFIX}/bin/rserver --test-config`
}

verifyInstallation() {
 `${CMAKE_INSTALL_PREFIX}/bin/rserver --verify-installation=1 --server-daemonize=0`
}


case "$1" in
    
    status)
        daemonCmd "status"
        ;;
    
    start)
        testConfig || return $?
        daemonCmd "start"
        ;;

    stop)
        daemonCmd "stop"
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rsession
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rworkspaces 2>/dev/null
        ;;

    restart)
        testConfig || return $?
        daemonCmd "restart"
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rsession
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rworkspaces 2>/dev/null
        ;;

    test-config)
        testConfig
        ;;

    verify-installation)
        daemonCmd "stop" 2>/dev/null
        verifyInstallation
        daemonCmd "start"
        ;;

    suspend-session)
        if test -n $2
        then
           kill -s USR1 $3 $4 $5 $6 $7 $8 $9 $2
        else
           echo "Must specify PID of session to suspend"
           exit 1
        fi
        ;;

    suspend-all)
        killall -s USR1 $2 $3 $4 $5 $6 $7 $8 $9 rsession
        killall -s USR1 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces 2>/dev/null
        ;;

    force-suspend-session)
        if test -n $2
        then
           kill -s USR2 $3 $4 $5 $6 $7 $8 $9 $2
        else
           echo "Must specify PID of session to force suspend"
           exit 1
        fi
        ;;

    force-suspend-all)
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rsession
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rworkspaces 2>/dev/null
        ;;

    kill-session)
        if test -n $2
        then
           kill -s KILL $3 $4 $5 $6 $7 $8 $9 $2
        else
           echo "Must specify PID of session to kill"
           exit 1
        fi
        ;;

    kill-all)
        killall -s KILL $2 $3 $4 $5 $6 $7 $8 $9  rsession
        killall -s KILL $2 $3 $4 $5 $6 $7 $8 $9  rworkspaces 2>/dev/null
        ;;

    offline)
        mkdir -p /var/lib/rstudio-server
        touch /var/lib/rstudio-server/offline
        daemonCmd "restart"
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rsession
        killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9  rworkspaces
        ;;

    online)
        mkdir -p /var/lib/rstudio-server
        rm -f /var/lib/rstudio-server/offline
        daemonCmd "restart"
        ;;

    active-sessions)
        ps opid,cputime,args -C "rsession"
        ;;

    version)
        echo "${CPACK_PACKAGE_VERSION}"
        ;;

    *)
        echo $"Usage: rstudio-server {status|start|stop|restart|test-config|verify-installation|suspend-session|suspend-all|force-suspend-session|force-suspend-all|kill-session|kill-all|offline|online|active-sessions|version}"
        exit 2
esac



