feat(user/mw_wc2sp.sh):Save wikicode files in separate dir
[BK-2020-03.git] / user / combine_pdfs.sh
1 #!/usr/bin/env bash
2 # Desc: Combines PDF files into a single PDF file
3 # Depends: pdftk 2.02-5 ( https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ )
4 # Version 0.0.1
5
6 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
7 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
8 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
9 showUsage() {
10 # Desc: Display script usage information
11 # Usage: showUsage
12 # Version 0.0.2
13 # Input: none
14 # Output: stdout
15 # Depends: GNU-coreutils 8.30 (cat)
16 cat <<'EOF'
17 USAGE:
18 combine_pdfs.sh [FILE output] [FILES input...]
19
20 EXAMPLE:
21 combine_pdfs.sh output.pdf input1.pdf input2.pdf
22 combine_pdfs.sh output.pdf input*.pdf
23 EOF
24 } # Display information on how to use this script.
25 checkDepends() {
26 if ! command -v pdftk 1>/dev/random 2>&1; then
27 showUsage;
28 die "FATAL:Missing pdftk."; fi;
29 }; # Check dependencies
30 checkArgs() {
31 local narg;
32
33 # Check arguments
34 narg=0;
35 for arg in "$@"; do
36 #yell "DEBUG:Checking argument:$arg";
37 if [[ $narg -le 0 ]]; then
38 if [[ ! -f "$arg" ]]; then
39 ((narg++)); continue;
40 else
41 ## Get permission to overwrite existing output file
42 yell "WARNING:Overwrite output file \"$arg\"? (y/n)";
43 read -r prompt;
44 case "$prompt" in
45 "y" | "yes" | "Yes" | "YES" )
46 ;;
47 "n" | "no" | "No" | "NO" )
48 showUsage; die "FATAL:Aborted.";
49 ;;
50 *)
51 showUsage; die "FATAL:Invalid response.";
52 ;;
53 esac;
54 fi;
55 fi; # handle first arg
56
57 if [[ ! -f "$arg" ]]; then
58 showUsage; die "FATAL:File does not exist:$arg"; fi;
59 ((narg++));
60 done;
61
62 return 0;
63 };
64 combinePDFs() {
65 # Desc: Combines PDFs and writes to output PDF file
66 # Depends: pdftk 2.02-5
67 local cmd path_fo;
68 local -a paths_fi;
69
70 # Save output file path
71 path_fo="$1";
72
73 # Form array of input file paths
74 paths_fi=("$@");
75 unset 'paths_fi[0]'; # discard first path which is output file
76
77 # Form command array
78 cmd+=("pdftk"); # executable
79 cmd+=("${paths_fi[@]}"); # input file paths
80 cmd+=("cat" "output");
81 cmd+=("$path_fo");
82 #yell "DEBUG:cmd:$(declare -p cmd)";
83 #yell "DEBUG:cmd:${cmd[*]}";
84
85 # Execute command array
86 "${cmd[@]}";
87 }; # Combines PDFs and writes to output PDF file
88 main() {
89 #yell "DEBUG:$(args=("$@"); declare -p args)";
90 checkDepends;
91 checkArgs "$@";
92 combinePDFs "$@";
93 exit 0;
94 }; # main program
95
96 main "$@";
97
98 # Author: Steven Baltakatei Sandoval
99 # License: GPLv3+
100
101 # # Copyright notices
102
103 # pdftk port to java 3.2.2 a Handy Tool for Manipulating PDF Documents
104 # Copyright (c) 2017-2018 Marc Vinyals - https://gitlab.com/pdftk-java/pdftk
105 # Copyright (c) 2003-2013 Steward and Lee, LLC.
106 # pdftk includes a modified version of the iText library.
107 # Copyright (c) 1999-2009 Bruno Lowagie, Paulo Soares, et al.
108 # This is free software; see the source code for copying conditions. There is
109 # NO warranty, not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
110
111