feat(bktemp):Add promptLoop template
[BK-2020-03.git] / user / bkcc
1 #!/bin/bash
2
3 # Date: 2020-02-08T22:21Z
4
5 # Author: Steven Baltakatei Sandoval
6
7 # License: This bash script, 'bkcc', is licensed under GPLv3 or
8 # later by Steven Baltakatei Sandoval:
9 #
10 # 'bkcc', a Creative Commons HTML tag generator.
11 # Copyright (C) 2020 Steven Baltakatei Sandoval (baltakatei.com)
12 #
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # A copy of the GNU General Public License may be found at
24 # <https://www.gnu.org/licenses/>.
25 #
26 # The structure of this script was modeled after the following
27 # Creative Commons license choosers:
28 #
29 # https://creativecommons.org/choose/
30 # https://chooser-beta.creativecommons.org/
31
32 # Description: bkcc - A tool that creates a machine-readable (RDFa
33 # XML) Creative Commons license tag. The tag is generated using
34 # information prompted from the user and exported to stdout.
35
36 # Usage: bkcc [ OPTIONS ]
37
38
39 #==DEFINITIONS==
40
41 # regular expression for identifying valid URLs (source: http://regexlib.com/REDetails.aspx?regexp_id=1051 )
42 regexURL="((http\://|https\://|ftp\://)|(www.))+(([a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9%:/-_\?\.'~]*)?"
43
44 # Define 'echoerr' function which outputs text to stderr
45 # Note: function copied from https://stackoverflow.com/a/2990533
46 echoerr() {
47 echo "$@" 1>&2;
48 }
49
50 promptAttribute() {
51 ###echoerr "Note: RECOMMENDED options promote \"Free Cultural Works\" as defined by creativecommons.org"
52 echoerr "======================================================="
53 echoerr "Do you want to require attribution for your work? (y/n)"
54 echoerr " y) Yes, anyone using my work must give me appropriate credit (RECOMMENDED)."
55 echoerr " n) No, anyone can use my work even without attribution."
56 read m
57 case $m in
58 y | Y | yes | 1) OPTION_BY="true";;
59 n | N | no | 2)
60 echoerr "======================================================="
61 echoerr "I hereby waive all copyright and related or neighboring rights together with all"
62 echoerr "associated claims and causes of action with respect to this work to the extent"
63 echoerr "possible under law according to"
64 echoerr "https://creativecommons.org/publicdomain/zero/1.0/ ."
65 echoerr ""
66 echoerr "I have read and understood the terms and intended legal effect of CC0, and"
67 echoerr "hereby voluntarily elect to apply it to this work (y/n):"
68 read m
69 case $m in
70 y | Y | yes | 1) OPTION_BY="false";
71 echoerr "DEBUG: OPTION_BY set to $OPTION_BY"
72 return 1
73 ;;
74 n | N | no | 2) echoerr "No license selected. Exiting."; exit 1;;
75 *) echoerr "Invalid CC0 release option. Exiting."; exit 1;;
76 esac ;;
77 *) echoerr "Invalid attribution requirement option. Exiting."; exit 1;;
78 esac
79 }
80
81 promptCommercial() {
82 echoerr "======================================================="
83 echoerr "Do you want to prohibit others commercial use of your work? (y/n)"
84 echoerr " y) Yes, others cannot use my work for commercial purposes."
85 echoerr " n) No, others can use my work for commercial purposes (RECOMMENDED)."
86 read m
87 case $m in
88 y | Y | yes | 1) OPTION_NC="true"; echoerr "DEBUG: OPTION_NC set to $OPTION_NC";;
89 n | N | no | 2) OPTION_NC="false"; echoerr "DEBUG: OPTION_NC set to $OPTION_NC";;
90 *) echoerr "Invalid commercial use permission option. Exiting."; exit 1;;
91 esac
92 }
93
94 promptDerivatives() {
95 echoerr "======================================================="
96 echoerr "Do you want to prohibit others from distributing your work if they remix, transform, or build upon it?"
97 echoerr " y) Yes, others can use and share my work only as is, without modifications."
98 echoerr " n) No, others may share modified versions of my work (RECOMMENDED)."
99 read m
100 case $m in
101 y | Y | yes | 1) OPTION_ND="true"; echoerr "DEBUG: OPTION_ND set to $OPTION_ND";;
102 n | N | no | 2) OPTION_ND="false"; echoerr "DEBUG: OPTION_ND set to $OPTION_ND";;
103 *) echoerr "Invalid derivative use permission option. Exiting."; exit 1;;
104 esac
105 }
106
107 promptShareAlike() {
108 echoerr "======================================================="
109 echoerr "Do you want to prohibit your work under other terms?"
110 echoerr " y) Yes, anyone who changes your work must share their contributions under the same license as the original. (RECOMMENDED)"
111 echoerr " n) No, anyone who changes your work can distribute their contributions under a different license."
112 read m
113 case $m in
114 y | Y | yes | 1) OPTION_SA="true"; echoerr "DEBUG: OPTION_SA set to $OPTION_SA";;
115 n | N | no | 2) OPTION_SA="false"; echoerr "DEBUG: OPTION_SA set to $OPTION_SA";;
116 *) echoerr "Invalid sharealike requirement option. Exiting."; exit 1;;
117 esac
118 }
119
120 promptLicenseDetails() {
121 # Usage: promptLicenseDetails
122 # Description: Sets LICENSE_TYPE by questioning user.
123
124 echoerr "DEBUG: Beginning promptLicenseDetails function."
125
126 # Evaluate BY option.
127 # Ask if author wishes to be attributed.
128 promptAttribute # set OPTION_BY to true or false.
129 if [[ $OPTION_BY == "false" ]]; then
130 LICENSE_TYPE="CC 1.0"
131 echoerr "DEBUG: LICENSE_TYPE set to $LICENSE_TYPE"
132 return 1;
133 fi
134
135 # Evaluate NC option.
136 # Ask if author permits commercial use of work.
137 promptCommercial # set OPTION_NC to true or false.
138
139 # Evaluate ND option.
140 # Ask if author permits derivative use of work.
141 promptDerivatives # set OPTION_ND to true or false.
142
143 # Evaluate SA option.
144 # Ask if author requires derivative works to also carry same license as work.
145 if [[ $OPTION_ND = "true" ]]; then
146 OPTION_SA="false" # An SA work cannot be made from an ND work.
147 else
148 promptShareAlike # set OPTION_SA to true or false.
149 fi
150
151 # Check for CC BY
152 if [[ $OPTION_BY == "true" ]] && \
153 [[ $OPTION_NC == "false" ]] && \
154 [[ $OPTION_ND == "false" ]] && \
155 [[ $OPTION_SA == "false" ]]
156 then
157 LICENSE_TYPE="CC BY 4.0"
158 return
159 fi
160
161 # Check for CC BY-SA
162 if [[ $OPTION_BY == "true" ]] && \
163 [[ $OPTION_NC == "false" ]] && \
164 [[ $OPTION_ND == "false" ]] && \
165 [[ $OPTION_SA == "true" ]]
166 then
167 LICENSE_TYPE="CC BY-SA 4.0"
168 return
169 fi
170
171 # Check for CC BY-ND
172 if [[ $OPTION_BY == "true" ]] && \
173 [[ $OPTION_NC == "false" ]] && \
174 [[ $OPTION_ND == "true" ]] && \
175 [[ $OPTION_SA == "false" ]]
176 then
177 LICENSE_TYPE="CC BY-ND 4.0"
178 return
179 fi
180
181 # Check for CC BY-NC
182 if [[ $OPTION_BY == "true" ]] && \
183 [[ $OPTION_NC == "true" ]] && \
184 [[ $OPTION_ND == "false" ]] && \
185 [[ $OPTION_SA == "false" ]]
186 then
187 LICENSE_TYPE="CC BY-NC 4.0"
188 return
189 fi
190
191 # Check for CC BY-NC-SA
192 if [[ $OPTION_BY == "true" ]] && \
193 [[ $OPTION_NC == "true" ]] && \
194 [[ $OPTION_ND == "false" ]] && \
195 [[ $OPTION_SA == "true" ]]
196 then
197 LICENSE_TYPE="CC BY-NC-SA 4.0"
198 return
199 fi
200
201 # Check for CC BY-NC-ND
202 if [[ $OPTION_BY == "true" ]] && \
203 [[ $OPTION_NC == "true" ]] && \
204 [[ $OPTION_ND == "true" ]] && \
205 [[ $OPTION_SA == "false" ]]
206 then
207 LICENSE_TYPE="CC BY-NC-ND 4.0"
208 return
209 fi
210
211 # Error message if none of the above if statements returned out of function.
212 echoerr "ERROR: Invalid license specified or LICENSE_TYPE not set."; exit 1;
213
214 }
215
216 selectLicense() {
217 # Usage: selectLicense
218 # Description: Sets LICENSE_TYPE variable according to a set of user prompts.
219
220 # Prompt license type
221 echoerr "Select license type:"
222 echoerr " 0) I don't know. Guide me."
223 echoerr " 1) CC0 1.0 \"CC0 1.0 Universal\""
224 echoerr " 2) CC BY 4.0 \"Attribution 4.0 International\""
225 echoerr " 3) CC BY-SA 4.0 \"Attribution-ShareAlike 4.0 International\""
226 echoerr " 4) CC BY-ND 4.0 \"Attribution-NoDerivatives 4.0 International\""
227 echoerr " 5) CC BY-NC 4.0 \"Attribution-NonCommercial 4.0 International\""
228 echoerr " 6) CC BY-NC-SA 4.0 \"Attribution-NonCommercial-ShareAlike 4.0 International\""
229 echoerr " 7) CC BY-NC-ND 4.0 \"Attribution-NonCommercial-NoDerivatives 4.0 International\""
230 read n
231 case $n in
232 0) promptLicenseDetails; return;; # Sets LICENSE_TYPE via many questions to user.
233 1) LICENSE_TYPE="CC 1.0";;
234 2) LICENSE_TYPE="CC BY 4.0";;
235 3) LICENSE_TYPE="CC BY-SA 4.0";;
236 4) LICENSE_TYPE="CC BY-ND 4.0";;
237 5) LICENSE_TYPE="CC BY-NC 4.0";;
238 6) LICENSE_TYPE="CC BY-NC-SA 4.0";;
239 7) LICENSE_TYPE="CC BY-NC-ND 4.0";;
240 *) echoerr "Invalid option selected. Exiting."; exit 1;;
241 esac
242
243 }
244
245 #==MAIN PROGRAM==
246 selectLicense # Sets LICENSE_TYPE via simple prompt.
247
248 # Display value of $LICENSE_TYPE
249 echoerr "======================================================="
250 echoerr "$LICENSE_TYPE"" license selected."
251
252 # Prompt metadata
253 echoerr "======================================================="
254 echoerr "Specify metadata (leave blank if not needed):"
255 read -p " Title of work [title]:" workTitle
256 read -p " Attribute work to [author]:" workAuthor
257
258 read -p " Attribute work to (ex: author's website) [URL]:" workAuthorURL
259 if [[ -n $workAuthorURL ]]; then
260 while [[ ! $workAuthorURL =~ $regexURL ]]; do
261 echoerr "WARNING: URL may contain error (ex: does not begin with \"https://\"."
262 echoerr " Preappend \"http://\" to URL? (y/n)"
263 read m
264 case $m in
265 y | Y | yes | 1) workAuthorURL="http://""$workAuthorURL" ;;
266 n | N | no | 2) read -p " Attribute work to (ex: author's website) [URL]:" workAuthorURL ;;
267 *) ;;
268 esac
269 done
270 fi
271
272 read -p " Source work from which work was derived (leave blank if original work) [URL]:" workSourceURL
273 if [[ -n $workSourceURL ]]; then
274 while [[ ! $workSourceURL =~ $regexURL ]]; do
275 echoerr "WARNING: URL may contain error (ex: does not begin with \"https://\."
276 echoerr " Preappend \"http://\" to URL? (y/n)"
277 read m
278 case $m in
279 y | Y | yes | 1) workSourceURL="http://""$workSourceURL" ;;
280 n | N | no | 2) read -p " Source work from which work was derived (leave blank if original work) [URL]:" workSourceURL ;;
281 *);;
282 esac
283 done
284 fi
285
286 read -p " Webpage where more additional permissions may be requested (OPTIONAL) [URL]:" morePermissionsURL
287 if [[ -n $morePermissionsURL ]]; then
288 while [[ ! $morePermissionsURL =~ $regexURL ]]; do
289 echoerr "WARNING: URL may contain error (ex: does not begin with \"https://\."
290 echoerr " Preappend \"http://\" to URL? (y/n)"
291 read m
292 case $m in
293 y | Y | yes | 1) morePermissionsURL="http://""$morePermissionsURL";;
294 n | N | no | 2) read -p " Webpage where more additional permissions may be requested (OPTIONAL) [URL]:" morePermissionsURL ;;
295 esac
296 done
297 fi
298
299 #===CONSTRUCT RDFA TAG===
300 #====RDFA:Handle workTitle, workAuthor, workAuthorURL====
301 ###echoerr "DEBUG: workTitle is --$workTitle--"
302 ###echoerr "DEBUG: workAuthor is --$workAuthor--"
303 ###echoerr "DEBUG: workAuthorURL is --$workAuthorURL--"
304 ###echoerr "DEBUG: workSourceURL is --$workSourceURL--"
305 ###echoerr "DEBUG: morePermissionsURL is --$morePermissionsURL--"
306 ###echoerr "DEBUG: LICENSE_TYPE is --$LICENSE_TYPE--"
307
308 # Exit early if critical parameters not provided
309 if [[ -z "$workTitle" ]] || \
310 [[ ! -v LICENSE_TYPE ]];
311 then
312 echoerr "ERROR: Problem with workTitle or LICENSE_TYPE."
313 exit 1;
314 fi
315
316 # Add workTitle to OUTPUT variable
317 OUTPUT=$OUTPUT"<span xmlns:dct=\"http://purl.org/dc/terms/\" property=\"dct:title\">$workTitle</span>"
318
319 # Add workAuthor and/or workAuthorURL to OUTPUT variable (if applicable)
320 if [[ -v workAuthor ]]; then
321 if [[ -v workAuthorURL ]]; then
322 # workAuthor set, workAuthorURL set
323 OUTPUT=$OUTPUT" by <a xmlns:cc=\"http://creativecommons.org/ns#\" href=\"$workAuthorURL\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">$workAuthor</a>"
324 else
325 #workAuthor set, workAuthorURL not set
326 OUTPUT=$OUTPUT" by <span xmlns:cc=\"http://creativecommons.org/ns#\" property=\"cc:attributionName\">$workAuthor</span>"
327 fi
328 fi
329
330 #====RDFA:Handle LICENSE_TYPE====
331 # Add selected license to OUTPUT variable
332
333 # Add boilerplate
334 OUTPUT=$OUTPUT" is licensed under "
335
336 # Add license text
337 case $LICENSE_TYPE in
338 "CC 1.0")
339 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/publicdomain/zero/1.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC0 1.0</a><a href=\"https://creativecommons.org/publicdomain/zero/1.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-cc0_icon.svg\" /></a>"
340 ;;
341 "CC BY 4.0")
342 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/licenses/by/4.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC BY 4.0</a><a href=\"https://creativecommons.org/licenses/by/4.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-by_icon.svg\" /></a>"
343 ;;
344 "CC BY-SA 4.0")
345 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/licenses/by-sa/4.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC BY-SA 4.0</a><a href=\"https://creativecommons.org/licenses/by-sa/4.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-by_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-sa_icon.svg\" /></a>"
346 ;;
347 "CC BY-ND 4.0")
348 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/licenses/by-nd/4.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC BY-ND 4.0</a><a href=\"https://creativecommons.org/licenses/by-nd/4.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-by_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-nd_icon.svg\" /></a>"
349 ;;
350 "CC BY-NC 4.0")
351 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/licenses/by-nc/4.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC BY-NC 4.0</a><a href=\"https://creativecommons.org/licenses/by-nc/4.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-by_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-nc_icon.svg\" /></a>"
352 ;;
353 "CC BY-NC-SA 4.0")
354 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/licenses/by-nc-sa/4.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC BY-NC-SA 4.0</a><a href=\"https://creativecommons.org/licenses/by-nc-sa/4.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-by_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-nc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-sa_icon.svg\" /></a>"
355 ;;
356 "CC BY-NC-ND 4.0")
357 OUTPUT=$OUTPUT"<a href=\"https://creativecommons.org/licenses/by-nc-nd/4.0/\" target=\"_blank\" rel=\"license noopener noreferrer\" style=\"display: inline-block;\">CC BY-NC-ND 4.0</a><a href=\"https://creativecommons.org/licenses/by-nc-nd/4.0/\"><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-by_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-nc_icon.svg\" /><img style=\"height:22px!important;margin-left: 3px;vertical-align:text-bottom;opacity:0.7;\" src=\"https://search.creativecommons.org/static/img/cc-nd_icon.svg\" /></a>"
358 ;;
359 *) echoerr "Error."; exit 1;;
360 esac
361 # End sentence.
362 OUTPUT=$OUTPUT"."
363
364
365 #====RDFA:Handle workSourceURL, morePermissionsURL (if applicable)====
366 # Add workSourceURL to OUTPUT (if applicable)
367 if [[ -n "$workSourceURL" ]]; then
368 OUTPUT=$OUTPUT"<br />$workTitle is based on a work at <a xmlns:dct=\"http://purl.org/dc/terms/\" href=\"$workSourceURL\" rel=\"dct:source\">$workSourceURL</a>"
369 # End sentence.
370 OUTPUT=$OUTPUT"."
371 fi
372
373 # Add morePermissionsURL to OUTPUT (if applicable)
374 if [[ -n "$morePermissionsURL" ]]; then
375 OUTPUT=$OUTPUT"<br />Permissions beyond the scope of this license may be available at <a xmlns:cc=\"http://creativecommons.org/ns#\" href=\"$morePermissionsURL\" rel=\"cc:morePermissions\">$morePermissionsURL</a>"
376 #End sentence
377 OUTPUT=$OUTPUT"."
378 fi
379
380
381
382 #====Deliver output====
383 # Send OUTPUT to stdout.
384 echo $OUTPUT