chiark / gitweb /
patch from Adam Borowski to fix FTBFS on x32
[bible-kjv.git] / makeindex2
1 #!/bin/bash
2 ###############################################################################
3 #
4 # File:         makeindex2
5 # RCS:          $Header: /home/matthew/cvs/bible-kjv-4.10/makeindex2,v 2.2 2003/01/13 17:40:29 matthew Exp $
6 # Description:  Create start_verse and start_chapter tables
7 # Author:       Chip Chapin
8 # Created:      Tue Jan  5 14:20:08 1993
9 # Modified:     Tue Apr 27 12:30:49 1993 (Chip Chapin) chip@hpclbis
10 # Language:     Sh
11 # Package:      Text Storage Library (Bible Retrieval System)
12 # Status:       Experimental (Do Not Distribute)
13 #
14 ###############################################################################
15 #
16 # Revisions:
17 # $Log: makeindex2,v $
18 # Revision 2.2  2003/01/13 17:40:29  matthew
19 # changed to #!/bin/bash, since that's in Debian's build-essential
20 # whereas ksh isn't :)
21 #
22 # Revision 2.1  2003/01/08 16:06:25  matthew
23 # create the vrs2num.index file earlier on so that the later routines do not barf
24 #
25 # Tue Apr 27 12:30:21 1993 (Chip Chapin) chip@hpclbis
26 #  Use temp files and only create output files if successful.
27 # Tue Jan  5 14:23:06 1993 (Chip Chapin) chip@hpclbis
28 #  Initial creation, incorporating the scripts "vrs2num", "mkvstruct", 
29 #  and "mkcstruct" from January 1989.  This is the first time I've had to
30 #  rebuild these tables since then...
31 #  Many changes to vrs2num, required by changes to rawtext format.
32 #  Additional changes were required throughout to reflect small data structure 
33 #  changes that had been made by hand.
34 ###############################################################################
35
36
37 if [ $# -lt 1 ]; then
38     print "Usage: $0 datafile"
39     exit 1
40 fi
41 RAWDATA="$1"
42
43 T1=brsA$$
44 T2=brsB$$
45 T3=brsC$$
46
47 ###############################################################################
48 # vrs2num, January 21, 1989
49 #
50 # script to read bible text  and produce a list will specify
51 # the absolute "verse number" from the start of the KJ text
52 # for each Book:Chapter (zero-based).  Also records how many verses
53 # are in each chapter.
54 # Output is the following table:
55 #
56 # Book  Chapter VrsNum  NumVerses
57 # Gen   1       0       31
58 # Gen   2       31      25
59 # ....
60
61 awk '
62     BEGIN { 
63         last_bc = "Ge1"; 
64         book = "Ge"; chap = 1;
65         start_absverse = 0; 
66     }
67     
68     {
69         # File starts with an info line. 
70         if ( split($1,p,":") < 2 ) next;
71
72         book_chap = p[1];
73         if (book_chap != last_bc) {
74             # starting new chapter
75             # Print info for last chapter.
76             # Book -- Chapter -- Starting absverse -- Number of verses
77             printf "%s\t%d\t%d\t%d\n", book, chap, start_absverse, nverses;
78
79             # Get ref without first character.  This avoids problems with
80             # refs that start with a number, e.g. "1Ki1:1".
81             # Then find out where the chapter number starts.
82             # Finally, extract the book and chapter info.
83             tempbc = substr(book_chap, 2);
84             n=match(tempbc, /[0-9]/);
85             book = substr(book_chap, 1, n);
86             chap = substr(book_chap, n+1);
87
88             start_absverse = NR-2;  # Remember where we are now.
89                                     # Lose one due to leading \n in file,
90                                     # and one because we are zero-based.
91             nverses = 0;
92             last_bc = book_chap;
93         }
94         nverses++;              # Keep count of verses in chapter
95     }
96
97     END { # print the last chapter.  Might as well do this right.
98         printf "%s\t%d\t%d\t%d\n", book, chap, start_absverse, nverses;
99     }
100 ' "$RAWDATA" > $T1
101
102 #Do this now, so we have a vrs2num.index to work from later.
103 if [ -s "$T1" ]; then mv $T1 vrs2num.index     ; fi
104
105
106
107 ###############################################################################
108 # mkvstruct, January 21, 1989
109 #
110 # Make verse structure -- creates the array that contains the 
111 # "absolute verse number" (zero-based) for every CHAPTER in the Bible.
112 # Uses the table "vrs2num" which we created earlier.
113 #
114 # This table gives the number of verses occuring in the Bible BEFORE
115 # any particular chapter.  Gen 1 is "chapter 1", Gen 2 is "chapter 2"
116 # etc.  Since there are no verses prior to Gen 1, start_verse[1] == 0.
117 # start_verse[2] == 31 because there are 31 verses prior to Gen 2:1.
118 #
119 # Chip Chapin, 890114.
120 #
121 awk '
122     BEGIN {
123         FS="\t";
124         printf "short start_verse[]={ 0,";
125     }
126     {
127         if (chaps_printed % 10 == 0) printf "\n\t";
128         chaps_printed++;
129         printf "%s, ", $3;
130         nextone = $3 + $4
131     }
132     END {
133         printf "%d\n\t};\n", nextone ;
134     }
135 ' vrs2num.index > $T2 
136
137
138 ###############################################################################
139 # mkcstruct, January 21, 1989
140 # Adapted from mkvstruct
141 #
142 # Make chapter structure -- creates the array that contains the 
143 # "absolute chapter number" (zero-based) for every BOOK in the Bible.
144 # Uses the table "vrs2num" which we created earlier, and contains one
145 # entry for each chapter in the Bible.  We wish to create a table that
146 # can be indexed by BOOK (Genesis == 0, Revelation == 65) to determine
147 # the number of chapters that precede that book in the Bible.
148 # Our only purpose here is to create the appropriate C structure.
149 #
150 #    The start_chapter table is indexed by Bible BOOK (0..65) and
151 #    gives the number of CHAPTERS in the whole Bible that precede that
152 #    particular book.  Thus given the BOOK, CHAPTER, and VERSE for a
153 #    reference, we can easily compute an absolute verse number for that
154 #    reference as follows:
155 #
156 #       abs_verse = start_verse[ start_chapter[BOOK] + CHAPTER ] + VERSE
157 #
158 # Chip Chapin, 890114.
159
160 awk '
161     BEGIN {
162         FS="\t";
163         printf "short start_chapter[]={";
164         books = 0;
165         last_book = "";
166     }
167     $1 != last_book {
168         if (books_printed % 10 == 0) printf "\n\t";
169         books_printed++;
170         printf "%d, ", NR-1;
171         last_book = $1;
172     }
173     END {
174         printf "%d\n\t};\n", NR ;
175     }
176 ' vrs2num.index > $T3
177
178 # Have to do this earlier or the second two routines barf
179 #if [ -s "$T1" ]; then mv $T1 vrs2num.index     ; fi
180 if [ -s "$T2" ]; then mv $T2 brl-startverse.h  ; fi
181 if [ -s "$T3" ]; then mv $T3 brl-startchapter.h; fi
182
183 # end
184
185 ###############################################################################
186 # Gnu Emacs variables...
187 #
188 #   Local Variables:
189 #   mode:                               sh
190 #   eval:                               (auto-fill-mode 0)
191 #   default-header-comment-character:   ?#
192 #   header-prefix:                      "#!/bin/ksh"
193 #   header-suffix:                      "#"
194 #   header-comment-character:           ?#
195 #   end: