# !/bin/bash -ex

#TCK env
export TCK_HOME=${WORKSPACE}
export TS_HOME=${WORKSPACE}/xml-registries-tck
export deliverabledir=jaxr
export javaee_home=${WORKSPACE}/glassfish5
export webserverHack=$TCK_HOME/glassfish5/glassfish

#ANT
webserverHack=$TCK_HOME/glassfish5/glassfish
ANT_VERSION=1.9.13
TOOLS_PREFIX='/opt/tools'
JAVA_PREFIX="${TOOLS_PREFIX}/java/oracle"
MVN_HOME="${TOOLS_PREFIX}/apache-maven/latest"
export JAVA_HOME="${JAVA_PREFIX}/jdk-8/latest"
PATH="${MVN_HOME}/bin:${JAVA_HOME}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ANT_HOME="${TS_HOME}/tools/ant"
PATH="${ANT_HOME}/bin:${PATH}"
export ANT_OPTS=-Djavax.xml.accessExternalSchema=all

docker run --rm -dit -p 8080:8080 jakartaee/cts-base:0.1 bash -c "/opt/jwsdp-1.3/bin/startup.sh; cat"

#get ANT
cd ${WORKSPACE}/download
wget -q http://mirror.koddos.net/apache/ant/binaries/apache-ant-${ANT_VERSION}-bin.tar.gz -O ant.tar.gz
tar xfz ant.tar.gz
wget -q ${JAXR_TCK_URL} -O ${deliverabledir}tck-2.2_latest.zip

#UNZIP GF, TCK, ANT
cd ${WORKSPACE}
unzip -q ${WORKSPACE}/download/glassfish.zip
unzip -q ${WORKSPACE}/download/${deliverabledir}tck-2.2_latest.zip
ls -alh ${WORKSPACE}
mkdir -p ${ANT_HOME} && cp -a ${WORKSPACE}/download/apache-ant-${ANT_VERSION}/. ${ANT_HOME}

#Copy TS.JTE
yes | cp -fa ${WORKSPACE}/download/ts.jte ${TS_HOME}/bin

JAVA_OPTIONS="-Djavax.net.ssl.keyStore=$TCK_HOME/glassfish5/glassfish/domains/domain1/config/keystore.jks \
-Djavax.net.ssl.keyStorePassword=changeit \
-Djavax.net.ssl.trustStore=$TCK_HOME/glassfish5/glassfish/domains/domain1/config/cacerts.jks \
-Djava.naming.factory.url=iiop://localhost:3700 -Djava.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory"


cd ${TS_HOME}/bin

sed -i "s#\${ts.home}#$TS_HOME#g" build.properties
sed -i "s#\${ant.home}#$ANT_HOME#g" build.properties
sed -i "s#webserver.home=.*#webserver.home=$TCK_HOME/glassfish5/glassfish#g" build.properties
sed -i 's#webserver.host=.*#webserver.host=localhost#g' build.properties
sed -i 's#webserver.port=.*#webserver.port=8080#g' build.properties
sed -i "s#webapp.dir=.*#webapp.dir=$TCK_HOME/glassfish5/glassfish/domains/domain1/autodeploy#g" build.properties
sed -i 's#jaxr\.home=.*#jaxr.home=\$\{webserver.home\}#g' build.properties
sed -i 's#j2ee\.home\.ri=.*#j2ee.home.ri=\$\{webserver.home\}#g' build.properties
sed -i 's#jaxr\.tool=.*#jaxr.tool=\$\{jaxr.home\}/bin/wscompile#g' build.properties
sed -i 's#jaxr.deploy.tool=.*#jaxr.deploy.tool=\$\{jaxr.home\}/bin/wsdeploy#g' build.properties
sed -i 's#\${class.dir}#/ts/jaxrtck/classes#g' build.properties

sed -i 's#local.classes=.*#local.classes=\$\{webserver.home\}/modules/webservices-osgi.jar:\$\{webserver.home\}/modules/jakarta.xml.registry-api.jar:\${webserver.home\}/modules/jakarta.servlet-api.jar:\$\{webserver.home\}/modules/jakarta.mail.jar:\$\{webserver.home\}/modules/jaxb-osgi.jar:\$\{webserver.home\}/modules/jakarta.ejb-api.jar:\$\{webserver.home\}/modules/glassfish-naming.jar:\$\{webserver.home\}/modules/bean-validator.jar#g' build.properties
#sed -i 's#local.classes=.*#local.classes=${webserverHack}/modules/webservices-osgi.jar:${webserverHack}/modules/jakarta.xml.registry-api.jar:${webserverHack}/modules/jakarta.servlet-api.jar:${webserverHack}/modules/jakarta.mail.jar:${webserverHack}/modules/jaxb-osgi.jar:${webserverHack}/modules/jakarta.ejb-api.jar:${webserverHack}/modules/glassfish-naming.jar:${webserverHack}/modules/bean-validator.jar#g' build.properties

