-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaperer.sh
executable file
·359 lines (267 loc) · 7.74 KB
/
paperer.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/bin/bash
# The current directory of the script.
CURRENT_DIR=$(pwd)
# Installation directory for `paperer`.
INSTALL_DIR="/usr/local/paperer"
# Define the executable directory.
BIN_DIR=/usr/local/bin
# Repository URL fo `paperer`.
REPO_URL="https://github.com/mihaiconstantin/paperer.git"
# Directory where to prepare a new manuscript.
DIRECTORY=""
# Location where templates are stored.
TEMPLATES="$INSTALL_DIR/templates"
# The default template to scaffold.
TEMPLATE="apa"
# Actions to perform on the `paperer` installation.
UPDATE=false
UNINSTALL=false
# Flag counter to protect against too many flags.
FLAGS=0
# Display help.
display_help() {
echo "Usage: paperer [option]" >&2
echo
echo "Options:"
echo " -d, --directory=DIR The directory DIR where to prepare the paper without the trailing slash."
echo " -t, --template=TPL The template TPL to use for scaffolding. The default is 'apa'."
echo " Currently included templates with 'paperer' are: 'apa'."
echo " However, custom templates can be used by specifying the source folder."
echo " -s, --source=DIR Source directory where to look for the template TPL for scaffolding."
echo " The default is the 'templates' folder of the 'paperer' installation."
echo " This option is useful if you want to use your own templates."
echo " --update Update 'paperer' from GitHub."
echo " --uninstall Uninstall 'paperer'."
echo " --help Display this help message."
echo
echo "Description:"
echo " - Repository: https://github.com/mihaiconstantin/paperer"
echo " - Mihai Constantin ([email protected])"
echo
exit 1
}
# Set `git` hooks.
set_git_hooks() {
# Create `git` hooks.
cp $INSTALL_DIR/config/gitinfo2.txt "./.git/hooks/post-checkout" &> /dev/null
cp $INSTALL_DIR/config/gitinfo2.txt "./.git/hooks/post-commit" &> /dev/null
cp $INSTALL_DIR/config/gitinfo2.txt "./.git/hooks/post-merge" &> /dev/null
# If `cp` failed.
if [ $? -ne 0 ]; then
# User feedback.
echo " (!) Error: failed to copy 'git' hooks." >&2
# Return with error code.
return 1
fi
# Make hooks executable.
chmod +x "./.git/hooks/post-checkout"
chmod +x "./.git/hooks/post-commit"
chmod +x "./.git/hooks/post-merge"
# Checkout a temporary branch to trigger `gitHeadInfo.gin` creation.
git checkout --quiet -b setup
# Checkout the main branch.
git checkout --quiet main
# Delete the temporary branch.
git branch --quiet -D setup
# Return.
return 0
}
# Prepare the directory structure.
prepare() {
# Create the directory.
mkdir "$DIRECTORY" &> /dev/null
# If `mkdir` failed.
if [ $? -ne 0 ]; then
# User feedback.
echo " (!) Error: failed to created manuscript directory." >&2
# Return with error code.
return 1
fi
# Copy template directory contents.
cp -r "$TEMPLATES/$TEMPLATE/." "$DIRECTORY"
# Change directory.
cd "$DIRECTORY"
# Remove `.gitkeep` files.
find . -name ".gitkeep" -type f -delete
# Initialize `.git` repository.
git init . --quiet
# Add all files to the repository.
git add .
# Commit the changes.
git commit --quiet -m "Initial commit"
# Prepare `.git` hooks.
set_git_hooks
# If setting the `.git` hooks failed.
if [ $? -ne 0 ]; then
# User feedback.
echo " (!) Error: failed to install 'git' hooks." >&2
# Return with error code.
return 1
fi
# Change back the directory.
cd "$CURRENT_DIR"
# Otherwise return cleanly.
return 0
}
# Create update function.
update() {
# Change to installation directory.
cd "$INSTALL_DIR"
# Pull the latest changes from GitHub.
git pull --quiet
# If `git` failed.
if [ $? -ne 0 ]; then
# Return with error code.
return 1
fi
# Remove the previous executable.
rm $INSTALL_DIR/paperer
# Rename the executable.
cp $INSTALL_DIR/paperer.sh $INSTALL_DIR/paperer
# Make the executable.
chmod +x $INSTALL_DIR/paperer
# Change directory back.
cd "$CURRENT_DIR"
# Return.
return 0
}
# Create uninstall function.
uninstall() {
# Remove the installation directory.
sudo rm -rf $INSTALL_DIR
# Remove the executable.
sudo rm $BIN_DIR/paperer
# Return.
return 0
}
# Parse command line arguments
for i in "$@"
do
case $i in
# For the directory option.
-d=*|--directory=*)
# Set the directory.
DIRECTORY="${i#*=}"
# Increment the flag counter.
FLAGS=$((FLAGS+1))
shift
;;
# For the template option.
-t=*|--template=*)
# Set the template.
TEMPLATE="${i#*=}"
# Increment the flag counter.
FLAGS=$((FLAGS+1))
shift
;;
# For the templates location option.
-s=*|--source=*)
# Set the templates folder.
TEMPLATES="${i#*=}"
# Increment the flag counter.
FLAGS=$((FLAGS+1))
shift
;;
# For the update flag.
--update)
# Set the update flag.
UPDATE=true
# Increment the flag counter.
FLAGS=$((FLAGS+1))
shift
;;
# For the uninstall flag.
--uninstall)
# Set the uninstall flag.
UNINSTALL=true
# Increment the flag counter.
FLAGS=$((FLAGS+1))
shift
;;
# For the help flag.
-h|--help)
# Display help.
display_help
;;
# For everything else.
*)
# Display help.
display_help
;;
esac
done
# REMOVE THIS COMMENT.
# If no flags have been provided.
if [ $FLAGS -eq 0 ]
then
# Display the help message.
display_help
fi
# If an update is requested.
if [ "$UPDATE" = true ]; then
# User feedback.
echo " + Updating 'paperer'..."
# Perform the update.
update
# If the `update` failed.
if [ $? -ne 0 ]; then
# User feedback.
echo " (!) Error: failed to update 'paperer'." >&2
# Return with error code.
exit 1
fi
# User feedback.
echo " + Successfully updated 'paperer'!"
fi
# If uninstall is requested.
if [ "$UNINSTALL" = true ]; then
# User feedback.
echo " + Uninstalling 'paperer'..."
# Perform the uninstall.
uninstall
# If the `uninstall` failed.
if [ $? -ne 0 ]; then
# User feedback.
echo " (!) Error: failed to uninstall 'paperer'." >&2
# Return with error code.
exit 1
fi
# User feedback.
echo " + Successfully uninstalled 'paperer'."
fi
# If a directory has been provided.
if [ "$DIRECTORY" != "" ]; then
# If directory exists, do not proceed.
if [ -d "$DIRECTORY" ]; then
# Ask the users to uninstall or update it instead.
echo " (!) Error: '$DIRECTORY' already exists. Will not proceed." >&2
# Exit with error code.
exit 1
fi
# If the template does not exist.
if [ ! -d "$TEMPLATES/$TEMPLATE" ]; then
# User feedback.
echo " (!) Error: template '$TEMPLATE' not found." >&2
# Exit with error code.
exit 1
fi
# User feedback.
echo " + Preparing the manuscript using the '$TEMPLATE' template..."
# Prepare the manuscript.
prepare
# If `prepare` failed.
if [ $? -ne 0 ]; then
# If the directory was created anyway.
if [ -d "$DIRECTORY" ]; then
# Remove the directory.
rm -rf "$DIRECTORY"
fi
# User feedback.
echo " (!) Error: failed to prepare the manuscript at '$DIRECTORY'." >&2
# Exit with error code.
exit 1
fi
# Otherwise print a message.
echo " + Manuscript prepared at '$DIRECTORY'."
echo " + Happy crafting!"
fi