#!/bin/sh

# To install:
# ln -sv $GOPATH/src/github.com/lmorg/murex/test/pre-commit .git/hooks/

set -e

PATH=$PATH:$GOPATH/bin
docs=docs
cdocs=builtins/docs
flag=${1:-undef}

if [ $flag != '--no-cd' ]; then
    cd $GOPATH/src/github.com/lmorg/murex
fi

trap ctrl_c INT

ctrl_c() {
    printf "\n\033[0;31m[COMMIT CANCELLED]\033[0m\n"
    exit 1
}

echo "Updating auto-generated code...."
#go generate ./...

echo 'Updating `import_build` includes....'
while read include; do
    cp -vf "./builtins/imports_src/$include" ./builtins/imports_build/
    git add -f "./builtins/imports_build/$include"
done < ./builtins/imports_src/standard_build.txt

echo "Compiling docgen...."
go install github.com/lmorg/murex/utils/docgen

echo "Compiling Murex docs...."
docgen -config gen/docgen.yaml

echo "Formatting autogen docs...."
go fmt builtins/docgen_test.go
go fmt $cdocs/*.go
go fmt config/defaults/*.go

export MUREXVERSION="$(go run github.com/lmorg/murex --version | grep -E -o '[0-9]+\.[0-9]+\.[0-9]+')"
sed "s/\$MUREXVERSION/$MUREXVERSION/g" \
        gen/website/version.svg > version.svg

echo "Updating git...."
find . -name "*_string.go" -exec git add -v {} \;
git add -v docs config/defaults builtins/docgen_test.go docs/user-guide.md version.svg $docs $cdocs

echo "Allowing git commit to proceed...."
