#!/bin/bash
# Latest git build procedure for a particular version of uncrustify
# Typical usage:

#./git_build.sh 0.69.0 yes

# where the first argument is the cmake version string, and
# only if the second argument is "yes" is the optional
# install symlink created.

# Tailor the following values for your own platform.

# make and ctest option for parallel jobs.  Ideally, the number should
# be same as number of hardware threads (16 for a Ryzen 7 1700 system)
# to get the maximum efficiency out of those threads.

# N.B. non-Linux platforms (e.g., Cygwin and MSYS2) generally have bad
# support for parallel jobs so if not Linux I recommend specifying
# PARALLEL_OPTION=
PARALLEL_OPTION=-j16

# Finished tailoring the above values for your own platform.  The rest of this script
# should be platform independent

# Process command-line arguments:

# Process "zeroth" command-line argument.
# N.B. this logic assumes the script is run as above with either an absolute or relative
# pathname for the script.  (There are more exotic ways to run bash scripts which
# do not place the relative or absolute pathname of the script in $0 and which
# therefore defeats the purpose of the logic below in a contrived way.)

# Find absolute PATH of script without using readlink (since readlink is
# not available on all platforms).  Followed advice at
# http://fritzthomas.com/open-source/linux/551-how-to-get-absolute-path-within-shell-script-part2/
cd "$(dirname $0)"
# Absolute Path of the script
SCRIPT_PATH="$(pwd)"

# Process first command-line argument
export UNCRUSTIFY_VERSION=$1
echo "Build, install, and test uncrustify ${UNCRUSTIFY_VERSION}"

# Process second command-line argument

export if_symlink=$2
if [ "$if_symlink" = "yes" ] ; then
    echo "A non-versioned symlink to the versioned installation directory will be created"
else
    echo "A non-versioned symlink to the versioned installation directory will NOT be created"
fi

TAG_VERSION=uncrustify-${UNCRUSTIFY_VERSION}
export CFLAGS='-O3'
export CXXFLAGS='-O3'

# Update the master branch of local uncrustify repository that has already been created with
# git clone https://github.com/uncrustify/uncrustify.git ${SCRIPT_PATH}/uncrustify.git
# N.B. there is also a SF git repository for uncrustify, but that is dated compared to
# the github version.
cd ${SCRIPT_PATH}/uncrustify.git

# Update local git repository to lastest master branch.
git checkout master
git fetch
git merge --ff-only origin/master

# Check TAG_VERSION from local updated git repository
# Commented out because apparently the tags for uncrustify are unsigned.
# git tag --verify ${TAG_VERSION}

# Checkout TAG_VERSION from local updated git repository
git checkout ${TAG_VERSION}

mkdir -p ${SCRIPT_PATH}/build_dir
cd ${SCRIPT_PATH}/build_dir
# Use fresh start every time
rm -rf ${SCRIPT_PATH}/build_dir/* ${SCRIPT_PATH}/install-${UNCRUSTIFY_VERSION}

echo "configure uncrustify"
cmake -DCMAKE_INSTALL_PREFIX=${SCRIPT_PATH}/install-${UNCRUSTIFY_VERSION} ../uncrustify.git >& cmake.out

echo "Build and install uncrustify"
make install $PARALLEL_OPTION >& make_install.out

echo "Test uncrustify"
ctest $PARALLEL_OPTION >& ctest.out

echo "Check for all warnings"
grep -i warning *.out

echo "Check for all errors"
grep -i error *.out

cd ${SCRIPT_PATH}

if [ "$if_symlink" = "yes" ] ; then
    rm -f install
    ln -s install-${UNCRUSTIFY_VERSION} install
fi
