#!/bin/sh

# wget http://119.96.169.117:3006/kpl-release/release/plgrunner.sh -O plgrunner.sh && chmod +x plgrunner.sh
# openwrt 下面 searchid 把-ef去掉
# 自动监控运行
# /home/kpl00/kpl-3030/plgrunner.sh kpl-3030 :3030 plugs-conf.json
shpath=$(cd $(dirname $0);pwd)
echo "shell($0) path:$shpath"

applocal=$1
cmdaddr=$2
configfile=$3
killfirst=$4

echo "applocal:$applocal"
echo "cmdaddr=$cmdaddr"
echo "configfile=$configfile"
echo "killfirst=$killfirst"

if [ "$debugmode"x == ""x ]; then
   debugmode=0
fi

echo "debugmode=$debugmode"


if [ "$applocal"x == ""x ]; then
   echo "first param is emtpy!"
   exit
fi

if [ "$killfirst"x == "2"x ]; then
   ps -ef|grep $applocal |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |awk '{print $2}'|xargs kill -9
   exit;
fi

if [ "$killfirst"x == "1"x ]; then
   ps -ef|grep $applocal |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |awk '{print $2}'|xargs kill -9
fi

search_pid=`ps -ef|grep -v "grep"|grep -v "sh" |grep -v "rpxc-mipsle"|grep -c "$applocal"`
echo "search:$applocal: $search_pid"

if [ $search_pid -eq 0 ]  
then
  cd $shpath

  p1="-cmd-addr=$cmdaddr"
  p2="-config-file=$configfile"

  if [ "$cmdaddr"x == ""x ]; then
   p1=""
  fi

  if [ "$configfile"x == ""x ]; then
   p2=""
  fi

  echo "[cmdline]$shpath/$applocal $p1 $p2 -uuidfile=app.uuid -debugmode=$debugmode &"
  $shpath/$applocal $p1 $p2 -uuidfile=app.uuid -debugmode=$debugmode &
fi

