19d65601f82cf487c6d89bba54f07bd75d4bdbf2
[senf.git] / site_scons / lib / doxygen.sh
1 #!/bin/bash -e
2
3 do_html_cleanup()
4 {
5     sed -e 's/id="current"/class="current"/' \
6         | tidy -ascii -q --wrap 0 --show-warnings no --fix-uri no \
7         | sed -e 's/name="\([^"]*\)"\([^>]*\) id="\1"/name="\1"\2/g' \
8               -e 's/id="\([^"]*\)"\([^>]*\) name="\1"/name="\1"\2/g' \
9         | xsltproc --novalid --nonet --html --stringparam topdir "$reltopdir" \
10               "$base/html-munge.xsl" -
11 }
12
13 ###########################################################################
14
15 # Build absolute, normalized pathname
16 abspath()
17 {
18     case "$1" in
19         /*) echo "$1" ;;
20         *) echo "`pwd`/$1" ;;
21     esac | sed \
22         -e 's/\/\/*/\//g' \
23         -e:a \
24         -e 's/\/\.\(\/\|$\)/\1/g' \
25         -eta \
26         -e:b \
27         -e 's/\/[^\/]*\/..\(\/\|$\)/\1/' \
28         -etb \
29         -e 's/^\(\/..\)*\(\/\|$\)/\2/' \
30         -e 's/^$/\//'
31 }
32
33 # Create relative path from absolute directory $1 to absolute path $2
34 relpath()
35 {
36     local src="`abspath "$1"`" # `"
37     local dst="`abspath "$2"`" # `"
38     src="${src#/}"
39     dst="${dst#/}"
40     while true; do
41         if [ -z "$src" -a -z "$dst" ]; then
42             echo "Internal error in relpath()" 1>&2
43             exit 1
44         fi
45         srcd="${src%%/*}"
46         dstd="${dst%%/*}"
47         if [ "$srcd" = "$dstd" ]; then
48             src="${src#$srcd}"; src="${src#/}"
49             dst="${dst#$dstd}"; dst="${dst#/}"
50         else
51             break
52         fi
53     done
54     rel="`echo "$src" | sed -e "s/[^\/]*/../g"`/$dst" # `"
55     echo "${rel%/}"
56 }
57
58 # Log executed commands
59 cmd()
60 {
61     echo "+" "$@"
62     "$@"
63 }
64
65 html_cleanup()
66 {
67     mv "$1" "${1}.orig"
68     do_html_cleanup <"${1}.orig" >"$1"
69 }
70
71 ###########################################################################
72
73 name="`basename "$0"`"; #`"
74 base="`dirname "$0"`"; base="`cd "$base"; pwd`" #`"
75 LIBDIR="$base"
76
77 TEMP=`getopt -n $name -o '' -l html,tagfile,tagfile-name:,tagfiles:,output-dir:,html-dir:  -- "$@"`
78 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
79 eval set -- "$TEMP"
80
81 tagxsl="$base/tag-munge.xsl"
82
83 html="NO"; tagfile="NO"; tagfile_name=""; tagfiles=""; output_dir="doc"; html_dir="html"
84
85 while true; do
86     case "$1" in
87         --html)
88             html="YES" ; shift ;;
89         --tagfile)
90             tagfile="YES" ; shift ;;
91         --tagfile-name)
92             tagfile_name="$2"; shift 2 ;;
93         --tagfiles)
94             for f in $2; do
95                 f="`abspath "$f"`" #`"
96                 tagfiles="$tagfiles${tagfiles:+ }$f"
97             done
98             shift 2
99             ;;
100         --output-dir)
101             output_dir="$2"; shift 2 ;;
102         --html-dir)
103             html_dir="$2"; shift 2 ;;
104         --)
105             shift; break ;;
106         *)
107             echo "Internal error: $*"; exit 1 ;;
108     esac
109 done
110
111 if [ -z "$1" ]; then
112     cat <<EOF
113 Usage:
114     $name
115         [--html]
116         [--tagfile]
117         [--tagfile-name=<name>]
118         [--tagfiles=<files>]
119         [--output-dir=<dir>]
120         [--html-dir=<dir>]
121             <doxyfile-or-dir>
122 EOF
123     exit 1
124 fi
125
126 if [ "$tagfile" = "YES" -a -z "$tagfile_name" ]; then
127     echo "--tagfile-name is required with --tagfile"
128     exit 1
129 fi
130
131 doxydir="$1"
132
133 if [ -f "$doxydir" ]; then
134     doxydir="`dirname "$doxydir"`" #`"
135 fi
136
137 doxydir="`abspath "$doxydir"`" #`"
138
139 ###########################################################################
140
141 ## Find $TOPDIR
142
143 if [ -z "$TOPDIR" ]; then
144     cd "$doxydir"
145     while [ ! -r "SConstruct" -a "`pwd`" != "/" ]; do cd ..; done
146     if [ ! -r "SConstruct" ]; then
147         echo "topdir not found"
148         exit 1;
149     fi
150     TOPDIR="`pwd`";
151 fi
152
153 reltopdir="`relpath "$doxydir/$output_dir/$html_dir" "$TOPDIR"`" #`"
154 cd "$doxydir"
155
156
157 ## Remove tagfile_name from list of tagfiles
158
159 if [ -n "$tagfile_name" ]; then
160     tagfile_name="`abspath "$output_dir/$tagfile_name"`" #`"
161     x="$tagfiles"; tagfiles=""
162     for f in $x; do
163         if [ "$f" != "$tagfile_name" ]; then
164             tagfiles="$tagfiles${tagfiles:+ }$f"
165         fi
166     done
167 fi
168
169 ## Remove empty tagfiles from list of tagfiles
170
171 x="$tagfiles"; tagfiles=""
172 for f in $x; do
173     if [ -s "$f" ]; then
174         tagfiles="$tagfiles${tagfiles:+ }$f"
175     fi
176 done
177
178 ## Call doxygen proper
179
180 generate_tagfile=""
181 if [ "$tagfile" = "YES" ]; then
182     generate_tagfile="$tagfile_name"
183 fi
184 export TOPDIR LIBDIR html tagfile tagfile_name tagfiles output_dir html_dir generate_tagfile
185
186 echo "+ cd $doxydir"
187 cmd ${DOXYGEN:-doxygen}
188
189
190 ## Clean up tagfile, if generated
191
192 if [ "$tagfile" = "YES" ]; then
193     mv "$tagfile_name" "${tagfile_name}.orig"
194     cmd xsltproc --novalid --nonet -o "$tagfile_name" "$tagxsl" "${tagfile_name}.orig"
195 fi
196
197
198 ## Call installdox
199
200 if [ -n "$tagfiles" ]; then
201     args=""
202     for f in $tagfiles; do
203         n="`basename "$f"`" #`"
204         d="`dirname "$f"`" #`"
205         url="`relpath "$doxydir/$output_dir/$html_dir" "$d/$html_dir"`" #`"
206         args="$args${args:+ }-l $n@$url"
207     done
208
209     (
210         cd "./$output_dir/$html_dir"
211         cmd "./installdox" $args
212     )
213 fi
214
215
216 ## Postprocess html files, if generated
217
218 if [ "$html" = "YES" ]; then
219     for h in "$doxydir/$output_dir/$html_dir"/*.html; do
220         cmd html_cleanup "$h"
221     done
222 fi