<commit_before>#!/usr/bin/env bash

export COPYFILE_DISABLE=true
export GO386=387 # Don't use SSE on 32 bit builds

distFiles=(README.md LICENSE CONTRIBUTORS) # apart from the binary itself
version=$(git describe --always --dirty)
date=$(git show -s --format=%ct)
user=$(whoami)
host=$(hostname)
host=${host%%.*}
bldenv=${ENVIRONMENT:-default}
ldflags="-w -X main.Version $version -X main.BuildStamp $date -X main.BuildUser $user -X main.BuildHost $host -X main.BuildEnv $bldenv"

check() {
	if ! command -v godep >/dev/null ; then
		echo "Error: no godep. Try \"$0 setup\"."
		exit 1
	fi
}

build() {
	check
	godep go build $* -ldflags "$ldflags" ./cmd/syncthing
}

assets() {
	check
	godep go run cmd/genassets/main.go gui > auto/gui.files.go
}

test-cov() {
	echo "mode: set" > coverage.out
	fail=0

	for dir in $(go list ./...) ; do
		godep go test -coverprofile=profile.out $dir || fail=1
		if [ -f profile.out ] ; then
			grep -v "mode: set" profile.out >> coverage.out
			rm profile.out
        fi
    done

   exit $fail
}

test() {
	check
	go vet ./...
	godep go test -cpu=1,2,4 $* ./...
}

sign() {
	if git describe --exact-match 2>/dev/null >/dev/null ; then
		# HEAD is a tag
		id=BCE524C7
		if gpg --list-keys "$id" >/dev/null 2>&1 ; then
			gpg -ab -u "$id" "$1"
		fi
	fi
}

tarDist() {
	name="$1"
	rm -rf "$name"
	mkdir -p "$name"
	cp syncthing "${distFiles[@]}" "$name"
	sign "$name/syncthing"
	tar zcvf "$name.tar.gz" "$name"
	rm -rf "$name"
}

zipDist() {
	name="$1"
	rm -rf "$name"
	mkdir -p "$name"
	for f in "${distFiles[@]}" ; do
		GOARCH="" GOOS="" go run cmd/todos/main.go < "$f" > "$name/$f.txt"
	done
	cp syncthing.exe "$name"
	sign "$name/syncthing.exe"
	zip -r "$name.zip" "$name"
	rm -rf "$name"
}

deps() {
	check
	godep save ./cmd/...
}

setup() {
	go get -v code.google.com/p/go.tools/cmd/cover
	go get -v code.google.com/p/go.tools/cmd/vet
	go get -v github.com/mattn/goveralls
	go get -v github.com/tools/godep
	GOPATH="$GOPATH:$(godep path)" go get -v -t ./...
}

xdr() {
	for f in discover/packets files/leveldb protocol/message ; do
		go run "$(godep path)/src/github.com/calmh/xdr/cmd/genxdr/main.go" -- "${f}.go" > "${f}_xdr.go"
	done
}

translate() {
	pushd gui
	go run ../cmd/translate/main.go lang-en.json < index.html > lang-en-new.json
	mv lang-en-new.json lang-en.json
	popd
}

transifex() {
	pushd gui
	go run ../cmd/transifexdl/main.go > valid-langs.js
	popd
	assets
}

