#!/bin/bash
# The version check in this script is used to avoid commit battles
# between different developers that use different astyle versions as
# different versions might have different output (this has happened in
# the past).

# If project management wishes to take a newer astyle version into use
# just change this string to match the start of astyle version string.
ASTYLE_VERSION="Artistic Style Version 2.03"

if [[ "`astyle --version 2>&1`" != ${ASTYLE_VERSION}* ]]; then
    echo "You should use: ${ASTYLE_VERSION}";
    exit 1;
fi

style="--style=stroustrup --indent=spaces=4 --indent-namespaces --lineend=linux --min-conditional-indent=0"
options="--options=none --pad-header --unpad-paren --suffix=none --convert-tabs"

astyle $style $options cli/*.cpp
astyle $style $options cli/*.h
astyle $style $options democlient/*.cpp
astyle $style $options gui/*.cpp
astyle $style $options gui/*.h
astyle $style $options -r gui/test/*.cpp
astyle $style $options -r gui/test/*.h
astyle $style $options lib/*.cpp
astyle $style $options lib/*.h
astyle $style $options test/*.cpp
astyle $style $options test/*.h

astyle $style $options tools/*.cpp
astyle $style $options --recursive "samples/*.c"
astyle $style $options --recursive "samples/*.cpp"

# Convert tabs to spaces.. even in strings
# sed -i "s/\t/ /g" test/test*.cpp

# format config files
# TODO: use other tool than xmllint? use tabs instead of spaces?
cat cfg/avr.cfg     | xmllint --format - | tee cfg/avr.cfg     > /dev/null
#cat cfg/gtk.cfg     | xmllint --format - | tee cfg/gtk.cfg     > /dev/null
cat cfg/linux.cfg   | xmllint --format - | tee cfg/linux.cfg   > /dev/null
cat cfg/posix.cfg   | xmllint --format - | tee cfg/posix.cfg   > /dev/null
cat cfg/qt.cfg      | xmllint --format - | tee cfg/qt.cfg      > /dev/null
cat cfg/sdl.cfg     | xmllint --format - | tee cfg/sdl.cfg     > /dev/null
cat cfg/std.cfg     | xmllint --format - | tee cfg/std.cfg     > /dev/null
cat cfg/windows.cfg | xmllint --format - | tee cfg/windows.cfg > /dev/null