#!/bin/sh
if [ ! -f "/home/$1" ]; then
cd /home&&wget http://119.96.169.117:3000/kpl-release/release/$1
chmod 777 $1
fi
cd /home&&rm -rf $2.md5
wget "http://119.96.169.117:3000/kpl-release/release/$2.md5"

localok=0
if [ -f "/home/$2" ];then
  localMD5=`md5sum $2 |awk 'NR==1'|awk '{print $1}'`
  mustMD5=`cat $2.md5 |awk '{print $1}'`
  echo " local: $localMD5"
  echo "expect: $mustMD5"
  if [ "$localMD5"x == "$mustMD5"x ]; then
    echo 'localfile md5 verify succ!'
    localok=1
  fi
fi


if [ "$localok"x == "0"x ]; then
  sleep 2s
  rm -rf $2
  cd /home&&wget -c "http://119.96.169.117:3000/kpl-release/release/$2"
  sleep 1s
  localMD5=`md5sum $2 |awk 'NR==1'|awk '{print $1}'`
  mustMD5=`cat $2.md5 |awk '{print $1}'`
  echo " local: $localMD5"
  echo "expect: $mustMD5"
  if [ "$localMD5"x != "$mustMD5"x ]; then
    echo 'md5 verify fail!'
  elif [ -f "/home/$2" ];then
    echo 'md5 verify succ!'
    localok=1
  fi
fi

wget "http://127.0.0.1:8081/plug/gnss-data/do?action=5&enable=-1"
for i in `seq 5`
do
startTime=`date +%s`
./$1 -d /dev/ttyS2 -b 115200 -w 115200 -p $2  -reset
endTime=`date +%s`
sumTime=$(( $endTime - $startTime ))
if [ $sumTime -lt 300 ] ;then
        continue
else
        break
fi
done
