summaryrefslogtreecommitdiffstats
path: root/modules/calvinscommentaries/bundle_and_install.sh
blob: 57cbf5b89c5a02a73280a0de71bea208df6115a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash


## Must modify these:
SWORDTOOLS="$HOME/devel/sword-tools"
CALCOMSOURCES="$HOME/christian/books/John Calvin/Commentaries/calcom_sources"


## Leave these to build in subdir 'build'
BUILDDIR="`pwd`/build"
OSIS2MODOUTPUT="$BUILDDIR/modules/comments/zcom/calvinscommentaries"
CONFDIR="$BUILDDIR/mods.d"
THISDIR=`pwd`

##############################################

function check_requirements {
	which csplit > /dev/null || { echo "Cannot find required tool 'csplit'.  Exiting."; exit 1;}
	which replace > /dev/null || { echo "Cannot find required tool 'replace'.  Exiting."; exit 1;}
}

function setup_dirs {
	mkdir -p $BUILDDIR
	mkdir -p $OSIS2MODOUTPUT
	mkdir -p $CONFDIR
}

function combine {
	echo "Running combine_calcom.py..."
	./combine_calcom.py "$CALCOMSOURCES"/calcom??.xml > "$BUILDDIR/calvinscommentaries.thml" || exit 1
}

function convert_to_osis {
	echo "Converting to OSIS..."
	xsltproc --novalid  "$SWORDTOOLS/thml2osis/xslt/thml2osis.xslt" "$BUILDDIR/calvinscommentaries.thml" > "$BUILDDIR/calvinscommentaries.osis" || exit 1
}

function reversify {
	cd "$BUILDDIR"

	##############################################################################
	# Splitting
	# We currently have to use genbookOsis2Commentary (since
	# osis2mod doesn't accept format unless it is marked up like a Bible),
	# genbookOsis2Commentary is a quick hack, and doesn't work well
	# with big files, since it is DOM based.  So we split the file
	# into lots of small ones, using markers inserted before.
	# Then recombine again.  This is hacky, should go away once
	# osis2mod is fixed.

	# Split
	echo "Splitting..."

	rm part*

	COUNT=$(csplit -f 'part' -b '%03d' calvinscommentaries.osis "/combine_calcom.py START/" '{*}' | nl | tail -n 1 | cut -c 1-7 )

	# $COUNT now contains the number of parts we split into

	FIRSTFILE="part000"
	FIRSTFILEALT="firstpart"
	LASTFILE="part`printf '%03d' $((COUNT-1))`"
	mv $FIRSTFILE $FIRSTFILEALT

	# $LASTFILE is special -- it will have trailing stuff
	TMP=`mktemp`
	replace '</osis>' '' '</osisText>' '' < $LASTFILE > $TMP || exit 1
	mv $TMP $LASTFILE


	# Fix individual files
	for F in part*;
	do
	    # prepend and append some stuff
	    TMP=`mktemp`
	    echo '<?xml version="1.0" encoding="UTF-8"?>' > $TMP
	    echo '<osis>' >> $TMP
	    echo '<osisText>' >> $TMP
	    cat $F >> $TMP
	    echo '</osisText>' >> $TMP
	    echo '</osis>' >> $TMP
	    mv $TMP $F

	    echo "re-versifying $F ..."
	    "$SWORDTOOLS/python/swordutils/osis/genbookOsis2Commentary.py" $F > "$F.versified" || exit 1

	    # Now strip stuff we added 
	    TMP2=`mktemp`
	    cat "$F.versified" | egrep -v 'xml version' | replace '<osis>' '' '<osisText>' '' '</osis>' '' '</osisText>' '' > $TMP2
	    mv $TMP2 "$F.versified"

	done

	# Now combine again
	COMBINED="calvinscommentaries.versified.osis"
	# Use this cleared up XML instead of the uncleaned stuff in $FIRSTFILEALT
	echo '<?xml version="1.0" encoding="UTF-8"?>' > $COMBINED
	echo '<osis xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bibletechnologies.net/2003/OSIS/namespace http://www.bibletechnologies.net/osisCore.2.1.1.xsd">' >> $COMBINED
	echo '<osisText osisRefWork="bible" canonical="true" osisIDWork="calvincommentaries" xml:lang="en">' >> $COMBINED

	for F in part*.versified;
	do
	    cat $F >> $COMBINED
	done

	echo '</osisText>' >> $COMBINED
	echo '</osis>' >> $COMBINED

}

function convert_to_mod {

	#######################################################################

	# clean out old stuff
	rm "$OSIS2MODOUTPUT/*"

	# Bug in osis2mod currently -- it removes all whitespace either side
	# of a newline, so if a sentence has a newline in it, words end
	# up merged together.  Fix that below:

	tr '\n' ' ' < "$COMBINED" > "$COMBINED.2"
	mv "$COMBINED.2" "$COMBINED"

	# osis2mod
	echo "Running osis2mod..."
	osis2mod "$OSIS2MODOUTPUT" "$BUILDDIR/$COMBINED" 0 2 3 || exit 1
}

function do_zip {

    echo "Zipping..."
    cp "$THISDIR/calvinscommentaries.conf" "$CONFDIR"


    cd "$BUILDDIR"

    rm -f CalvinsCommentaries.zip
    zip -r CalvinsCommentaries.zip mods.d/ modules/
}

function do_install {
    echo "Installing..."

    unzip -o -d $HOME/.sword CalvinsCommentaries.zip

}

check_requirements
setup_dirs
combine
convert_to_osis
reversify
convert_to_mod
do_zip
do_install