builder.sh 9.11 KB
Newer Older
notklaatu committed
1 2
#!/bin/sh
# bash script to build Dark oCCult for printing
notklaatu committed
3 4 5 6 7 8 9

# GNU All-Permissive License
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

10
VILE="tee"
11
BLD=${BLD:-build}
notklaatu committed
12
DEST=${DEST:-print}
13 14 15
RES=${RES:-600}
XARGLIKE=parallel
PAPER=`grep -v '^$\|^\s*\#' /etc/papersize`
16

notklaatu committed
17
help() {
18 19
    echo "builder.sh [build|deck|docs|merge|clean] /path/to/dir" | $VILE
    echo "build Dark oCCult for printing" | $VILE
notklaatu committed
20
    echo " "
21
    echo "./builder.sh build . -> build printable deck and docs from current directory."
22 23 24 25 26 27 28 29
    echo "Paper size defaults to A4. Change it with:"
    echo "./builder.sh --paper letter build ."
    echo "./builder.sh --paper a4 build ."
    echo " "
    echo "Output dir defaults to build. Set it with:"
    echo "BLD=build-letter ./builder.sh --paper letter build ." 
    echo " "
    echo "Individual stages:"
30 31 32 33
    echo "builder.sh deck /path/to/dark/oCCult/dir -> build card set for printing" | $VILE
    echo "builder.sh deck merge /path/to/dark/oCCult/dir -> build to one PDF file" | $VILE
    echo "builder.sh docs /path/to/dark/oCCult/dir  -> build docs for printing" | $VILE
    echo "builder.sh clean /path/to/dark/oCCult/dir -> start over" | $VILE
notklaatu committed
34
    echo " "
notklaatu committed
35
    exit
notklaatu committed
36 37
}

38
merge() {
39 40 41 42 43 44 45
    cd "${CWD}"/"${BLD}"/"${DEST}"

    pdf-stapler cat `ls 0*pdf | "${XARGLIKE}" -I% --max-args=1 echo % back.pdf` print-deck.pdf

    echo "Combined into one document for easy printing."
    mv 0??.pdf _trash
    mv back.pdf _trash
46 47 48
}

sane() {
49

notklaatu committed
50
# do we have the fonts?
51 52 53
fc-list | grep -i "essays1743" || { exit "Essays1743 font missing. Install from the fonts directory and try again." ; exit; }
fc-list | grep -i "orbitron" || { echo "Orbitron font missing. Install from the fonts directory and try again."; exit; }
fc-list | grep -i "ubuntu" || { echo "Ubuntu font missing. Install from the fonts directory and try again."; exit; }
54
echo "all fonts accounted for"
notklaatu committed
55 56 57

#sanity checks
PDFNUP=${PDFNUP:-`which pdfnup`} || PDFNUP=""
notklaatu committed
58 59
CONVERT=${CONVERT:-`which convert`} || CONVERT=${CONVERT:-`which gm` convert}
MONTAGE=${MONTAGE:-`which montage`} || MONTAGE=${MONTAGE:-`which gm` montage}
60
MOGRIFY=${MOGRIFY:-`which mogrify`} || MOGRIFY=${MOGRIFY:-`which gm` mogrify}
notklaatu committed
61 62 63 64
if [ X"$CONVERT" = "X" -o X"$MONTAGE" = "X" ]; then
    echo "ImageMagick or GraphicsMagick is required, but cannot be found."
    exit 1
fi
65
}
notklaatu committed
66

