#! /bin/sh

export PATH=/bin:$PATH
export SCREENSHOT_FAILURES=true
export HEADLESS=true
export MEDIAWIKI_URL=https://zero.wikimedia.org/wiki/

if [ ! -e ~/.zero/ ]
then
	mkdir ~/.zero
	error=true
fi
chmod 700 ~/.zero

if [ ! -e ~/.zero/prod.partner.u ]
then
	touch ~/.zero/prod.partner.u
	echo "REPLACE_W_PROD_TESTING_USERNAME" > ~/.zero/prod.partner.u
	error=true
fi
chmod 600 ~/.zero/prod.partner.u

if [ ! -e ~/.zero/prod.partner.p ]
then
	touch ~/.zero/prod.partner.p
	echo "REPLACE_W_PROD_TESTING_PASSWORD" > ~/.zero/prod.partner.p
	error=true
fi
chmod 600 ~/.zero/prod.partner.p

if [ ! -e ~/.zero/email ]
then
	touch ~/.zero/email
	echo "REPLACE_WITH_EMAIL" > ~/.zero/email
	error=true
fi
chmod 600 ~/.zero/email

if [ "$error" = true ]
then
	echo "ERROR"
	echo "====="
	echo "Go setup the ~/.zero/ partner files"
	exit 1
fi

# okay, we look good to go
export MEDIAWIKI_USER=`cat ~/.zero/prod.partner.u`
export MEDIAWIKI_PASSWORD=`cat ~/.zero/prod.partner.p`
export EMAIL=`cat ~/.zero/email`

if [ ! -e reports/ ]
then
	mkdir reports
fi
chmod 755 reports

d=`date "+%Y%m%d-%H%M-%S"`
report="reports/cucumber_partner_prod-${d}.html"
screenshots="reports/cucumber_partner_prod_screenshots-${d}"
mkdir $screenshots
chmod 755 $screenshots
export SCREENSHOT_FAILURES_PATH=$screenshots
bundle exec cucumber --tags @partner -f html -o $report
if [ $? -eq 1 ]
then
	echo "Partner portal PARTNER PROD failure. See attached." | mailx -s "Portal Cucumber: FAIL Partner Prod" -a $report $EMAIL
fi
