#!/bin/bash

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() {
 `/usr/local/lib/rstudio/bin/rserver --test-config`
}

verifyInstallation() {
 /usr/local/lib/rstudio/bin/rserver --verify-installation=1 --server-daemonize=0 $1 $2 $3 $4 $5 $6 $7 $8
}

isServerRunning() {
 pgrep -x rserver &> /dev/null
}


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

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

    restart)
        testConfig || exit $?
        daemonCmd "restart"
        if (pidof rsession >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        if (pidof rworkspaces >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        ;;

    test-config)
        testConfig
        ;;

    verify-installation)
        if isServerRunning ; then
           echo "Server is running and must be stopped before running verify-installation"
           exit 1
        fi

        # we call stop here even though the service should already be stopped
        # just in case (we don't want the service running when invoking verify-installation)
        daemonCmd "stop" 2>/dev/null

        verifyInstallation $2 $3 $4 $5 $6 $7 $8 $9
        ;;

    suspend-session)
        if [ -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)
        if (pidof rsession >/dev/null); then killall -s USR1 $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        if (pidof rworkspaces >/dev/null); then killall -s USR1 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        ;;

    force-suspend-session)
        if [ -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)
        if (pidof rsession >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        if (pidof rworkspaces >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        ;;

    kill-session)
        if [ -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)
        if (pidof rsession >/dev/null); then killall -s KILL $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        if (pidof rworkspaces >/dev/null); then killall -s KILL $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        ;;

    offline)
        mkdir -p /var/lib/rstudio-server
        touch /var/lib/rstudio-server/offline
        daemonCmd "restart"
        if (pidof rsession >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        if (pidof rworkspaces >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        ;;

    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 "99.9.9 (Middlemist Red) for Unknown OS"
        ;;

    license-manager)
        echo "This open-source edition of RStudio Server does not require activation. Download and install RStudio Server Pro to use a licensed version of RStudio Server."
        exit 1
        ;;

    *)
        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



