-
Notifications
You must be signed in to change notification settings - Fork 3
/
docx2hub.sh
executable file
·131 lines (114 loc) · 2.58 KB
/
docx2hub.sh
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
#!/bin/bash
function usage {
echo ""
echo "docx2hub"
echo ""
echo "Usage: d2h [options ...] <docx file>"
echo ""
echo "Options:"
echo " -o relative or absolute path to custom output directory"
echo " -d debug mode"
1>&2; exit 1;
}
function exitonerror {
echo "Errors encountered while running $2. Exited with code $1."
echo "For details see $LOG"
exit 1
}
function log {
2>&1 2>>$OUT_DIR/$BASENAME.d2h.log
}
# cygwin check
cygwin=false;
case "`uname`" in
CYGWIN*) cygwin=true;
esac
# script directory
DIR="$( cd -P "$(dirname $( readlink -f "$0" ))" && pwd )"
PWD=$(readlink -f .)
CALABASH=$DIR/calabash/calabash.sh
HEAP=1024m
# specify options
while getopts ":o:d" opt; do
case "${opt}" in
o)
OUT_DIR=${OPTARG}
;;
d)
DEBUG=yes
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
shift $((OPTIND-1))
# check if argument for file is set
if [ -z $1 ]; then
usage
fi
FILE=$(readlink -f $1)
BASENAME=$(basename $FILE .docx)
if [[ -z "$OUT_DIR" ]]; then
OUT_DIR="$(dirname $(readlink -f "$FILE" ))"
else
if [[ "$OUT_DIR" != /* ]]; then
OUT_DIR="$PWD/$OUT_DIR"
fi
fi
mkdir -p $OUT_DIR
# set log
LOG=$OUT_DIR/$BASENAME.log
# remove log from previous runs
if [ -e $LOG ]; then
rm $LOG
fi
# debugging
DEBUG_DIR=$OUT_DIR/$BASENAME.debug
# make absolute paths
if $cygwin; then
FILE=$(cygpath -ma "$FILE")
DIR=$(cygpath -ma "$DIR")
OUT_DIR=$(cygpath -ma "$OUT_DIR")
DEBUG_DIR_URI=file:/$(cygpath -ma "$DEBUG_DIR" )
else
DEBUG_DIR_URI=file:$(readlink -f $DEBUG_DIR)
fi
# check if file exists
if [ ! -f $FILE ]; then
echo "Error: input file not found: $FILE"
usage
fi
echo "starting docx2hub"
if [ "$DEBUG" = "yes" ]; then
echo "debug mode: $DEBUG"
echo "storing debug files to $DEBUG_DIR"
echo ""
echo "Parameters"
echo " workdir: $DIR"
echo " outdir: $OUT_DIR"
echo " file: $FILE"
echo ""
fi
# docx2hub xproc pipeline
HEAP=$HEAP $CALABASH \
-o result=$OUT_DIR/$BASENAME.xml \
-o report=$OUT_DIR/$BASENAME.report.svrl \
-o zip-manifest=$OUT_DIR/$BASENAME.manifest.xml \
$DIR/xpl/docx2hub-frontend.xpl \
docx=$FILE \
debug=$DEBUG \
debug-dir-uri=$DEBUG_DIR_URI \
status-dir-uri=$DEBUG_DIR_URI/status 2>&1 2>>$LOG || exitonerror $? xproc
# delete temp files
if [ "$DEBUG" != "yes" ]; then
rm -rf $FILE.tmp $DEBUG_DIR
fi
echo "writing xml => $OUT_DIR/$BASENAME.xml"
echo "docx2hub finished, for details see $LOG"
echo ""