#!/bin/bash

CHECKSUM=86dd9a4bf4aee45f1a84c9f61cf1947c1d6dce9b9e8d2a907105da7852460d2f
VERSION=2.1.1

set -ex

if [ -x "$(command -v phantomjs >/dev/null 2>&1)" ]; then
	if [ "$(phantomjs --version)" = "$VERSION" ]; then
		echo "phantomjs is up to date"
		exit 0
	fi
fi

TMPDIR=$(mktemp -d)

pushd $TMPDIR

cat > checksum <<EOF
${CHECKSUM}  phantomjs-$VERSION-linux-x86_64.tar.bz2
EOF

curl -LOs https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$VERSION-linux-x86_64.tar.bz2

sha256sum --check checksum --strict

tar -xjf phantomjs-$VERSION-linux-x86_64.tar.bz2

pushd phantomjs-$VERSION-linux-x86_64

mv -f bin/phantomjs /usr/local/bin

popd
popd

rm -rf $TMPDIR
