#!/bin/sh

#-------------------------------------------------------------------------------------------------
# Copyright (c) 2013 LilyView Team: <https://lilyviewapp.com>
# Some rights reserved: <https://opensource.org/licenses/mit-license.php>
#-------------------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------------------
# Bash script to relaunch the app

processPath=$1
processId=$2
processBundleId=$3
bundlePath=$4

# waiting cycle
i="10"
while [ $i -gt 0 ]
do
# wait one second
sleep 0.5

# check if running
instanceCount=$(ps -p $processId 2>/dev/null | grep $processPath | wc -l) 

if [ $instanceCount -gt 0 ]
then
# wait again
# keep quiet
#echo Still running $instanceCount instances of application, $i attempts left;
i=$(($i - 1))

else
# exit cycle
i="0"
fi
done

# kill the app
# keep quiet
#echo Killing application "$bundlePath" > ~/oops.txt
rm -r "$bundlePath"


