#!/bin/bash
# Usage:
# ./frontEndUpgrade.sh html.zip "/etc/nginx" "/home/admin.hq" "production/investigate/frontEnd"
# ./frontEndUpgrade.sh html.zip "/var/www" "/home/ubuntu" "stage/investigate/frontEnd"
fileName=$1  # html.zip zhuize.zip
projectPath=$2  # /etc/nginx  /var/www
workPath=$3
ossLogPath=$4
user=$USER

protal=oss
basePath=/var/tmp/autoDeploy

if [ -n $5 ]; then
  user=$5
fi

if [ ! -d $basePath ]; then
  mkdir $basePath -p
fi

if [ ! -d $workPath ]; then
  echo workPath $workPath not exist!
  exit 1
fi

PATH=PATH:/usr/bin:/usr/sbin:/usr/local/bin
projectName="${fileName%.*}" # html, zhuize
preName="${fileName%.*}"
! 
function uploadLog()
{
  ctype=$1
  identify=$2
  count=$3
  echo "current user is ${USER}"
  journalctl -n $count -xe -${ctype} $identify > ${workPath}/${identify}.log
  ossutil cp -f ${workPath}/${identify}.log  ${protol}://${ossLogPath}/logs/${identify}.log
  if [ $? -eq 0 ]; then
    echo "ok: log ${identify} uploaded to oss!"
    return 0
  else
    echo "error: upload log ${identify} to oss failed!"
    return 1
  fi
}


function extractHere() {
  cd $projectPath
  rm $fileName -f | true
  rm $projectName -rf | true
  cp $workPath/$fileName .
  unzip -oq $fileName 
  mv dist $projectName
  echo "user: $user"
  chgrp $user $projectName -R
  chown $user $projectName -R
  cp ${workPath}/${fileName}_oss.info $projectName
  sleep 5
  md5Sum=$(md5sum $fileName)
  md5Sum="${md5Sum%  *}"
  echo ${md5Sum} > ${projectName}/${fileName}_md5.txt 
}

function init(){
  cd $workPath
  if [ ! -f "$fileName" ];then
    echo "file $fileName not exist in $workPath !"
    # cd $projectPath
    # mkdir dist
    # echo "helo" > helo.txt
    # mv helo.txt dist
    # zip -r $fileName dist
    exit 0
  fi
  cd $workPath
  if [ ! -d "$projectPath" ]; then
    echo "path $projectPath exist! "
    exit 0
  fi

  rm dist -fr | true
  rm $projectName -fr | true

  cd $projectPath
  if [ ! -d "$projectName" ];then
    extractHere
  fi 
  if [ ! -f "$fileName" ];then
    extractHere
  fi 
}

function compareMd5(){
  cd $workPath
  oriSum=$(md5sum $fileName)
  oriSum="${oriSum%  *}"
  echo "locTemp file ${fileName} md5sum: ${oriSum}" 

  cd $projectPath
  desSum=$(md5sum $fileName)
  desSum="${desSum%  *}"
  echo "project Path ${fileName} md5sum: ${desSum}"

  if [ ${oriSum} = ${desSum} ];then
    return 0
  else 
    return 1
  fi
}

function replaceFile(){
  cd $projectPath
  if [ -f "$fileName"_bak2 ];then
    rm "${fileName}"_bak2 -fr
  fi
  if [ -f "${fileame}"_bak1 ];then
    mv ${fileName}_bak1 ${fileName}_bak2
  fi
  if [ -f "$fileName"_bak ];then
    mv ${fileName}_bak ${fileName}_bak1
  fi
  if [ -f "$fileName" ];then
    mv ${fileName} ${fileName}_bak
  fi
  extractHere
  return 0
}

function verify() {
  cd /tmp/autoDeploy
  cd $projectPath/${preName}
  echo "the md5sum injected in ${projectPath}/${preName} is $(cat ${fileName}_md5.txt)"
}

function main() {
  echo " "
  sleep 5
  echo "frontEndUpgrade start"
  init
  compareMd5
  result=$?
  if [ ${result} = 0 ];then
    echo "local temp file ${fileName} is same with ${projectPath},no need to replace"
    echo "ok: frontEndUpgrade done"
    echo " "
    exit 0
  else 
    echo "local temp file $fileName is different from ${projectPath}, will replace..."
  fi
  replaceFile
  result=$?
  if [ $result = 0 ];then 
    echo "ok: replaced done"
  else
    echo "error: replace faild"
    exit 0
  fi
  sleep 3
  verify
  echo " "
  uploadLog t autoDeploy 100
}

main