#!/usr/bin/env bash

set -e

PKG="$1"

DIR="$(pwd -P)"
GOPATH="$(go env GOPATH)"

# The path separator is ; on windows.
if [ "$(go env GOOS)" = "windows" ]; then
    PATHSEP=';'
else
    PATHSEP=':'
fi

while read -r -d "$PATHSEP" p; do
    if ! cd "$p/src/$PKG" 2>/dev/null; then
        continue
    fi

    if [ "$DIR" = "$(pwd -P)" ]; then
        exit 0
    fi
    cd "$DIR"
done <<< "$GOPATH$PATHSEP"

echo "go-ipfs must be built from within your \$GOPATH directory."
echo "expected within '$GOPATH' but got '$DIR'"
exit 1
