#!/bin/bash

set -e

go build

mkdir -p ./testdata/

# prepare variables
	export C4GO_DIR=$GOPATH/src/github.com/Konstantin8105/c4go
	export C4GO=$C4GO_DIR/c4go
	export GIT_SOURCE="https://github.com/vurtun/nuklear.git"
	export NAME="nuklear"
	export TEMP_FOLDER="./testdata/$NAME"
	export GO_FILE="$TEMP_FOLDER/$NAME.go"
	export GO_APP="$TEMP_FOLDER/$NAME.app"
	export COMMIT=6b9f937475db9280d966f44f469bc80191b5092a

# prepare C code
    if [ ! -d $TEMP_FOLDER ]; then
		mkdir -p $TEMP_FOLDER
		git clone $GIT_SOURCE $TEMP_FOLDER
		cd $TEMP_FOLDER/
		git checkout $COMMIT
		cd ../../
	fi

# remove go files from last transpilation
	echo "***** remove go files"
	rm -f $TEMP_FOLDER/*.go
	rm -f $TEMP_FOLDER/*.app

# transpilation of all projects
	echo "Transpile to $GO_FILE"
	$C4GO transpile                         \
		-s                                  \
		-clang-flag="-I$TEMP_FOLDER/"       \
		-o=$TEMP_FOLDER/x11.go              \
		$TEMP_FOLDER/demo/x11/*.c

	$C4GO transpile                         \
		-s                                  \
		-clang-flag="-I$TEMP_FOLDER/"       \
		-o=$TEMP_FOLDER/x11_opengl2.go      \
		$TEMP_FOLDER/demo/x11_opengl2/*.c


# Arguments menu
echo "    -s for show detail of Go build errors"

# transpilation each file
	for f in $TEMP_FOLDER/*.go; do
			# iteration by C projects
				echo "***** transpilation folder : $f"
			# show warnings comments in Go source
				export FILE="$f"
				echo "Calculate warnings : $FILE"
				WARNINGS=`cat $FILE | grep "^// Warning" | sort | uniq | wc -l`
				echo "		After transpiling : $WARNINGS warnings."
			# show amount error from `go build`:
				echo "Build to $GO_APP file"
				WARNINGS_GO=`go build -o $GO_APP -gcflags="-e" $f 2>&1 | wc -l`
				echo "		Go build : $WARNINGS_GO warnings"
			# amount unsafe
				UNSAFE=`cat $FILE | grep "unsafe\." | wc -l`
				echo "		Unsafe   : $UNSAFE"

			if [ "$1" == "-s" ]; then
				# show go build warnings	
					# c4go warnings
						cat $f | grep "^// Warning" | sort | uniq
					# show amount error from `go build`:
						go build -o $GO_APP -gcflags="-e" $f 2>&1 && echo "OK" || echo "NOK"
			fi
	done