echo $TCK_HOME
echo "webserver.home=$TCK_HOME/glassfish5/glassfish" >> build.properties
echo "webServerHost.1=localhost" >>  build.properties
echo "webHost1=localhost" >>  build.properties
echo "webServerPort.1=8080" >>  build.properties
echo "ts.home=$TS_HOME" >>  build.properties

cd /home/jenkins/workspace/TCK_JAXR_run2/glassfish5/glassfish
ls
cd /home/jenkins/workspace/TCK_JAXR_run2/glassfish5/glassfish/modules
ls

mkdir -p $TCK_HOME/jaxrtckreport/jaxrtck
mkdir -p $TCK_HOME/jaxrtckwork/jaxrtck

cp $TS_HOME/lib/tsharness.jar $TCK_HOME/glassfish5/glassfish/lib
cd $TCK_HOME/glassfish5/bin
./asadmin start-domain

echo "AS_ADMIN_MASTERPASSWORD=changeit" > $TS_HOME/bin/password.txt
echo "AS_ADMIN_PASSWORD=" >> $TS_HOME/bin/password.txt
echo "AS_ADMIN_USERPASSWORD=j2ee" >> $TS_HOME/bin/password.txt

./asadmin create-file-user --user admin --passwordfile $TS_HOME/bin/password.txt --groups Administrator:Employee j2ee

sed -i 's/AS_ADMIN_USERPASSWORD=.*/AS_ADMIN_USERPASSWORD=javajoe/g' $TS_HOME/bin/password.txt
sed -i 's/javax\.xml\.namespace=1\.1//g' $TS_HOME/bin/sig-test.map
sed -i 's/javax\.xml\.namespace//g' $TS_HOME/bin/sig-test-pkg-list.txt

./asadmin create-file-user --user admin --passwordfile $TS_HOME/bin/password.txt --groups Manager:Employee:Guest javajoe
./asadmin stop-domain || true
./asadmin start-domain

cd $TS_HOME/src/com/sun/ts/tests/jaxr/
ant  -propertyfile $TS_HOME/bin/build.properties -Dreport.dir=$TCK_HOME/jaxrtckreport/jaxrtck -Dwork.dir=$TCK_HOME/jaxrtckwork/jaxrtck deploy.all

ant  -propertyfile $TS_HOME/bin/build.properties -Dreport.dir=$TCK_HOME/jaxrtckreport/jaxrtck -Dwork.dir=$TCK_HOME/jaxrtckwork/jaxrtck runclient | tee ${WORKSPACE}/run.log
cd $TS_HOME/bin
ant -propertyfile build.properties -Dts.harness.classpath=$TS_HOME/lib/javatest.jar:$TS_HOME/lib/tsharness.jar:$TS_HOME/lib/jaxrtck.jar:$TS_HOME/classes -Dwork.dir=$TCK_HOME/jaxrtckwork/jaxrtck -Dreport.dir=$TCK_HOME/jaxrtckreport/jaxrtck report

cd ${WORKSPACE}
#create a report
export NAME=`echo ${JAXR_TCK_URL} | awk -F'/' '{print $8}'`

wget -q ${JAXR_TCK_URL} -O ${NAME} 

echo '***********************************************************************************' >> ${WORKSPACE}/SUMMARY.TXT
echo '***                        TCK bundle information                               ***' >> ${WORKSPACE}/SUMMARY.TXT
echo "*** Name:       ${NAME}                                     ***" >> ${WORKSPACE}/SUMMARY.TXT
echo "*** Download URL:	${JAXR_TCK_URL} ***"  >> ${WORKSPACE}/SUMMARY.TXT
echo '*** Date and size: '`stat -c "date: %y, size(b): %s" ${NAME}`'        ***'>> ${WORKSPACE}/SUMMARY.TXT
echo "*** SHA256SUM: "`sha256sum ${NAME} | awk '{print $1}'`' ***' >> ${WORKSPACE}/SUMMARY.TXT
echo '***                                                                             ***' >> ${WORKSPACE}/SUMMARY.TXT
echo '***********************************************************************************' >> ${WORKSPACE}/SUMMARY.TXT
echo '***                        TCK results summary                                  ***' >> ${WORKSPACE}/SUMMARY.TXT
cat run.log | sed -e '1,/Completed running/d' >> ${WORKSPACE}/SUMMARY.TXT
cat ${WORKSPACE}/SUMMARY.TXT
#cat SUMMARY.TXT | grep "FAILED" > FAILURES.TXT
#cat SUMMARY.TXT | grep "ERROR" > ERRORS.TXT
PASSED_COUNT=`head -10 ${WORKSPACE}/SUMMARY.TXT | tail -1 | sed 's/.*=\s\(.*\)/\1/'`
FAILED_COUNT=`head -11 ${WORKSPACE}/SUMMARY.TXT | tail -1 | sed 's/.*=\s\(.*\)/\1/'`
ERROR_COUNT=`head -12  ${WORKSPACE}/SUMMARY.TXT | tail -1 | sed 's/.*=\s\(.*\)/\1/'`

echo ERROR_COUNT=${ERROR_COUNT}
echo FAILED_COUNT=${FAILED_COUNT}
echo PASSED_COUNT=${PASSED_COUNT}