sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
sudo apt-get install -y php5.6 php5.6-cli php5.6-xdebug php5.6-xml php5.6-curl php5.6-mbstring php5.6-mcrypt php-pear php5.6-pgsql

# install CFEngine
sudo apt-get purge apache* postgresql*
wget 'https://cfengine-package-repos.s3.amazonaws.com/enterprise/Enterprise-3.10.0/hub/debian_7_x86_64/cfengine-nova-hub_3.10.0-1_amd64.deb'
sudo dpkg -i cfengine-nova-hub_3.10.0-1_amd64.deb 
sudo cf-agent -B "$(ifconfig | grep -A1 Ethernet | sed '2!d;s/.*addr:\([0-9.]*\).*/\1/')"
wget -qO- http://localhost/index.php/login/index | grep -q '<title>CFEngine Mission Portal - Log in</title>'

# install vfsStreams
sudo pear channel-discover pear.bovigo.org
sudo pear install bovigo/vfsStream-beta

# patch cf-apache to point to the new repo
# note that current work dir is repo root
# - sudo service cf-execd stop
sudo service --status-all
sudo systemctl stop cf-execd
sudo systemctl stop cf-hub
sudo sed -i "s./var/cfengine/httpd/htdocs.$(pwd)." /var/cfengine/httpd/conf/httpd.conf
cp -r /var/cfengine/httpd/htdocs/public/tmp .
cp -r /var/cfengine/httpd/htdocs/api .
cp -r /var/cfengine/httpd/htdocs/application/config/appsettings.php .
sudo /var/cfengine/httpd/bin/httpd -k restart

# Install PHPUnit
wget https://phar.phpunit.de/phpunit-5.7.phar
chmod +x phpunit-5.7.phar
sudo mv phpunit-5.7.phar /usr/local/bin/phpunit

# run tests
cd tests
sudo phpunit