case "$1" in
	"")
		shift
		export GOBIN=$(pwd)/bin
		godep go install $* -ldflags "$ldflags" ./cmd/...
		;;

	race)
		build -race
		;;

	guidev)
		echo "Syncthing is already built for GUI developments. Try:"
		echo "    STGUIASSETS=~/someDir/gui syncthing"
		;;

	test)
		test -short
		;;

	test-cov)
		test-cov
		;;

	tar)
		rm -f *.tar.gz *.zip
		test -short || exit 1
		assets
		build

		eval $(go env)
		name="syncthing-${GOOS/darwin/macosx}-$GOARCH-$version"

		tarDist "$name"
		;;

	all)
		rm -f *.tar.gz *.zip
		test -short || exit 1
		assets

		for os in darwin-amd64 linux-386 linux-amd64 freebsd-amd64 windows-amd64 windows-386 solaris-amd64 ; do
			export GOOS=${os%-*}
			export GOARCH=${os#*-}

			build

			name="syncthing-${os/darwin/macosx}-$version"
			case $GOOS in<commit_msg>Also build for freebsd-386 (fixes #458)<commit_after>#!/usr/bin/env bash

export COPYFILE_DISABLE=true
export GO386=387 # Don't use SSE on 32 bit builds

distFiles=(README.md LICENSE CONTRIBUTORS) # apart from the binary itself
version=$(git describe --always --dirty)
date=$(git show -s --format=%ct)
user=$(whoami)
host=$(hostname)
host=${host%%.*}
bldenv=${ENVIRONMENT:-default}
ldflags="-w -X main.Version $version -X main.BuildStamp $date -X main.BuildUser $user -X main.BuildHost $host -X main.BuildEnv $bldenv"

check() {
	if ! command -v godep >/dev/null ; then
		echo "Error: no godep. Try \"$0 setup\"."
		exit 1
	fi
}

build() {
	check
	godep go build $* -ldflags "$ldflags" ./cmd/syncthing
}

assets() {
	check
	godep go run cmd/genassets/main.go gui > auto/gui.files.go
}

test-cov() {
	echo "mode: set" > coverage.out
	fail=0

	for dir in $(go list ./...) ; do
		godep go test -coverprofile=profile.out $dir || fail=1
		if [ -f profile.out ] ; then
			grep -v "mode: set" profile.out >> coverage.out
			rm profile.out
        fi
    done

   exit $fail
}

test() {
	check
	go vet ./...
	godep go test -cpu=1,2,4 $* ./...
}

sign() {
	if git describe --exact-match 2>/dev/null >/dev/null ; then
		# HEAD is a tag
		id=BCE524C7
		if gpg --list-keys "$id" >/dev/null 2>&1 ; then
			gpg -ab -u "$id" "$1"
		fi
	fi
}

tarDist() {
	name="$1"
	rm -rf "$name"
	mkdir -p "$name"
	cp syncthing "${distFiles[@]}" "$name"
	sign "$name/syncthing"
	tar zcvf "$name.tar.gz" "$name"
	rm -rf "$name"
}

zipDist() {
	name="$1"
	rm -rf "$name"
	mkdir -p "$name"
	for f in "${distFiles[@]}" ; do
		GOARCH="" GOOS="" go run cmd/todos/main.go < "$f" > "$name/$f.txt"
	done
	cp syncthing.exe "$name"
	sign "$name/syncthing.exe"
	zip -r "$name.zip" "$name"
	rm -rf "$name"
}

deps() {
	check
	godep save ./cmd/...
}

setup() {
	go get -v code.google.com/p/go.tools/cmd/cover
	go get -v code.google.com/p/go.tools/cmd/vet
	go get -v github.com/mattn/goveralls
	go get -v github.com/tools/godep
	GOPATH="$GOPATH:$(godep path)" go get -v -t ./...
}

xdr() {
	for f in discover/packets files/leveldb protocol/message ; do
		go run "$(godep path)/src/github.com/calmh/xdr/cmd/genxdr/main.go" -- "${f}.go" > "${f}_xdr.go"
	done
}

translate() {
	pushd gui
	go run ../cmd/translate/main.go lang-en.json < index.html > lang-en-new.json
	mv lang-en-new.json lang-en.json
	popd
}

transifex() {
	pushd gui
	go run ../cmd/transifexdl/main.go > valid-langs.js
	popd
	assets
}

case "$1" in
	"")
		shift
		export GOBIN=$(pwd)/bin
		godep go install $* -ldflags "$ldflags" ./cmd/...
		;;

	race)
		build -race
		;;

	guidev)
		echo "Syncthing is already built for GUI developments. Try:"
		echo "    STGUIASSETS=~/someDir/gui syncthing"
		;;

	test)
		test -short
		;;

	test-cov)
		test-cov
		;;

	tar)
		rm -f *.tar.gz *.zip
		test -short || exit 1
		assets
		build

		eval $(go env)
		name="syncthing-${GOOS/darwin/macosx}-$GOARCH-$version"

		tarDist "$name"
		;;

	all)
		rm -f *.tar.gz *.zip
		test -short || exit 1
		assets

		for os in darwin-amd64 freebsd-amd64 freebsd-386 linux-amd64 linux-386 windows-amd64 windows-386 solaris-amd64 ; do
			export GOOS=${os%-*}
			export GOARCH=${os#*-}

			build

			name="syncthing-${os/darwin/macosx}-$version"
			case $GOOS in
{'fraction_non_alphanumeric': 0.17955459325703682, 'fraction_numerical': 0.015001546551190844, 'mean_word_length': 2.8608955223880597, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5224763', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2641, 'n_words': 705}