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