From 74dc91682c9bcaced8eee67475d26ad9015acece Mon Sep 17 00:00:00 2001
From: Marius Tolzmann <tolzmann@molgen.mpg.de>
Date: Tue, 3 Jan 2012 16:43:35 +0100
Subject: [PATCH] added mxservicectl and mxstartupctl

---
 mxservicectl | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++
 mxstartupctl |  23 +++++++++
 2 files changed, 155 insertions(+)
 create mode 100755 mxservicectl
 create mode 100755 mxstartupctl

diff --git a/mxservicectl b/mxservicectl
new file mode 100755
index 0000000..65d5bd4
--- /dev/null
+++ b/mxservicectl
@@ -0,0 +1,132 @@
+#!/bin/bash
+
+CMD_IP="/sbin/ip"
+CMD_MXS2MXSRV="/usr/sbin/mxstartup2mxconfig"
+
+RUNDIR="/run/mariux"
+
+# expands *-pattern in pathnames to null if no matching files are found..
+shopt -s nullglob
+
+# exit on any error
+set -e
+
+function mxsrv_start_one() {
+   local cfg=$1
+   local -i i mip mfwd
+
+   echo "starting ${cfg} .."
+
+   . ${cfg}
+
+   su - ${MX_SRV_USER} -c "${MX_SRV_SCRIPT} start" &
+
+   mv ${cfg}{,.r}
+
+   unset -v MX_SRV_USER MX_SRV_SCRIPT
+}
+
+function mxsrv_stop_one() {
+   local cfg=$1
+   local -i i mip mfwd
+
+   echo "stopping ${cfg} .."
+
+   . ${cfg}
+
+   su - ${MX_SRV_USER} -c "${MX_SRV_SCRIPT} stop" &
+
+   rm ${cfg}
+
+   unset -v MX_SRV_USER MX_SRV_SCRIPT
+}
+
+function mxsrv_start() {
+    local cfg
+    local pattern=$1
+
+    : ${pattern:=*}
+
+    for cfg in ${RUNDIR}/mxservice.${pattern}.cfg ; do
+        if [ -e ${cfg}.r ] ; then
+            echo >&2 "skipping $cfg: already running.."
+            continue
+        fi
+
+        if [ ! -r ${cfg} ] ; then
+            echo >&2 "skipping $cfg: can't read file"
+            continue
+        fi
+
+        if [ ! -O ${cfg} ] ; then
+            echo >&2 "skipping $cfg: possible hack attempt?"
+            continue
+        fi
+
+        mxsrv_start_one ${cfg}
+
+    done
+}
+
+function mxsrv_stop() {
+    local cfg
+    local pattern=$1
+
+    : ${pattern:=*}
+
+    for cfg in ${RUNDIR}/mxservice.${pattern}.cfg.r ; do
+        if [ ! -r ${cfg} ] ; then
+            echo >&2 "skipping $cfg: can't read file"
+            continue
+        fi
+
+        if [ ! -O ${cfg} ] ; then
+            echo >&2 "skipping $cfg: possible hack attempt?"
+            continue
+        fi
+
+        mxsrv_stop_one ${cfg}
+
+    done
+}
+
+
+function create_run_dir_if_not_exists() {
+    if [ ! -d $rundir ] ; then
+        mkdir -m 0700 ${RUNDIR}
+    fi
+
+    if [ ! -O $rundir ] ; then
+        echo >&2 "${RUNDIR}: wrong owner: possible hack attempt? exiting.."
+        exit 1
+    fi
+}
+
+function create_mxservice() {
+    ${CMD_MXS2MXSRV} ${RUNDIR} >/dev/null
+}
+
+
+##############################################################################
+
+create_run_dir_if_not_exists
+
+case "${1}" in
+    start)
+        create_mxservice
+        mxsrv_start $2
+        ;;
+    stop)
+        mxsrv_stop $2
+        ;;
+    restart)
+        mxsrv_stop $2
+        create_mxservice
+        mxsrv_start $2
+        ;;
+    *)
+        echo >&2 "$0 start|stop"
+        ;;
+esac
+
+
diff --git a/mxstartupctl b/mxstartupctl
new file mode 100755
index 0000000..39e9e31
--- /dev/null
+++ b/mxstartupctl
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+case "${1}" in
+    start)
+        mxvipctl     start $2
+        mxservicectl start $2
+        ;;
+    stop)
+        mxservicectl stop $2
+        mxvipctl     stop $2
+        ;;
+    restart)
+        mxservicectl stop $2
+        mxvipctl     stop $2
+        mxvipctl     start $2
+        mxservicectl start $2
+        ;;
+    *)
+        echo >&2 "$0 start|stop|restart <service>"
+        ;;
+esac
+
+