67 68
deck() {
echo "Working in $CWD" 
69

notklaatu committed
70
# convert svg to png
71 72 73 74
mkdir -p "$CWD"/"${BLD}"/"${DEST}"

echo "Exporting with Inkscape..." #debug
find "${CWD}"/deck -type f -iname "*.svg" | "${XARGLIKE}" -I% inkscape -z -e %.png -w 1464 -h 2078 --export-dpi $RES %
75

76 77 78 79 80
#convert xcf player cards
for i in "${CWD}"/deck/player/*xcf; do
    xcf2png $i > "${CWD}"/"${BLD}"/"${DEST}"/`basename $i .xcf`.png
    #"${MOGRIFY}" -rotate 90 -density $RES -geometry 2078x1464 "${CWD}"/"${BLD}"/"${DEST}"/`basename $i .xcf`.png
done
81

82 83 84
#trim excess cards to maximize printables
mv "${CWD}"/"${BLD}"/"${DEST}"/opend6*paranormal*g \
   "${CWD}"/"${BLD}"/"${DEST}"/player_paranormal.svg.png
85

86 87 88 89
#gather all pngs
find "${CWD}"/deck -type f -name "*_*.png" | "${XARGLIKE}" -I% mv % "${CWD}"/"${BLD}"/"${DEST}"
find "${CWD}"/deck/system -type f -name "title*svg.png" | "${XARGLIKE}" -I% mv % "${CWD}"/"${BLD}"/"${DEST}"
find "${CWD}"/"${BLD}"/"${DEST}" -type f -name "*png" | "${XARGLIKE}" "${MOGRIFY}" -rotate 90
notklaatu committed
90 91

# this is where the pngs live
92
mkdir -p "${CWD}"/"${BLD}"/"${DEST}"/_trash 2> /dev/null
93

94
cd "${CWD}"/"${BLD}"/"${DEST}"
95

96
#layout
97
echo "Using $PDFNUP to build card layout."
98
ls *svg.png | "${XARGLIKE}" --max-args=9 "${PDFNUP}" --papersize \'\{$W,$H\}\' --no-tidy --nup 3x3 --suffix '3x3'
99

100
echo "Creating optional player cards."
101
ls opend6*ng | "${XARGLIKE}" --max-args=9 "${PDFNUP}" --papersize \'\{$W,$H\}\' --nup 3x3 --suffix '3x3'
102 103

#rename
104 105
n=1
for i in *pdf; do
106
    echo $i `printf "%03d" $n`.pdf
107 108 109 110
    mv $i `printf "%03d" $n`.pdf
    let n++
done

111 112 113 114
#back sheet
cat "${CWD}"/deck/system/back.png > "${CWD}"/"${BLD}"/"${DEST}"/back.png
"${MOGRIFY}" -verbose -rotate 90 "${CWD}"/"${BLD}"/"${DEST}"/back.png

115
# gen back
116
"${PDFNUP}" --nup 3x3 --papersize \{$W,$H\} --suffix '3x3' \
117 118 119 120 121 122
	    back.png back.png back.png \
	    back.png back.png back.png \
	    back.png back.png back.png

mv back*pdf back.pdf

123
echo "Print-spreads generated as single signature documents in the print folder." | $VILE
notklaatu committed
124 125
}

126 127
tidy() {
#clean up
128 129 130 131 132 133
mv "${CWD}"/"${BLD}"/"${DEST}"/back.png "${CWD}"/"${BLD}"/"${DEST}"/_trash/ 2>/dev/null
mv "${CWD}"/"${BLD}"/"${DEST}"/*png "${CWD}"/"${BLD}"/"${DEST}"/_trash/ 2>/dev/null
mv *_*3x3*pdf "${CWD}"/"${BLD}"/"${DEST}"/_trash/ 2>/dev/null
echo "${CWD}"
/bin/rm "${CWD}"/doc/source/images/*.jpeg 2>/dev/null
/bin/rm -rf "${CWD}"/"${BLD}"/"${DEST}"/_trash/
notklaatu committed
134 135 136
}

doc() {
137 138 139 140 141 142 143 144 145 146 147
    pushd "${CWD}"/doc
    echo "Using doc dir" `pwd`
    mkdir -p "$CWD"/"${BLD}"/"${DEST}" 2>/dev/null || true
    mkdir build 2>/dev/null || true
    for i in source/images/*svg ; do
	convert $i -density 300 source/images/`basename $i .svg`.jpeg
    done

    # pdf
    xsltproc --output source/tmp.fo \
	     --stringparam paper.type "${PAPER}" \
148 149
	     --stringparam page.width $W \
	     --stringparam page.height $H \
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
	     --stringparam redist.text "no" \
	     --stringparam column.count.titlepage 1 \
	     --stringparam column.count.lot 1 \
	     --stringparam column.count.front 1 \
	     --stringparam column.count.body 2 \
	     --stringparam column.count.back 1 \
	     --stringparam column.count.index 2 \
	     --stringparam body.font.family "TeX Gyre Bonum" \
	     --stringparam title.font.family "Essays1743" \
	     --stringparam bridgehead.font.family "TeX Gyre Bonum" \
	     --stringparam symbol.font.family "UniCons" \
	     --stringparam footer.column.widths "1 0 0" \
	     --stringparam body.font.master 10 \
	     --stringparam body.font.size 10 \
	     --stringparam page.margin.inner .5in \
	     --stringparam page.margin.outer .5in \
	     --stringparam page.margin.top .45in \
	     --stringparam page.margin.bottom .45in \
	     --stringparam title.margin.left 0 \
	     --stringparam title.start.indent 0 \
	     --stringparam body.start.indent 0 \
	     --stringparam chapter.autolabel 0 \
	     style/mystyle.xsl source/docbook.xml
    fop -c style/rego.xml source/tmp.fo build/dark-oCCult.pdf
    /bin/rm source/tmp.fo

    mkdir build/html
    xmlto --skip-validation -o build/html html-nochunks source/docbook.xml
178 179 180 181 182 183 184 185
    mv build/html ../"${BLD}"/html-guide
    mv build/ ../"${BLD}"/print/docs

    mkdir ../"${BLD}"/html-guide/images
    cp source/images/*svg ../"${BLD}"/html-guide/images
    sed -i 's_../images_./images_g' ../"${BLD}"/html-guide/docbook.html
    sed -i 's_jpeg_svg_g' ../"${BLD}"/html-guide/docbook.html
    mv ../"${BLD}"/html-guide/docbook.html ../"${BLD}"/html-guide/index.html
notklaatu committed
186 187 188
}

clean() {
189
    /bin/rm -r "${CWD}"/"${BLD}"
190 191 192
    /bin/rm -f "${CWD}/doc/build"
    /bin/rm "${CWD}"/doc/source/images/*.jpeg 2>/dev/null
    echo "Everything is clean, and tidy, and back to normal." | $VILE
notklaatu committed
193 194 195 196 197 198 199
}

## parse
while [ True ]; do
if [ "$1" = "clean" -o "$1" = "--clean" ]; then
    CLEAN=1
    shift 1
200 201 202
elif [ "$1" = "tidy" -o "$1" = "--tidy" ]; then
    TIDY=1
    shift 1
notklaatu committed
203 204 205
elif  [ "$1" = "doc" -o "$1" = "--doc" -o "$1" = "docs" -o "$1" = "--docs" ]; then 
    DOC=1
    shift 1
206
elif  [ "$1" = "deck" -o "$1" = "--deck" -o "$1" = "d" -o "$1" = "-d" ]; then 
notklaatu committed
207 208
    BUILD=1
    shift 1
209 210 211 212 213 214
elif  [ "$1" = "merge" -o "$1" = "--merge" ]; then 
    MERGE=1
    shift 1
elif  [ "$1" = "--no-tidy" ]; then 
    TIDY=0
    shift 1
215 216 217 218
elif  [ "$1" = "--paper" ]; then 
    PAPER=`echo $2 | tr [:upper:] [:lower:]`
    echo $PAPER
    shift 2
notklaatu committed
219 220 221
elif  [ "$1" = "evil" -o "$1" = "--vile" -o "$1" = "666" -o "$1" = "redrum" ]; then 
    EVIL=1
    shift 1
222 223 224 225 226 227 228
elif  [ "$1" = "build" -o "$1" = "--build" -o "$1" = "b" -o "$1" = "-b" ]; then 
    TIDY=1
    CLEAN=1
    BUILD=1
    MERGE=1
    DOC=1
    shift 1
notklaatu committed
229 230 231 232 233 234 235 236
elif  [ "$1" = "help" -o "$1" = "--help" -o "$1" = "-h" -o "$1" = "--options" -o "$1" = "options" -o "$1" = "" ]; then 
    help
    shift 1
else
    break
fi
done

notklaatu committed
237
if [ X"${1}" = "X" ]; then
238 239
    CWD=`pwd`
    echo "CWD is $CWD"
240
else
241 242
    CWD=`readlink -f "${1}"`
    echo "CWD is $CWD"
notklaatu committed
243 244
fi

notklaatu committed
245 246 247
## main
if [ X"$CLEAN" = X"1" ]; then
    # clean first
248
    clean "${CWD}"
notklaatu committed
249 250
fi

251 252 253 254 255 256 257 258 259 260 261 262
if [ X"$PAPER" = "X" ]; then
    PAPER=${PAPER:-a4}
fi

if [ X"$PAPER" = X"letter" ]; then
    W="8.5in"
    H="11in"
elif [ X"$PAPER" = X"a4" ]; then
    W="210mm"
    H="297mm"
fi

notklaatu committed
263 264 265
if [ X"$EVIL" = X"1" ]; then
    # find out if we're evil
    echo "Something beckons you from the comfort of your home, out into the night..."
notklaatu committed
266
    VILE='tr "[A-Za-z]" "[N-ZA-Mn-za-m]"'
notklaatu committed
267 268 269 270
fi 

if [ X"$BUILD" = X"1" ]; then
    #then build the cards
271
    sane
272
    deck "${CWD}"
notklaatu committed
273 274
fi

275 276 277
if [ X"$MERGE" = X"1" ]; then
    echo "------------------MERGING ---------------------"
    merge
notklaatu committed
278 279
fi

280 281 282 283
if [ X"$TIDY" = X"1" ]; then
    tidy
fi
    
notklaatu committed
284 285
if [ X"$DOC" = X"1" ]; then
    #and the docs
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    cat > "${CWD}"/doc/style/rego.xml << EOF

    <fop version="1.0">
<renderers>
  <renderer mime="application/pdf">
     <fonts>
        <directory recursive="true">$HOME/.local/share/fonts</directory>
        <auto-detect/>
     </fonts>
  </renderer>
</renderers>
</fop>
EOF
    doc "${CWD}"
    #/bin/rm "${CWD}"/doc/style/rego.xml
notklaatu committed
301 302
fi