#!/bin/sh

set -ev

if which goctest >/dev/null; then
    goctest="goctest"
else
    goctest="go test"
fi

echo Checking docs
./mdlint.py docs/*.md

echo Checking formatting
fmt=$(gofmt -l .)

if [ -n "$fmt" ]; then
    echo "Formatting wrong in following files"
    echo $fmt
    exit 1
fi

echo Installing godeps
go get launchpad.net/godeps
export PATH=$PATH:$GOPATH/bin

echo Install golint
go get github.com/golang/lint/golint
export PATH=$PATH:$GOPATH/bin

echo Obtaining dependencies
godeps -u dependencies.tsv



echo Building
go build -v launchpad.net/snappy/...


# tests
echo Running tests from $(pwd)
$goctest -v -cover ./...


# go vet
echo Running vet
go vet ./...
go vet ./_integration-tests/tests/...

# golint
echo Running lint
lint=$(golint ./... && golint ./_integration-tests/tests/...)
if [ -n "$lint" ]; then
    echo "Lint complains:"
    echo $lint
    exit 1
fi

# pot file
TMPF="$(mktemp)"
./update-pot "$TMPF"
trap "rm -f $TMPF" 0
if ! diff -u --ignore-matching-lines=.*POT-Creation-Date.* po/snappy.pot $TMPF; then
    echo "You need to run ./update-pot"
    exit 1
fi

# always build
go build _integration-tests/main.go

# integration tests
if which adt-run >/dev/null 2>&1; then
    echo "Running integration tests"
    go run _integration-tests/main.go --snappy-from-branch
fi

echo "All good, what could possibly go wrong"
