#!/bin/bash
# 
# restyle - A wrapper around indent(1) to produce the code style
#           documented in the CONTRIBUTING.md file.
#
# Copyright © 2017 Warren Young
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS LISTED ABOVE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

if indent --version 2>&1 | grep -q 'GNU indent'
then
	# It's GNU indent here, so start with K&R, then:
	# 
	# -   nce:  don't cuddle else
	# -   cli4: indent case statement labels 4 spaces
	# -   nlp:  don't align continued statements at the opening parenthesis
	# -   pcs:  put a space before the opening parenthesis of a function call
	# -   di1:  don't line up variable types and names in separate columns
	# -   i4:   use 4-space indents
	# -   l100: allow lines up to 100 columns before forcibly breaking them
	# -   ncs:  don't put a space between a cast and its operand
	# -   ss:   add a space before semicolon with empty loop body
	# -   nbbo: don't break long lines before || and && operators
    indent -kr \
            -nce -cli4 -nlp -pcs -di1 -i4 -l100 \
            -nut -ncs -ss -nbbo "$@"
else
    # BSD `indent` does't understand the `-kr` option, so we need to use
    # the following alternative on BSD and macOS systems:
    indent \
            -nce -cli4 -nlp -pcs -di1 -i4 -l100 \
            -bap -ncdb -nfc1 -npsl -nsc "$@"
fi
