Debian Codegraph @

bin/daps-init

raw
1 #!/bin/bash
2 #
3 # Copyright (C) 2012-2020 SUSE Software Solutions Germany GmbH
4 #
5 # Author:
6 # Frank Sundermeyer <fsundermeyer at opensuse dot org>
7 # Thomas Schraitle
8 # Vanessa Wallfahrer
9 #
10 # Initialize a working directory to get started with daps
11 #
12
13 #----------------
14 # Verbose error handling
15 #
16 function exit_on_error {
17 ccecho "error" "ERROR: ${1}" >&2
18 exit 1;
19 }
20
21 function usage {
22 cat <<EOF_helptext
23 $ME --docdir <PATH TO DIRECTORY> [OPTIONS]
24
25 Create a working environment for DAPS with an example DocBook document.
26 Specifying the project directory with --docdir is mandatory, all other
27 options are optional.
28 Specifying a name for the document with "--name" is recommended--it will
29 be used in filenames. Setting the title of the document with "--title"
30 is also recommended.
31
32 Options:
33
34 --docdir=<DIR>
35 Path to the project directory under which the DAPS environment will be
36 set up. If the directory does not exists, it will be created. Mandatory.
37
38 --dapsroot=<DIR>
39 Specify the path to the directory where daps-init can find the template
40 files (directory init-templates/). This is only necessary if you have
41 installed DAPS in a custom directory. Default: /usr/share/daps
42
43 --date=<YYYY-MM-DD>
44 Specify a date in the format YYYY-MM-DD. Will be used to set the
45 publication date of the document. (<date/> in the document's info
46 section). Default: Current date (<?dbtimestamp format=\"B d, Y\"?>)
47
48 --docbook4
49 Create a document in DocBook 4 format (default)
50
51 --docbook5
52 Create a document in DocBook 5 format
53
54 --name=<NAME>
55 String that will be used in filenames (e.g. when generating PDF or HTML
56 output). Only use letters, digits, dashes and underscores [A-Za-z0-9-_].
57 Default: daps-example
58
59 --productname=<PRODUCT>
60 Specify a name for the product the document describes. Will be used in the
61 subtitle of the document (<productname/> in the document's info section)
62
63 --productnumber=<PRODUCT_VERSION>
64 Specify a version for the product the document describes. Will be used in
65 the subtitle of the document (<productnumber/> in the document's info
66 section). Requires --productname.
67
68 --rootelement=<article|book|set>
69 DocBook root element of the example document. Currently supported
70 are \"book\", \"article\" (default) and \"set\".
71
72 --title=<TITLE>
73 Title of the document. Default: "DAPS Example"
74 EOF_helptext
75 exit 0
76 }
77
78 #################################################
79 # MAIN
80 #
81
82 ME=$(basename "$0")
83
84 trap "exit_on_error '\nCaught SIGTERM/SIGINT'" SIGTERM SIGINT
85
86 # show help when called with no arguments
87 [[ -z "$1" ]] && usage
88
89 # check for xmlstarlet binary
90 for _binary in xmlstarlet xml; do
91 XMLSTARLET=$(which $_binary 2>/dev/null)
92 [[ 0 -eq $? ]] && break
93 done
94
95 [[ -z $XMLSTARLET ]] && exit_on_error "Required package \"xmlstarlet\" is not installed"
96
97 #----------------
98 # Parse the command line arguments
99
100 ARGS=$(getopt -o 45d:hn:r:t: -l date:,dapsroot:,docbook4,docbook5,docdir:,help,name:,productname:,productnumber:,rootelement:,title: -n "$ME" -- "$@")
101
102 eval set -- "$ARGS"
103
104 while true ; do
105 case "$1" in
106 --dapsroot)
107 if [[ -d "$2" ]]; then
108 DAPSROOT="$2"
109 else
110 exit_on_error "dapsroot \"$2\" is not a valid directory."
111 fi
112 shift 2
113 ;;
114 --date)
115 if [[ -z "$2" ]]; then
116 exit_on_error "Date must be set (YYYY-MM-DD)"
117 else
118 DATE="$2"
119 date -d "$DATE" > /dev/null 2>&1 || echo "Wrong date format. Use YYYY-MM-DD"
120 fi
121 shift 2
122 ;;
123 --docdir|-d)
124 DOC_DIR="$2"
125 shift 2
126 ;;
127 --docbook4|-4)
128 DOCBOOKVERSION=4
129 shift
130 ;;
131 --docbook5|-5)
132 DOCBOOKVERSION=5
133 shift
134 ;;
135 --help|-h)
136 usage
137 ;;
138 --name|-n)
139 if [[ -z "$2" ]]; then
140 exit_on_error "Please specify a name"
141 elif [[ ! $2 =~ ^[-_A-Za-z0-9]*$ ]]; then
142 exit_on_error "The name must not contain characters other than [-_A-za-z0-9]"
143 else
144 NAME="$2"
145 fi
146 shift 2
147 ;;
148 --productname)
149 if [[ -z "$2" ]]; then
150 exit_on_error "Productname must be set"
151 else
152 PRODUCTNAME="$2"
153
154 fi
155 shift 2
156 ;;
157 --productnumber)
158 if [[ -z "$2" ]]; then
159 exit_on_error "Productnumber must be set"
160 else
161 PRODUCTNUMBER="$2"
162 fi
163 shift 2
164 ;;
165 --rootelement|-r)
166 if [[ article = "$2" || book = "$2" || "set" = "$2" ]]; then
167 ROOTELEMENT="$2"
168 else
169 exit_on_error "Rootelement must be either \"article\", \"book\" or \"set\""
170 fi
171 shift 2
172 ;;
173 --title|-t)
174 if [[ -z "$2" ]]; then
175 exit_on_error "Title must be set"
176 else
177 TITLE="$2"
178 fi
179 shift 2
180 ;;
181 --) shift ; break ;;
182 *) exit_on_error "Wrong parameter: $1" ;;
183 esac
184 done
185
186 if [[ -z "$DOC_DIR" ]]; then
187 exit_on_error "Please specify a directory with --docdir"
188 else
189 # strip trailing slash
190 DOC_DIR=${DOC_DIR%/}
191 fi
192
193 if [[ -n $PRODUCTNUMBER && -z $PRODUCTNAME ]]; then
194 exit_on_error "--productnumber also requires --productname to be specified"
195 fi
196
197 #---------------------
198 # Defaults:
199
200 [[ -z "$DAPSROOT" ]] && DAPSROOT="/usr/share/daps"
201 [[ -z "$DATE" ]] && DATE='<?dbtimestamp format="B d, Y"?>'
202 [[ -z "$DOCBOOKVERSION" ]] && DOCBOOKVERSION="4"
203 [[ -z "$NAME" ]] && NAME="daps-example"
204 [[ -z "$ROOTELEMENT" ]] && ROOTELEMENT="article"
205 [[ -z "$TITLE" ]] && TITLE="DAPS Example"
206
207
208 TEMPLATE_DIR="${DAPSROOT}/init_templates"
209
210 DC_TEMPLATE="${TEMPLATE_DIR}/DC-file.template"
211 MAIN_TEMPLATE="${TEMPLATE_DIR}/MAIN.db${DOCBOOKVERSION}-$ROOTELEMENT.template"
212 DC_FILE="${DOC_DIR}/DC-${NAME}"
213 MAIN="${DOC_DIR}/xml/MAIN-${NAME}.xml"
214 IMG_DIR="${DOC_DIR}/images/src"
215
216
217 #----------------
218 # Create the project directory if it does not exist
219 #
220
221 CONT=""
222 if [[ ! -d $DOC_DIR ]]; then
223 while [[ ! $CONT =~ ^[NnYy]$ ]] ; do
224 read -e -t 20 -i y -p "$DOC_DIR does not exist. Create it? [y/n] " CONT
225 [[ $? -gt 128 ]] && CONT="y" # timeout reached
226 done
227 if [[ $CONT =~ ^[Yy]$ ]]; then
228 mkdir -p "$DOC_DIR" || exit_on_error "Cannot create $DOC_DIR"
229 else
230 exit_on_error "Aborted by user"
231 fi
232 fi
233
234 # Create needed subdirs in DOC_DIR
235 mkdir -p "${DOC_DIR}"/images/src/{dia,jpg,png,svg,eps,pdf} "${DOC_DIR}/xml" || exit_on_error "Cannot create needed subdirectories in $DOC_DIR"
236
237 #----------------
238 # copy the templates
239 #
240
241 # DC-file
242 cp "$DC_TEMPLATE" "$DC_FILE" || exit_on_error "Failed to copy $DC_TEMPLATE"
243 # MAIN
244 cp "$MAIN_TEMPLATE" "$MAIN" || exit_on_error "Failed to copy $MAIN_TEMPLATE"
245 # graphics
246 cp "${DAPSROOT}"/init_templates/example{1,2}.png "${IMG_DIR}/png/" || exit_on_error "Failed to copy images from $TEMPLATE_DIR}"
247
248 #----------------
249 # Adjust the content in the tempate files
250
251 # replace placeholder in DC-file with $MAIN
252 sed -i "s/§§MAIN§§/$(basename "$MAIN")/g" "$DC_FILE"
253
254 # replace DATE in MAIN
255 sed -i "s/§§DATE§§/$DATE/g" "$MAIN"
256
257 # replace TITLE in MAIN
258 sed -i "s/§§TITLE§§/$TITLE/g" "$MAIN"
259
260 # insert <productname> and <productnumber> tags
261
262 if [[ -n $PRODUCTNAME ]]; then
263 if [[ $DOCBOOKVERSION -eq 5 ]]; then
264 XPATH="/_:${ROOTELEMENT}/_:info/_:title"
265 else
266 XPATH="/${ROOTELEMENT}/${ROOTELEMENT}info/title"
267 fi
268
269 $XMLSTARLET ed --inplace --append "$XPATH" --type elem --name productname \
270 --value "$PRODUCTNAME" "$MAIN"
271 if [[ -n $PRODUCTNUMBER ]]; then
272 $XMLSTARLET ed --inplace --append "$XPATH" --type elem \
273 --name productnumber --value "$PRODUCTNUMBER" "$MAIN"
274 fi
275 fi
276
277 #----------------
278 #
279 ccecho "result" "Successfully created a DAPS Project at ${DOC_DIR}"
280 echo "To build a PDF or HTML version of the example document, run:
281 daps -d $DC_FILE pdf
282 or
283 daps -d $DC_FILE html"
284
285 exit 0;