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