#!/bin/sh


# 这是一个自动更新脚本

appfn=embed-websvr.latest
applocal=$1


if [ "$applocal"x == ""x ]; then
   applocal=embed-websvr
fi


echo "p0:$0"
echo "applocal:$applocal"


rm -rf $appfn.tar.gz
wget "http://119.96.169.117:3000/kpl-release/release/$appfn.tar.gz"

# 比较MD5
rm -rf $appfn.md5
wget "http://119.96.169.117:3000/kpl-release/release/$appfn.md5"

localMD5=`md5sum $appfn.tar.gz |awk 'NR==1'|awk '{print $1}'`
mustMD5=`cat $appfn.md5 |awk '{print $1}'`
echo " local: $localMD5"
echo "expect: $mustMD5"
if [ "$localMD5"x != "$mustMD5"x ]; then
  echo 'md5 verify fail!'
elif [ -f "/home/$appfn.tar.gz" ];then
  echo 'md5 verify succ!'
  rm -rf $applocal

  echo '解压文件$appfn.tar.gz'
  tar -xvzf $appfn.tar.gz -C /home/

  echo '解压文件$appfn 到$applocal'
  cp $appfn $applocal
  chmod +x $applocal
  
  search_cnt=`ps |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |grep -c $applocal`
  echo "cnt:$search_cnt"
  if [ $search_cnt -gt 0 ]
  then  
    ps |grep $applocal |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |awk '{print $1}'|xargs kill -9
    echo "$applocal killed"
  fi


  ps |grep mgr |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |awk '{print $1}'|xargs kill -9
  ps |grep i2o |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |awk '{print $1}'|xargs kill -9
  echo 'mgr killed'

  ./$applocal -uuidfile=app.uuid &
fi

