#!/bin/bash
cd /usr/lib/luisvmf.com-qimon
./clearprocesses
echo "Try run Qimon in existing namespace"
pidlistsrnstestluisvmfcom=$(pidof /usr/lib/luisvmf.com-spectraread/nstest)
#Here we check if Spectraread is already running and enter the namespace for running another instance, in this case exit code is different than 1 after pressing close button, and we exit.
for pidsrnstestluisvmfcom in $pidlistsrnstestluisvmfcom; do ./enternsmock /proc/$pidsrnstestluisvmfcom/ns/user /proc/$pidsrnstestluisvmfcom/ns/pid /proc/$pidsrnstestluisvmfcom/ns/uts /proc/$pidsrnstestluisvmfcom/ns/mnt a; if [ $? != 1 ]; then exit; break; fi; done

echo "Try run Qimon in existing namespace"
pidlistsrnstestluisvmfcom=$(pidof /usr/lib/luisvmf.com-qimon/nstest)
#Here we check if Qimon is already running and enter the namespace for running another instance, in this case exit code is different than 1 after pressing close button, and we exit.
for pidsrnstestluisvmfcom in $pidlistsrnstestluisvmfcom; do ./enternsmock /proc/$pidsrnstestluisvmfcom/ns/user /proc/$pidsrnstestluisvmfcom/ns/pid /proc/$pidsrnstestluisvmfcom/ns/uts /proc/$pidsrnstestluisvmfcom/ns/mnt a; if [ $? != 1 ]; then exit; break; fi; done

#If we get here, we haven't run exit() yet, which means we were unable to enter a namespace because another instance of Qimon was not open, lets run Qimon...
echo "Run Qimon creating namespace"
/usr/lib/luisvmf.com-qimon/nstest /usr/lib/luisvmf.com-Runtimelibs/Runtime 1 1 1 1 /opt/qimon/bin/qimon-mock-run
