
set -e

MONGOVERSION=$(mongod --version | sed -n 's/.*v\([0-9]\+\.[0-9]\+\)\..*/\1/p')
MONGOMAJOR=$(echo $MONGOVERSION | sed 's/\([0-9]\+\)\..*/\1/')
MONGOMINOR=$(echo $MONGOVERSION | sed 's/[0-9]\+\.\([0-9]\+\)/\1/')

versionAtLeast() {
	TESTMAJOR="$1"
	TESTMINOR="$2"
	if [ "$MONGOMAJOR" -gt "$TESTMAJOR" ]; then
		return 0
	fi
	if [ "$MONGOMAJOR" -lt "$TESTMAJOR" ]; then
		return 100
	fi
	if [ "$MONGOMINOR" -ge "$TESTMINOR" ]; then
		return 0
	fi
	return 100
}

COMMONDOPTSNOIP="
	--nohttpinterface
	--noprealloc
	--nojournal
	--smallfiles
	--nssize=1
	--oplogSize=1
	--dbpath ./db
	"
COMMONDOPTS="
	$COMMONDOPTSNOIP
	--bind_ip=127.0.0.1
	"
COMMONCOPTS="
	$COMMONDOPTS
	"
COMMONSOPTS="
	--chunkSize 1
	--bind_ip=127.0.0.1
	"

if versionAtLeast 3 2; then
	# 3.2 doesn't like --nojournal on config servers.
	#COMMONCOPTS="$(echo "$COMMONCOPTS" | sed '/--nojournal/d')"
	# Using a hacked version of MongoDB 3.2 for now.

	# Go back to MMAPv1 so it's not super sluggish. :-(
	COMMONDOPTSNOIP="--storageEngine=mmapv1 $COMMONDOPTSNOIP"
	COMMONDOPTS="--storageEngine=mmapv1 $COMMONDOPTS"
	COMMONCOPTS="--storageEngine=mmapv1 $COMMONCOPTS"
fi

if [ "$TRAVIS" = true ]; then
	set -x
fi
