#!/bin/bash



# 这是一个自动更新脚本

appfn=$2
applocal=$1

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

if [ "$applocal"x == ""x ]; then
   echo 'applocal is null'
   exit
fi

if [ "$appfn"x == ""x ]; then
   echo 'appfn is null'
   exit
fi


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

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

localMD5=`md5sum $appfn |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 "$appfn" ];then
  echo 'md5 verify succ!'
  rm -rf $applocal
  cp $appfn $applocal
  chmod +x $applocal

  search_cnt=`ps -ef|grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |grep -c $applocal`
  echo "cnt:$search_cnt"
  if [ $search_cnt -gt 0 ]
  then
    ps -ef|grep $applocal |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-mipsle' |awk '{print $2}'|xargs kill -9
    echo "$applocal killed"
    sleep 2s
  fi

  ./$applocal &
fi

