#!/bin/sh


# 这是一个自动更新脚本

appfn=gnss-app-mipsle.latest
applocal=$1
cmdaddr=$2
configfile=$3

if [ "$applocal"x == ""x ]; then
   applocal=kpl-robot
fi

if [ "$cmdaddr"x == ""x ]; then
   cmdaddr=:8081
fi

if [ "$configfile"x == ""x ]; then
   configfile=plugs-robot.json
fi

echo "p0:$0"
echo "applocal:$applocal"
echo "cmdaddr:$cmdaddr"
echo "configfile:$configfile"

if [ ! -f "/home/getRssi" ];then
  wget http://119.96.169.117:3000/kpl-release/release/getRssi -O /home/getRssi
fi

if [ ! -f "/home/$configfile" ];then
  wget http://119.96.169.117:3000/kpl-release/plug-conf/plugs-robot.json -O /home/$configfile
fi

if [ ! -f "/home/$configfile" ];then
  echo "/home/$configfile not found!!!"
  exit 1
fi

# 监控SD只读脚本
ps |grep sd-ro-monitor.sh |grep -v 'grep' |grep -v 'rpxc-mipsle' |awk '{print $1}'|xargs kill -9; wget http://119.96.169.117:3000/kpl-release/release/kpl-runner-sdcard.sh -O /overlay/kpl-runner-sdcard.sh

if [ ! -f "/overlay/kpl-runner-sdcard.sh" ];then
  wget http://119.96.169.117:3000/kpl-release/release/kpl-runner-sdcard.sh -O /overlay/kpl-runner-sdcard.sh
fi
chmod +x /overlay/kpl-runner-sdcard.sh

if [ ! -f "/overlay/sd-ro-monitor.sh" ];then
  wget http://119.96.169.117:3000/kpl-release/release/sd-ro-monitor.sh -O /overlay/sd-ro-monitor.sh
fi
chmod +x /overlay/sd-ro-monitor.sh


search_cnt=`crontab -l |grep -v 'grep' |grep -c kpl-runner-sdcard.sh`
echo "crontab-kpl-runner-sdcard:$search_cnt"
if [ $search_cnt -eq 0 ]
then
  echo "*/1 * * * * /overlay/kpl-runner-sdcard.sh" >>/etc/crontabs/root
  echo "*/1 * * * * /overlay/kpl-runner-sdcard.sh 加入到任务"
fi

# END 监控SD只读脚本

chmod +x /home/getRssi


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

localok=0
downok=0
if [ -f "$appfn.tar.gz" ];then
  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 'localfile md5 verify succ!'
    localok=1
    downok=1
  fi
fi

if [ "$localok"x == "0"x ]; then
  if [ -f "$appfn.tar.gz.bk" ];then
    localMD5=`md5sum $appfn.tar.gz.bk |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 'localfile md5 verify succ!'
      mv $appfn.tar.gz.bk $appfn.tar.gz
      localok=1
      downok=1
    fi
  fi
fi

downcnt=0

if [ "$localok"x == "0"x ]; then
  sleep 2s
  #
  # rm -rf $appfn.tar.gz
  rm -rf $appfn
  wget -c "http://119.96.169.117:3000/kpl-release/release/$appfn.tar.gz"
  downret=$?
  if [ "$downret"x == "0"x ]; then
    downok=1
  else
    TIMEIDSTR=$(date "+%Y%m%d%H%M%S")
    echo "download $TIMEIDSTR faile:$downret" >> up.log
    exit
  fi
  TIMEIDSTR=$(date "+%Y%m%d%H%M%S")
  echo "download $TIMEIDSTR result:$downret" >> up.log

  sleep 1s
  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! try again!'

    rm -rf $appfn.tar.gz
    wget -c "http://119.96.169.117:3000/kpl-release/release/$appfn.tar.gz"
    downret=$?
    if [ "$downret"x == "0"x ]; then
      downok=1
      localok=1
    else
      TIMEIDSTR=$(date "+%Y%m%d%H%M%S")
      echo "download $TIMEIDSTR faile:$downret" >> up.log
      exit
    fi

  elif [ -f "$appfn.tar.gz" ];then
    echo 'md5 verify succ!'
    localok=1
  fi
fi

if [ "$localok"x == "1"x ]; then
  rm -rf $applocal

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

  # 下载完成备份文件
  if [ "$downok"x == "1"x ]; then
    mv $appfn.tar.gz $appfn.tar.gz.bk
  fi

  echo '解压文件$appfn 到$applocal'
  cp $appfn $applocal
  chmod +x $applocal
  
  search_cnt=`ps |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-' |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-' |awk '{print $1}'|xargs kill -9
    echo "$applocal killed"
  fi


  ps |grep mgr |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-' |awk '{print $1}'|xargs kill -9
  ps |grep i2o |grep -v 'grep' |grep -v 'sh' |grep -v 'rpxc-' |awk '{print $1}'|xargs kill -9
  echo 'mgr killed'
  
  # ./$applocal -cmd-addr=:8081 -config-file=plugs-robot.json -uuidfile=app.uuid &
  ./$applocal -cmd-addr=$cmdaddr -config-file=$configfile -uuidfile=app.uuid &
fi

