1#!/bin/sh
2#
3# @(#)genidx.sh	6.50 03/12/12
4#
5# Generate xmcd Local Discography main index and per-genre index files
6# This is run automatically by xmcd/cda as needed, or can be manually
7# invoked.
8#
9#    xmcd  - Motif(R) CD Audio Player/Ripper
10#    cda   - Command-line CD Audio Player/Ripper
11#    libdi - CD Audio Device Interface Library
12#
13#    Copyright (C) 1993-2004  Ti Kan
14#    E-mail: xmcd@amb.org
15#
16#    This program is free software; you can redistribute it and/or modify
17#    it under the terms of the GNU General Public License as published by
18#    the Free Software Foundation; either version 2 of the License, or
19#    (at your option) any later version.
20#
21#    This program is distributed in the hope that it will be useful,
22#    but WITHOUT ANY WARRANTY; without even the implied warranty of
23#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24#    GNU General Public License for more details.
25#
26#    You should have received a copy of the GNU General Public License
27#    along with this program; if not, write to the Free Software
28#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29#
30PATH=/bin:/usr/bin:/usr/local/bin; export PATH
31
32# Modify these to suit your local configuration
33XMCDLIB=/usr/lib/X11/xmcd
34DISCOGDIR=$XMCDLIB/discog
35FILE_MODE=666
36DIR_MODE=777
37
38XMCD_EMAIL=xmcd@amb.org
39XMCD_URL=http://www.amb.org/xmcd/
40TMPFILE=/tmp/_gilist.$$
41
42
43header_out()
44{
45	dhome="$1"
46	genre="$2"
47
48	echo "<!-- xmcd Local Discography index"
49	echo "     DO NOT EDIT: Generated by @(#)genidx.sh	6.50 03/12/12"
50	echo "     Copyright (C) 1993-2004 Ti Kan"
51	echo "     E-mail: $XMCD_EMAIL URL: $XMCD_URL -->"
52	echo "<HTML>"
53	echo "<HEAD>"
54	echo "<META HTTP-EQUIV=\"Content-type\" CONTENT=\"text/html; charset=utf-8\">"
55	if [ $genre = "main" ]
56	then
57	    echo "<TITLE>xmcd: Local Discographies Main Index</TITLE>"
58	else
59	    echo "<TITLE>xmcd: Local Discographies index for &quot;${gname}&quot;</TITLE>"
60	fi
61	echo "</HEAD>"
62
63	echo "<BODY BGCOLOR=\"$FFFFFF\" BACKGROUND=\"${dhome}bkgnd.gif\">"
64	echo "<DIV ALIGN=\"center\">"
65
66	echo "<A HREF=\"$XMCD_URL\">"
67	echo "<IMG SRC=\"${dhome}xmcdlogo.gif\" ALT=\"xmcd\" BORDER=\"0\"></A>"
68	if [ $genre = "main" ]
69	then
70	    echo "<H3>Local Discographies: Main Index</H3>"
71	else
72	    echo "<H3>Local Discographies: Index for &quot;${gname}&quot;</H3>"
73	fi
74	echo "</DIV>"
75	echo "<P>"
76}
77
78genre_out()
79{
80	dhome="$1"
81	echo "<P>"
82	echo "<H4>Local Discography</H4>"
83	echo "<P>"
84
85	echo "<UL>"
86
87	if [ -n "$dhome" ]
88	then
89		echo "<LI><A HREF=\"${dhome}index.html\">Main index</A>"
90	else
91		echo "<LI>Browse genres:"
92
93		echo "<UL>"
94		prev_pri=""
95		for k in $genredirs
96		do
97			pri_genre=`dirname $k`
98			sec_genre=`basename $k | sed 's/_/ /'`
99
100			if [ -z "$prev_pri" ]
101			then
102				echo "<LI>$pri_genre -&gt;"
103				echo "<UL>"
104			elif [ "$prev_pri" != "$pri_genre" ]
105			then
106				echo "</UL>"
107				echo "</UL>"
108				echo "<UL>"
109				echo "<LI>$pri_genre -&gt;"
110				echo "<UL>"
111			fi
112
113			echo "<LI><A HREF=\"${dhome}$k/index.html\">$sec_genre</A>"
114			prev_pri="$pri_genre"
115		done
116		echo "</UL>"
117
118		echo "</UL>"
119	fi
120
121	echo "<LI><A HREF=\"${dhome}discog.html\">How to use Local Discography</A></LI>"
122	echo "</UL>"
123	echo "<P>"
124	if [ -z "$dhome" ]
125	then
126		echo "<H4>Man pages</H4>"
127		echo "<UL>"
128		echo "<LI><A HREF=\"xmcd.html\">xmcd man page</A></LI>"
129		echo "<LI><A HREF=\"cda.html\">cda man page</A></LI>"
130		echo "</UL>"
131	fi
132}
133
134tail_out()
135{
136	echo "<HR>"
137	echo "<SMALL>Generated on `date` by @(#)genidx.sh	6.50 03/12/12</SMALL>"
138	echo "</BODY>"
139	echo "</HTML>"
140}
141
142
143#
144# Main starts here
145#
146
147if [ $# -gt 1 ]
148then
149	echo "Usage: $0 [genredir]" >&2
150fi
151
152echo ""
153echo "Generating xmcd Local Discography index files..."
154
155if [ ! -d $DISCOGDIR ]
156then
157	echo "	Error: cannot process $DISCOGDIR" >&2
158	exit 1
159fi
160
161cd $DISCOGDIR
162
163# Determine genre directories
164if [ $# -eq 1 ]
165then
166    genredirs=$1
167else
168    genredirs=`find */* -type d \
169	! -name '[0-9,a-f][0-9,a-f][0-9,a-f]??[0-9,a-f][0-9,a-f][0-9,a-f]' \
170	-print 2>/dev/null`
171fi
172
173if [ -z "$genredirs" ]
174then
175	echo "	No genres to process" >&2
176	exit 1
177fi
178
179for i in $genredirs
180do
181	if [ ! -d "$i" ]
182	then
183		toplev=`dirname "$i"`
184		mkdir "$toplev" 2>/dev/null && mkdir "$i" 2>/dev/null
185		if [ $? -ne 0 ]
186		then
187			echo "	Cannot create $DISCOGDIR/${i}: skipping." >&2
188			continue
189		fi
190		chmod $DIR_MODE "$toplev" "$i"
191	fi
192
193	echo "	Scanning $i"
194	dhome="../../"
195
196	rm -f $i/index.html
197	(
198		cd $i
199		gname=`echo $i | sed -e 's/_/ /g' -e 's/\// -\&gt; /g'`
200
201		header_out "$dhome" "$i"
202
203		for j in *
204		do
205			if [ $j = index.html -o ! -r $j/index.html ]
206			then
207				continue
208			fi
209			if [ `expr $j : '[0-9,a-f][0-9,a-f][0-9,a-f][0-9,a-f][0-9,a-f][0-9,a-f][0-9,a-f][0-9,a-f]'` -ne 8 ]
210			then
211				continue
212			fi
213			dtitle=`grep "^<!-- tItLe:" $j/index.html | \
214			sed -e 's/<!-- tItLe: //' -e 's/ -->//'`
215			echo "$j $dtitle"
216		done | sort -f +1 | awk '{ print $1 }' >$TMPFILE 2>&1
217
218		echo "<DIV ALIGN="center">"
219		first=1
220		for j in `cat $TMPFILE`
221		do
222			if [ ! -r $j/index.html ]
223			then
224				continue
225			fi
226			if [ $first = 1 ]
227			then
228				first=0
229				echo "<TABLE CELLSPACING=\"0\" CELLPADDING=\"2\" BORDER=\"1\""
230				echo "<TR>"
231				echo "<TD ALIGN=\"center\">Disc ID</TD>"
232				echo "<TD ALIGN=\"center\">Description<BR></TD>"
233				echo "</TR>"
234			fi
235
236			echo "<TR>"
237			echo "<TD ALIGN=\"left\">"
238			echo "<A HREF=\"$j/index.html\">$j</A>"
239			echo "</TD>"
240			echo "<TD ALIGN=\"left\">"
241
242			grep "^<!-- tItLe:" $j/index.html | \
243			sed -e 's/<!-- tItLe: //' -e 's/ -->//'
244
245			echo "<BR></TD>"
246			echo "</TR>"
247		done
248		rm -f $TMPFILE
249
250		if [ $first = 1 ]
251		then
252			echo "<B>No albums found in this genre</B>"
253		else
254			echo "</TABLE>"
255		fi
256		echo "</DIV>"
257
258		genre_out "$dhome"
259		tail_out
260	) >$i/index.html 2>/dev/null
261
262	chmod $FILE_MODE $i/index.html 2>/dev/null
263done
264
265genredirs=`find */* -type d \
266	! -name '[0-9,a-f][0-9,a-f][0-9,a-f]??[0-9,a-f][0-9,a-f][0-9,a-f]' \
267	-print 2>/dev/null`
268
269# Generate main index page
270echo "	Generating Main index"
271rm -f index.html
272(
273	header_out "" "main"
274	genre_out ""
275	tail_out
276) >index.html 2>/dev/null
277chmod $FILE_MODE index.html 2>/dev/null
278
279exit 0
280
281
282