#!/bin/sh


# 这是一个文件下载脚本
# kplgettar.sh tarfilename localpath

# 文件路径
httpprefix=http://119.96.169.117:3000/kpl-release/release/

tarfilename=$1
localpath=$2

if [ "$localpath"x == ""x ]; then
   localpath=./
fi


if [ "$tarfilename"x == ""x ]; then
   echo ' tarfilename is empty'
   exit
fi

# 比较MD5
rm -rf $tarfilename.md5
echo "download $httpprefix$tarfilename.md5"
wget "$httpprefix$tarfilename.md5"

localok=0
if [ -f "$tarfilename" ];then
  localMD5=`md5sum $tarfilename |awk 'NR==1'|awk '{print $1}'`
  mustMD5=`cat $tarfilename.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 $tarfilename
  wget -c "$httpprefix$tarfilename"
  sleep 1s
  localMD5=`md5sum $tarfilename |awk 'NR==1'|awk '{print $1}'`
  mustMD5=`cat $tarfilename.md5 |awk '{print $1}'`
  echo " local: $localMD5"
  echo "expect: $mustMD5"
  if [ "$localMD5"x != "$mustMD5"x ]; then
    echo 'md5 verify fail!'
  elif [ -f "$tarfilename" ];then
    echo 'md5 verify succ!'
    localok=1
  fi
fi

if [ "$localok"x == "1"x ]; then
  tar -xvzf $tarfilename -C $localpath
  echo 'unzip succ'
fi

