aboutsummaryrefslogtreecommitdiff
path: root/resources/scripts/helpers/build/docs/.master
blob: 4c8613534d857937b5d598b5ff316d40fbf17731 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash -e
#
# Libreboot documentation build script

# The function index() borrows from gendocs.sh, part of gnulib, the
# GNU Portability Library (https://www.gnu.org/software/gnulib/). The
# objective is to generate a html document which indexes the libreboot
# documentation in its various formats.
#
# index() does not assume a certain set of formats have been
# generated. It generates the index based on which files are located
# in $man_dir.

# INITIALIZATION #
##################

# Initialization for manual directory
[ -d docs/manual ] || mkdir docs/manual

# Initialization for building manuals
pkgname="libreboot"
name=$(basename $1) # Always defined; users do not execute directly
constants="docs/constants.texi"
man_dir="docs/manual/"
texinfo_src="docs/libreboot.texi"
resources_dir="docs/resources" # for including in html tar.gz
css_path=""  # TODO: add css?

# Default variables
prefix="resources/"
preprefix="../"
make_cmd="makeinfo"
comp_cmd="gzip -f -9 -c"
c_ext=".gz"
meta_type="man"
type="reg"
is_html="no"
make_opts=""
use_install=""

# Initialization for generating eps images
images_path="docs/resources/images"

# Initialization for making index.html
template_dir="docs/templates/"
pkg_title="GNU Libreboot manual"
pkg_email="example@libreboot.org" # sample
pkg_name="libreboot"
cur_date=`date '+%B %d, %Y'`

# In case users try to build more than one format at once.
if [ $# -gt 2 ]; then
    echo "ERROR: Only one docs format can be built at once."
    exit 1
fi

case "$2" in
    "w")
	meta_type="web";;
    "")
	:;; # This is the normal usage case
    *)
       	echo "ERROR: Only one docs format can be built at once."
	echo "If you are building html files for the libreboot website, use:"
	echo "\$ ./build docs <html-type> w"
	exit 1;;
esac

### FUNCTIONS ####
##################

set_constants()
{
    echo "@set docsdir $1" > $constants
    echo "@set useinstall $2" >> $constants
}

check_eps()
{
    printf "INFO: Checking if jpgs have been converted to eps... "
    for i in $(find $images_path); do
	tdir_name=$(dirname $i)
	tfile_name=$(basename ${i,,})
	tfile_ext=${tfile_name##*.}
	tfile_base=$(basename -s .jpg ${i,,})

	if [ $tfile_ext = "jpg" ] && [ ! -e "${tdir_name}/${tfile_base}.eps" ]; then
	    printf "converting ${i} >>>\n           ${tdir_name}/${tfile_base}.eps ...\n\n"
	    convert $i eps2:${tdir_name}/${tfile_base}.eps # using eps2 reduces file size
	fi
    done
    printf "Done.\n"
}

# FUNCTION COPIED FROM gendocs.sh
# Return size of $1 in something resembling kilobytes.
calcsize()
{
  size=`ls -ksl $1 | awk '{print $1}'`
  echo $size
}

# Concatenation function called by index
concat_index()
{
    if [ -e $man_dir$1 ]; then
	if [ -d $man_dir$1 ]; then
	    echo "<li><a href=\"$1/index.html\">$2</a>$3</li>" >> "${man_dir}index.html"
	else
	    echo "<li><a href=\"$1\">$2</a>$3</li>" >> "${man_dir}index.html"
	fi
    fi
}

# Main index function
build_index()
{
    sed \
      -e "s!%%TITLE%%!$pkg_title!g" \
      -e "s!%%PACKAGE%%!$pkg_name!g" \
      -e "s!%%DATE%%!$cur_date!g" \
    ${template_dir}gendocs_template_header >"${man_dir}index.html"

    for i in $(ls $man_dir); do
	[ -d $man_dir$i ] || size=`calcsize "${man_dir}${i}"`
	case $i in
	    ${pkg_name}.dvi.gz)
		dvi_gz_size=$size;;
	    ${pkg_name}_by-node.tar.gz)
		html_node_tgz_size=$size;;
	    ${pkg_name}_by-section.tar.gz)
		html_section_tgz_size=$size;;
	    ${pkg_name}_one-page.html)
		html_mono_size=$size;;
	    ${pkg_name}_one-page.tar.gz)
		html_mono_gz_size=$size;;
	    ${pkg_name}.info.gz)
		info_gz_size=$size;;
	    ${pkg_name}.pdf)
		pdf_size=$size;;
	    ${pkg_name}.pdf.gz)
		pdf_gz_size=$size;;
	    ${pkg_name}.ps.gz)
		ps_gz_size=$size;;
	    ${pkg_name}.txt)
		plaintext_size=$size;;
	    ${pkg_name}.txt.gz)
		plaintext_gz_size=$size;;
	    ${pkg_name}.texi.gz)
		texinfo_gz_size=$size;;
	    *)
		:;;
	esac
    done

    concat_index "${pkg_name}_one-page.html" "HTML (${html_mono_size}K bytes)" " - entirely on one web page."
    concat_index "${pkg_name}_by-node" "HTML" " - with one web page per node."
    concat_index "${pkg_name}_by-section" "HTML" " - with one web page per section."
    concat_index "${pkg_name}_one-page.tar.gz" "HTML compressed (${html_mono_gz_size}K gzipped tar file)" " - entirely on one web page."
    concat_index "${pkg_name}_by-node.tar.gz" "HTML compressed (${html_node_tgz_size}K gzipped tar file)" " - with one web page per node."
    concat_index "${pkg_name}_by-section.tar.gz" "HTML compressed (${html_section_tgz_size}K gzipped tar file)" " - with one web page per section."
    concat_index "${pkg_name}.info.gz" "Info document (${info_gz_size}K bytes gzipped)."
    concat_index "${pkg_name}.txt" "ASCII text (${plaintext_size}K bytes)."
    concat_index "${pkg_name}.txt.gz" "ASCII text compressed (${plaintext_gz_size}K bytes gzipped)."
    concat_index "${pkg_name}.dvi.gz" "TeX dvi file (${dvi_gz_size}K bytes gzipped)."
    concat_index "${pkg_name}.pdf" "PDF file (${pdf_size}K bytes)."
    concat_index "${pkg_name}.pdf.gz" "PDF file compressed (${pdf_gz_size}K bytes gzipped)."
    concat_index "${pkg_name}.ps.gz" "Postscript file compressed (${ps_gz_size}K bytes gzipped)."
    concat_index "${pkg_name}.texi.gz" "Texinfo source (${texinfo_gz_size}K bytes gzipped)."

    sed -e "s!%%EMAIL%%!$pkg_email!g" ${template_dir}gendocs_template_footer >>"$man_dir/index.html"
}

