dd726092963766d3c1249a1882742ee37cd06c17
[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 cd "$doxydir"
143 while [ ! -r "SConstruct" -a "`pwd`" != "/" ]; do cd ..; done
144 if [ ! -r "SConstruct" ]; then
145     echo "topdir not found"
146     exit 1;
147 fi
148 TOPDIR="`pwd`";
149 reltopdir="`relpath "$doxydir/$output_dir/$html_dir" "$TOPDIR"`" #`"
150 cd "$doxydir"
151
152
153 ## Remove tagfile_name from list of tagfiles
154
155 if [ -n "$tagfile_name" ]; then
156     tagfile_name="`abspath "$output_dir/$tagfile_name"`" #`"
157     x="$tagfiles"; tagfiles=""
158     for f in $x; do
159         if [ "$f" != "$tagfile_name" ]; then
160             tagfiles="$tagfiles${tagfiles:+ }$f"
161         fi
162     done
163 fi
164
165 ## Call doxygen proper
166
167 generate_tagfile=""
168 if [ "$tagfile" = "YES" ]; then
169     generate_tagfile="$tagfile_name"
170 fi
171 export TOPDIR LIBDIR html tagfile tagfile_name tagfiles output_dir html_dir generate_tagfile
172
173 cmd ${DOXYGEN:-doxygen}
174
175
176 ## Clean up tagfile, if generated
177
178 if [ "$tagfile" = "YES" ]; then
179     mv "$tagfile_name" "${tagfile_name}.orig"
180     cmd xsltproc --novalid --nonet -o "$tagfile_name" "$tagxsl" "${tagfile_name}.orig"
181 fi
182
183
184 ## Call installdox
185
186 if [ -n "$tagfiles" ]; then
187     args=""
188     for f in $tagfiles; do
189         n="`basename "$f"`" #`"
190         d="`dirname "$f"`" #`"
191         url="`relpath "$doxydir/$output_dir/$html_dir" "$d/$html_dir"`" #`"
192         args="$args${args:+ }-l $n@$url"
193     done
194
195     (
196         cd "./$output_dir/$html_dir"
197         cmd "./installdox" $args
198     )
199 fi
200
201
202 ## Postprocess html files, if generated
203
204 if [ "$html" = "YES" ]; then
205     for h in "$doxydir/$output_dir/$html_dir"/*.html; do
206         cmd html_cleanup "$h"
207     done
208 fi