#!/bin/bash
#
# styleindent.sh - Fixes spacing in stylesheets (requires sed 4.0.9 or later)
#
# Author: Andreas Ericsson <ae@op5.se>
#
# Rule 1: At least one space after ;:{
# Rule 2: Exactly one space after :{
# Rule 3: Exactly two spaces after ;
# Rule 4: No whitespace after or before ,
# Rule 5: No whitespace before ;:
# Rule 6: At least one space before {}
# Rule 7: Exactly one space before {}
# Rule 8: No whitespace at end of line
# Rule 9: No whitespace at beginning of line
#
# Result will look like this;
# someclass { style-info: parameters;  next-style-info: parameters; }
#
#
# This script is too silly to have a copyright notice.
#

if [ $# -le 0 ]; then
	echo "Usage: ${0##*/} <style-files>"
	echo ""
	echo "Typically, '${0##*/} *.css' should do the trick"
	exit 1
fi

for f in "$@" ; do
	sed -i $f \
		-e 's/\([;:{]\)\([^ \t]\)/\1 \2/g' \
		-e 's/\([:{]\) */\1 /g' \
		-e 's/;[ \t]*/;  /g' \
		-e 's/[ \t]*\([;:]\)/\1/g' \
		-e 's/[ \t]*,[ \t]/,/g' \
		-e 's/\([^ ]\)\([{}]\)/\1 \2/g' \
		-e 's/[ \t]*\([{}]\)/ \1/g' \
		-e 's/ *$//' \
		-e 's/^[ \t]*//'
done