#### OPTIONS #####
##################

# options to makeinfo/texi2dvi and file names
case "$name" in
    "dvi")
	check_eps
	make_opts="-q -c -e -o"
	type="tex"
	basefile="${pkgname}.dvi";;
    "html")
	type="dir"
	is_html="yes"
	make_opts="--html --no-warn --split=node --css-include=$css_path -o"
	basefile="${pkgname}_by-node";;
    "html-by-section")
	type="dir"
	is_html="yes"
	make_opts="--html --no-warn --split=section --css-include=$css_path -o"
	basefile="${pkgname}_by-section";;
    "html-one-page")
	is_html="yes"
	make_opts="--html --no-warn --no-split --css-include=$css_path -o"
	basefile="${pkgname}_one-page.html";;
    "info")
	make_opts="--no-warn --no-split -o"
	basefile="${pkgname}.info";;
    "pdf")
	type="tex"
	make_opts="-q -c -e -p -o"
	basefile="${pkgname}.pdf";;
    "plaintext")
	make_opts="--no-warn --no-split --plaintext -o"
	basefile="${pkgname}.txt";;
    "postscript")
	check_eps
	make_opts="-q -c -e --ps -o"
	type="tex"
	basefile="${pkgname}.ps";; 
    "texinfo-gz")
	basefile="${pkgname}.texi"
	type="texi";;
esac

# split html and pdf/postscript need special paths to resources
case "$type" in
    "dir")
	preprefix="../../";;
    "tex")
	preprefix=""
	make_cmd="texi2dvi";;
esac

# compress html using tar
if [ $is_html = "yes" ]; then
    comp_cmd="tar czf"
    c_ext=".tar.gz"
fi

# set up special paths for web mode
if [ $meta_type = "web" ]; then
    if [ $is_html = "yes" ]; then
	echo "INFO: Using web mode for $name"
	prefix="docs/"
	use_install="install/"
    else
	echo "WARNING: Using web mode has no effect for non-html output."
    fi
fi

# write pathnames (prefixes) to file
set_constants "$preprefix$prefix" "$use_install"

outfile="$man_dir$basefile"

###### MAIN ######
##################
if [ $type = "texi" ]; then 
    echo "Making compressed manual: $outfile$c_ext..."
    gzip -f -9 -c $texinfo_src > $outfile$c_ext

else # The normal case
    echo "Writing manual: $outfile..."
    $make_cmd $make_opts $outfile $texinfo_src
    
    # Don't compress anything if in web mode
    if [ ! $meta_type = "web" ]; then
	echo "Making compressed manual: ${outfile%\.html}$c_ext..."
	# tar and gzip have different usages
	if [ $is_html = "yes" ]; then
	    # Include resources in html tar archives, but not .eps
	    # and remove .html extension for html-one-page
	    $comp_cmd ${outfile%\.html}$c_ext $outfile $resources_dir --exclude=*eps
	else 
	    $comp_cmd $outfile > $outfile$c_ext
	fi
	echo "Building index..."
	build_index # TODO: when building all, do this only once
    fi
fi

echo "Done."