# #!/bin/bash
branchName=$2
url=$1
zipUrl=$3
commitMessage=$8

unique=$(date +"%d_%m_%y_Time_%H_%M_%S")
publishFolder=$branchName"_"$unique

rm -rf ./reports
rm -rf ./scrTemplate
rm -rf ./errTemplate
rm -rf ./css
rm -rf ./js
rm -rf ./index.html

curl $zipUrl | tar xz

cp -rf ./reports/css ./css
cp -rf ./reports/js ./js
cp -rf ./reports/index.html ./index.html

if [ -d "./screenShots" ] ; then
  cp -rf ./scrTemplate/* ./screenShots/
else
  mkdir ./screenShots
  cp -rf ./scrTemplate/* ./screenShots/

fi

[ ! -d "./commitCoverage" ] && mkdir commitCoverage && cp -rf ./errTemplate/* ./commitCoverage/
[ ! -d "./unittest" ] && mkdir unittest && cp -rf ./errTemplate/* ./unittest/
[ ! -d "./coverageTest" ] && mkdir coverageTest && cp -rf ./errTemplate/* ./coverageTest/
[ ! -d "./coverage" ] && mkdir coverage && cp -rf ./errTemplate/* ./coverage/

tar -czvf $publishFolder.tar.gz coverage screenShots unittest coverageTest commitCoverage ./css ./js ./index.html

curl -i -F name=file -F file=@$publishFolder.tar.gz $url"/cgi-bin/upload.py"
replace=$publishFolder
reportUrl=$url"/"$replace
subject="Client Report - React - $publishFolder"
msg="<p><b>Report URL - <a href='$reportUrl'>Link</a></b></p>
<p><b>Report Branchname - $branchName</b></p>
<p><b>Report Unique ID - $unique</b></p>
<p><b>Report Developer - $6</b></p>
<p><b>Report commitMessage - $commitMessage</b></p>"
#node mailSender.js <from> <pass> <to> <subject> <text>
BASEDIR=$(dirname "$0")
node $BASEDIR"/lib/mailSender" $4 $5 $6 "$subject" "$msg" $7
