# Reset Kurt packaging temporary folders
# ======================================================
export TMP_KURT=/tmp/kurt
rm -rf $TMP_KURT


export TMP_PACKAGING_KURT=$TMP_KURT/package
export TMP_SOURCE_KURT=$TMP_KURT/source
export TMP_UPLOAD_KURT=$TMP_KURT/upload
export TMP_DEPLOY_KURT=$TMP_KURT/deploy

mkdir -p $TMP_PACKAGING_KURT
mkdir -p $TMP_SOURCE_KURT
mkdir -p $TMP_UPLOAD_KURT
mkdir -p $TMP_DEPLOY_KURT

export HOST="10.24.192.36"

export BUILD_FILENAME="build_infos.txt"
export BUILD_FILE="${TMP_PACKAGING_KURT}/${BUILD_FILENAME}"

cd /tmp
echo "Build information:" > ${BUILD_FILE}
echo " - Job name: ${JOB_NAME}" >> ${BUILD_FILE}
echo " - Reference: ${BUILD_ID}" >> ${BUILD_FILE}
echo " - Number: ${BUILD_NUMBER}" >> ${BUILD_FILE}
echo " - SVN revision: ${SVN_REVISION}" >> ${BUILD_FILE}
echo " - Jenkins URL: ${BUILD_URL}" >> ${BUILD_FILE}
echo " - Workspace: ${WORKSPACE}" >> ${BUILD_FILE}

# Group sources
# ======================================================
cp ${WORKSPACE}/kurt-ejb/target/kurt-ejb-*-sources.jar $TMP_SOURCE_KURT
cp ${WORKSPACE}/jasper/target/jasper-*-sources.jar $TMP_SOURCE_KURT
cp ${WORKSPACE}/kurt-utility/target/kurt-utility-*-sources.jar $TMP_SOURCE_KURT
cp ${WORKSPACE}/kurt-web/target/kurt-web-*-sources.jar $TMP_SOURCE_KURT
cp ${WORKSPACE}/teamcenter/target/teamcenter-*-sources.jar $TMP_SOURCE_KURT

# Create source package for report WebApp
cd $TMP_SOURCE_KURT
zip -r ${TMP_PACKAGING_KURT}/snecma-reports-${TAG_VERSION}-sources.zip .

# Deploy policies on Teamcenter
# ======================================================
cd ${WORKSPACE}/policies
ftp -n ${HOST} <<END_SCRIPT
quote USER policies
quote PASS policies
binary
prompt off
mput *.xml
quit
END_SCRIPT


# Archive EAR, JBoss configuration and reporting.properties
# ======================================================
cp $WORKSPACE/core-ear/target/core-ear-${TAG_VERSION}.ear ${TMP_PACKAGING_KURT}/snecma-reports-${TAG_VERSION}.ear
cp $WORKSPACE/standalone-pv-reports.xml ${TMP_PACKAGING_KURT}/standalone-pv-reports.xml
cp $WORKSPACE/kurt-utility/src/main/resources/reporting.properties ${TMP_PACKAGING_KURT}/reporting.properties


# Update properties for Snecma
# ======================================================
cd ${TMP_PACKAGING_KURT}
sed -i 's/devmode=true/devmode=false/g' reporting.properties
sed -i 's/infodba/TO_BE_DEFINED/g' reporting.properties
sed -i 's/localhost:8080/HOSTNAME_TO_BE_DEFINED:PORT/g' reporting.properties
sed -i 's/defaultExpeditorMail=reporting.tool@capgemini.com/defaultExpeditorMail=snm.support_teamcenter@snecma.fr/g' reporting.properties
sed -i 's/defaultDestinatorMail=/defaultDestinatorMail=thierry.petit@snecma.fr/g' reporting.properties
sed -i 's/defaultSmtpHost=smtp.capgemini.fr/defaultSmtpHost=smtp.bsc.snecma/g' reporting.properties
sed -i 's/item_id=Item ID/item_id=ItemID/g' reporting.properties
sed -i 's/http:\/\/10.24.192.36:80\/tc/TO_BE_DEFINED/g' reporting.properties
sed -i 's/C:\/Users\/Public\/kurt\/Jasper\/output\//TO_BE_DEFINED/g' reporting.properties
sed -i "s/report.buildDate=/report.buildDate=${BUILD_ID}/g" reporting.properties


# Group in a unique ZIP package archive
# ======================================================
cd ${TMP_PACKAGING_KURT}
zip -r ${TMP_UPLOAD_KURT}/package-snecma-reports-${TAG_VERSION}.zip .


echo "======================================================"
echo "Deploy following files through FTP:"
echo "======================================================"
ls $TMP_UPLOAD_KURT

cd ${TMP_UPLOAD_KURT}
ftp -n ${HOST} <<END_SCRIPT
quote USER pvreport
quote PASS pvreport
binary
cd packages
put package-snecma-reports-${TAG_VERSION}.zip
quit
END_SCRIPT


# Replace properties with validation values
# ======================================================
cd $WORKSPACE/kurt-utility/src/main/resources/
sed -i 's/devmode=true/devmode=false/g' reporting.properties
sed -i 's/localhost:8080/frpar-sn-rep:8080/g' reporting.properties
sed -i 's/defaultDestinatorMail=/defaultDestinatorMail=magali.boisgard@capgemini.com,cecile.laborde.externe@capgemini.com/g' reporting.properties
sed -i "s/report.buildDate=/report.buildDate=${BUILD_ID}/g" reporting.properties

# Send reporting.properties
# ======================================================
cd $WORKSPACE/kurt-utility/src/main/resources/
ftp -n ${HOST} <<END_SCRIPT
quote USER pvreport
quote PASS pvreport
cd configuration
put reporting.properties
quit
END_SCRIPT

# Send deployed web application and the token file
# ======================================================
cp $WORKSPACE/core-ear/target/core-ear-${TAG_VERSION}.ear ${TMP_DEPLOY_KURT}/snecma-reports.ear
echo "Token for Jenkins deployment: " > ${TMP_DEPLOY_KURT}/deploy.token
date >> ${TMP_DEPLOY_KURT}/deploy.token
cd $TMP_DEPLOY_KURT
ftp -n ${HOST} <<END_SCRIPT
quote USER pvreport
quote PASS pvreport
binary
cd packages
cd deploy
put snecma-reports.ear
put deploy.token
quit
END_SCRIPT


# TAG release
# ======================================================

# Disable exit on error
set +e

export SVN_LOGIN=svoise
export SVN_PASSWORD=*******

export SVN_TAG_PATH="https://coconet-svn-fr-01.capgemini.com/svn/repos/pv-reports/tags/kurt_$TAG_VERSION"

# Delete tag if it already exist
svn info ${SVN_TAG_PATH} --no-auth-cache --username=${SVN_LOGIN} --password=${SVN_PASSWORD}
IS_EXISTING=$?
if [ $IS_EXISTING -ne 0 ]
then
    echo "Tag does not exist yet"
else
    echo "Remove existing tag"
    svn rm ${SVN_TAG_PATH} -m "Automatic delete from Jenkins - version: $TAG_VERSION" --no-auth-cache --username=${SVN_LOGIN} --password=${SVN_PASSWORD}
fi

# Enable exit on error
set -e

# Tag current head version in SVN
svn copy https://coconet-svn-fr-01.capgemini.com/svn/repos/pv-reports/trunk/kurt ${SVN_TAG_PATH} -m "Automatic release from Jenkins - version: $TAG_VERSION" --no-auth-cache --username=${SVN_LOGIN} --password=${SVN_PASSWORD}

echo "Tag has been created"
