Merge pull request #1160 from hlef/master
[openjpeg.git] / scripts / astyle.sh
1 #!/bin/bash
2 ###########################################################################
3 #    astyle.sh
4 #    ---------------------
5 #    Date                 : August 2008
6 #    Copyright            : (C) 2008 by Juergen E. Fischer
7 #    Email                : jef at norbit dot de
8 ###########################################################################
9 #                                                                         #
10 #   This program is free software; you can redistribute it and/or modify  #
11 #   it under the terms of the GNU General Public License as published by  #
12 #   the Free Software Foundation; either version 2 of the License, or     #
13 #   (at your option) any later version.                                   #
14 #                                                                         #
15 ###########################################################################
16
17 for ASTYLE in ${OPJSTYLE} $(dirname $0)/opjstyle $(dirname $0)/RelWithDebInfo/opjstyle
18 do
19         if type -p $ASTYLE >/dev/null; then
20                 break
21         fi
22         ASTYLE=
23 done
24
25 if [ -z "$ASTYLE" ]; then
26         echo "opjstyle not found - please enable WITH_ASTYLE in cmake and build it" >&2
27         exit 1  
28 fi
29
30 if type -p tput >/dev/null; then
31         elcr="$ASTYLEPROGRESS$(tput el)$(tput cr)"
32 else
33         elcr="$ASTYLEPROGRESS                   \r"
34 fi
35
36 if ! type -p flip >/dev/null; then
37         if type -p dos2unix >/dev/null; then
38                 flip() {
39                         dos2unix -k $2
40                 }
41         else
42                 echo "flip not found" >&2
43                 flip() {
44                         :
45                 }
46         fi
47 fi
48
49 if ! type -p autopep8 >/dev/null; then
50         echo "autopep8 not found" >&2
51         autopep8() {
52                 :
53         }
54 fi
55
56 ASTYLEOPTS=$(dirname $0)/astyle.options
57 if type -p cygpath >/dev/null; then
58         ASTYLEOPTS="$(cygpath -w $ASTYLEOPTS)"
59 fi
60
61 set -e
62
63 astyleit() {
64         $ASTYLE --options="$ASTYLEOPTS" "$1"
65         #modified=$1.unify_includes_modified
66         #cp "$1" "$modified"
67         #scripts/unify_includes.pl "$modified"
68         #scripts/doxygen_space.pl "$modified"
69         #diff "$1" "$modified" >/dev/null || mv "$modified" "$1"
70         #rm -f "$modified"
71 }
72
73 for f in "$@"; do
74         case "$f" in
75                 thirdparty/*)
76                         echo -ne "$f skipped $elcr"
77                         continue
78                         ;;
79
80                 *.cpp|*.h|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.hpp)
81                         if [ -x "$f" ]; then
82                                 chmod a-x "$f"
83                         fi
84                         cmd=astyleit
85                         ;;
86
87                 *.py)
88                         #cmd="autopep8 --in-place --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701"
89                         echo -ne "Formatting $f $elcr"
90                         cmd="autopep8 --in-place --ignore=E261,E265,E402,E501"
91                         ;;
92
93
94                 *)
95                         echo -ne "$f skipped $elcr"
96                         continue
97                         ;;
98         esac
99
100         if ! [ -f "$f" ]; then
101                 echo "$f not found" >&2
102                 continue
103         fi
104
105         if [[ -f $f && `head -c 3 $f` == $'\xef\xbb\xbf' ]]; then
106                 mv $f $f.bom
107                 tail -c +4 $f.bom > $f
108                 echo "removed BOM from $f"
109         fi
110
111         modified=$f.flip_modified
112         cp "$f" "$modified"
113         flip -ub "$modified"
114         diff "$f" "$modified" >/dev/null || mv "$modified" "$f"
115         rm -f "$modified"
116         eval "$cmd '$f'"
117 done