#!/bin/sh

#    Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
# 
# This file is part of AFPL Ghostscript.
# 
# AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
# distributor accepts any responsibility for the consequences of using it, or
# for whether it serves any particular purpose or works at all, unless he or
# she says so in writing.  Refer to the Aladdin Free Public License (the
# "License") for full details.
# 
# Every copy of AFPL Ghostscript must include a copy of the License, normally
# in a plain ASCII text file named PUBLIC.  The License grants you the right
# to copy, modify and redistribute AFPL Ghostscript, but only under certain
# conditions described in the License.  Among other things, the License
# requires that the copyright notice and this notice be preserved on all
# copies.

# $Id: gsindent,v 1.3 2000/09/19 18:10:51 lpd Exp $

# This script formats C source code per the Ghostscript C style guide.
# The perl invocations work around a bug in GNU indent.

if [ $# -ne 0 ]; then
    for f in $*
    do
	$0 < $f > /tmp/$$
	cp -p $f $f.bak
	if ( test ! -e $f~ ) then cp -p $f $f~; fi
	mv /tmp/$$ $f
    done
    exit
fi
exec \
perl -pe 's{(#.*/\*)(\*.*)}{$1&$2}' | \
indent \
--blank-lines-after-declarations \
--no-blank-lines-after-procedures \
--leave-optional-blank-lines \
--braces-on-if-line \
--indent-level4 \
--case-indentation4 \
--no-space-after-function-call-names \
--dont-star-comments \
--declaration-indentation0 \
--procnames-start-lines \
--continue-at-parentheses \
--cuddle-else \
--no-space-after-casts \
--leave-preprocessor-space \
- | \
perl -pe 's{(#.*/\*)&(\*.*)}{$1$2}'
