* improved makefile mode to make it depend on style.css if there is a file
[adtree.git] / adtree
blob7ae26405e92c361d201717650cc910239be2805d
1 #!/bin/sh
2 # Copyright (C) 2007 Alejandro Mery <amery@geeks.cl>
3 #
4 # More information can be found in the files COPYING and README.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 3 of the License. A copy of the
9 # GNU General Public License can be found in the file COPYING.
11 version='0.0-trunk'
12 progname=${0##*/}
14 verbose=
15 mode=list
16 output=plain
18 root=
19 base=/
20 home=home
22 adtree_usage() {
23 cat <<EOT
24 usage: $progname [-V|--version]
25 $progname -m <mode> [-o <format>] [OPTIONS] [locations...]
27 options:
28 -v|--verbose verbose
29 -q|--quiet not verbose
30 -R|--root directory to use as root
32 -m|--mode <mode>
33 query mode to use (default:list)
34 -o|--output <format>
35 output format to use (default:plain)
37 modes:
38 * navigation (-n)
39 * formats: plain, html
40 * list (-l)
41 * formats: plain, html
42 * deps (-d)
43 * formats: plain
44 * makefile
45 * formats: plain
47 html format options:
48 -B|--base relative path to this location (/)
49 -h|--home name to give to the base (home)
50 EOT
53 adtree_echo() { [ -z "$verbose" ] || echo -e $* >&2 ; }
54 adtree_warn() { echo -e "WARN: $@" >&2 ; }
55 adtree_error() { echo -e "ERROR: $@" >&2 ; }
57 # analyse the given options
58 shortopts='Vvqnldm:o:R:b:h:'
59 longopts='version,verbose,quiet,mode:,output:,root:,base:,home:'
61 options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
62 if [ $? -ne 0 ]; then
63 adtree_usage
64 exit -1
67 # load new arguments list
68 eval set -- "$options"
70 while [ $# -gt 0 ]; do
71 case "$1" in
72 -V|--version)
73 cat <<-EOT
74 adtree $version
75 Copyright (C) 2007 Alejandro Mery <amery@geeks.cl>
76 EOT
77 exit
79 -v|--verbose)
80 verbose=1 ;;
81 -q|--quiet)
82 verbose= ;;
84 -m|--mode)
85 shift;
86 case "$1" in
87 navigation|list|deps|makefile)
88 mode="$1" ;;
89 *) adtree_error "unknown mode '$1'"
90 adtree_usage
91 exit -1
93 esac ;;
94 -n)
95 mode="navigation" ;;
96 -l)
97 mode="list" ;;
98 -d)
99 mode="deps" ;;
101 -o|--output)
102 shift;
103 case "$mode" in
104 deps|makefile)
105 case "$1" in
106 plain)
107 output="$1" ;;
108 *) adtree_error "unknown output format '$1'"
109 adtree_usage
110 exit -1 ;;
111 esac ;;
112 list|navigation)
113 case "$1" in
114 html|plain)
115 output="$1" ;;
116 *) adtree_error "unknown output format '$1'"
117 adtree_usage
118 exit -1 ;;
119 esac ;;
120 esac ;;
122 -R|--root)
123 shift; root="$1" ;;
124 -B|--base)
125 shift; base="$1" ;;
126 -h|--home)
127 shift; home="$1" ;;
129 shift; break ;;
130 esac
131 shift
132 done
134 if [ -z "$mode" ]; then
135 adtree_error "no mode choosen"
136 adtree_usage
137 exit -1
140 # if not folder was given, use .
141 [ $# -gt 0 ] || set -- .
143 if [ -n "$root" ]; then
144 if [ -d "$root" ]; then
145 adtree_echo "root: $root"
146 else
147 adtree_error "$root: invalid root"
148 adtree_usage
149 exit -2
151 else
152 root="."
154 root=$( cd "$root"; pwd -P )
156 tmpfile=$( mktemp )
157 trap 'echo "Got SIGINT (Crtl-C). Aborting." >&2 ; rm -rf '$tmpfile'; exit 1' INT
159 adtree_list() {
160 local source="$1" target= name=
161 local filename= folder= path=
163 name="${source#$root/}"
164 if ! grep -q "^$name\$" $tmpfile; then
165 echo "$name" >> $tmpfile
167 filename="${source##*/}"; name="${filename%.txt}"
168 path="${source%/*}"; folder="${path##*/}"
170 case "$output" in
171 html)
172 if [ "$name" == "$folder" ]; then
173 target="$path/index.html"
174 else
175 target="$path/$name.html"
176 fi ;;
177 plain)
178 target="$source"
180 esac
182 echo "${target#$root/}"
186 adtree_deps() {
187 local source="$1" path= name= dep=
189 name="${source#$root/}"
190 if ! grep -q "^$name\$" $tmpfile; then
191 echo "$name" >> $tmpfile
193 if [ -r "$source" ]; then
194 path="${source%/*}"
195 adtree_echo "$name: list of dependencies"
197 sed -n -e "s,^\(include\|include1\)::\(.*\)\[\(.*\)\].*$,\2,p" "$source" | while read dep; do
198 dep="$path/$dep"
199 echo "${dep#$root/}"
201 adtree_deps "$dep"
202 done
203 else
204 adtree_warn "$name: include file not found"
209 adtree_makefile() {
210 local source="$1" html=
211 local filename= name= path=
212 local style=
214 filename="${source##*/}"; name="${filename%.txt}"
215 if [ "$filename" != "$source" ]; then
216 path="${source%/*}";
219 if [ "$name" == "${path##*/}" ]; then
220 html="${path:+$path/}index.html"
221 else
222 html="${path:+$path/}$name.html"
225 if [ -s "$path/style.css" ]; then
226 style="${path:+$path/}style.css"
229 cat<<EOT
230 ${html#$root/}: ${source#$root/} ${style#$root/} $( $0 -dR "$root" -- "$source" | tr '\n' ' ' )
235 case "$mode" in
236 list|deps|navigation)
237 for x; do if [ -e "$x" ]; then
238 if [ -d "$x/" ]; then
239 x=$( cd "$x"; pwd -P )
240 else
241 f="${x##*/}";
242 [ "$x" != "$f" ] || x="$PWD/$f"
244 x="$( cd ${x%/*}; pwd -P )/$f"
247 case "$mode" in
248 list) $0 -dR "$root" -- "$x" >> "$tmpfile"
249 find "$x" -type f -name '*.txt' | while read f; do
250 adtree_list "$f"
251 done ;;
252 deps)
253 find "$x" -type f -name '*.txt' | while read f; do
254 adtree_deps "$f"
255 done ;;
256 esac
257 fi; done
260 makefile)
261 "$0" -lR "$root" -- "$@" | while read x; do
262 adtree_makefile "$x"
263 done
265 esac
266 rm -f "$tmpfile"