-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_patch.sh
executable file
·132 lines (106 loc) · 2.71 KB
/
generate_patch.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
#!/bin/bash
set -eu
source convert.sh
get_out_file_ref () {
local output_file="$1"
echo ".$output_file"
}
start_patch () {
local input_file="$1"
local output_file="$2"
local output_file_ref="$3"
local output_file_diff="$4"
# convert once and save a reference "raw" file (i.e. without current patches)
convert "$input_file" "$output_file"
cp "$output_file" "$output_file_ref"
patch_modifs "$output_file_diff"
}
end_patch () {
local input_file="$1"
local output_file="$2"
local output_file_ref="$3"
local output_file_diff="$4"
if [[ ! -f "$output_file_ref" ]]
then
echo "Please call \"generate_patch.sh $input_file start\" first!"
return 1
fi
mkdir -p "patches/print"
# generate diff
# Note: If there is an actual difference between the two files, the diff
# command returns non-0. Consequently, the call is marked to never fail.
diff -au "$output_file_ref" "$output_file" >"patches/print/$output_file_diff" || true
# remove reference file
rm --force "$output_file_ref"
}
usage () {
cat <<EOF
generate_patch.sh [-h] FILE {start|end}
Generate a patch for the final document.
Positional arguments:
FILE
Input Markdown file.
Subcommands:
start
Start the patch generation.
end
Finalize the patch generation.
Optional arguments:
-h
Show this help message and exit.
EOF
}
main () {
while getopts ":h" option
do
case $option in
h) # help
usage
return 0
;;
*)
echo "Unknown option $OPTARG"
usage
return 1
;;
esac
done
shift $((OPTIND-1))
if [[ -z "${1+_}" ]]
then
echo "Missing argument"
usage
return 1
fi
local input_file="$1"
local output_file="$(get_out_file $input_file)"
local output_file_ref="$(get_out_file_ref $output_file)"
local output_file_diff="$(get_out_file_diff $output_file)"
if [[ -z "${2+_}" ]]
then
echo "Missing argument"
usage
return 1
fi
local mode="$2"
case $mode in
"start")
start_patch "$input_file" "$output_file" "$output_file_ref" "$output_file_diff"
echo "You can modify $output_file for final edits"
return 0
;;
"end")
end_patch "$input_file" "$output_file" "$output_file_ref" "$output_file_diff"
echo "Patch generated"
return 0
;;
*)
echo "Unknown mode $mode"
usage
return 1
esac
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
main "$@"
fi