| 
									
										
										
										
											2019-12-27 16:13:25 +00:00
										 |  |  | #!/usr/bin/env bash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # yadm - Yet Another Dotfiles Manager | 
					
						
							| 
									
										
										
										
											2021-01-07 15:24:11 +00:00
										 |  |  | # Copyright (C) 2015-2021 Tim Byrne and Martin Zuther | 
					
						
							| 
									
										
										
										
											2019-12-27 16:13:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | # it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | # the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | # (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 11:51:29 +00:00
										 |  |  | YADM_CHECKSUMS="$YADM_HOOK_DIR/files.checksums" | 
					
						
							|  |  |  | WARNING_MESSAGE="Checksums were not verified" | 
					
						
							| 
									
										
										
										
											2019-12-27 16:13:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | # unpack exported array; filenames including a newline character (\n) | 
					
						
							|  |  |  | # are NOT supported | 
					
						
							|  |  |  | OLD_IFS="$IFS" | 
					
						
							|  |  |  | IFS=$'\n' | 
					
						
							|  |  |  | YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES ) | 
					
						
							|  |  |  | IFS="$OLD_IFS" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function get_checksum_command { | 
					
						
							|  |  |  |     # check if "shasum" exists and supports the algorithm (which is | 
					
						
							|  |  |  |     # tested by sending an empty string to "shasum") | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |     if command -v "shasum" > /dev/null && printf "" | shasum --algorithm "256" &> /dev/null; then | 
					
						
							|  |  |  |         printf "shasum --algorithm 256" | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     # check if "sha256sum" exists | 
					
						
							|  |  |  |     elif command -v "sha256sum" > /dev/null; then | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |         printf "sha256sum" | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     # check if "gsha256sum" exists | 
					
						
							|  |  |  |     elif command -v "gsha256sum" > /dev/null; then | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |         printf "gsha256sum" | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |         # display warning in bright yellow | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |         printf "\033[1;33m" >&2 | 
					
						
							|  |  |  |         printf "\nWARNING: \"shasum\", \"sha256sum\" and \"gsha256sum\" not found.   %s\n" "$WARNING_MESSAGE." >&2 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # reset output color | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |         printf "\033[0m" >&2 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # signal error | 
					
						
							|  |  |  |         return 1 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | # if there is no checksum file, exit with original status of yadm | 
					
						
							|  |  |  | # command | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | if [ ! -f "$YADM_CHECKSUMS" ]; then | 
					
						
							|  |  |  |     exit "$YADM_HOOK_EXIT" | 
					
						
							| 
									
										
										
										
											2019-12-27 16:13:25 +00:00
										 |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | # get checksum command | 
					
						
							|  |  |  | CHECKSUM_COMMAND=$(get_checksum_command) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # no command found | 
					
						
							| 
									
										
										
										
											2019-12-29 23:11:36 +00:00
										 |  |  | if (($?)); then | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     # return original exit status of yadm command | 
					
						
							|  |  |  |     exit "$YADM_HOOK_EXIT" | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | # check encrypted files for differences and capture output and error | 
					
						
							|  |  |  | # messages | 
					
						
							|  |  |  | YADM_CHECKSUM_OUTPUT=$($CHECKSUM_COMMAND --check "$YADM_CHECKSUMS" 2>&1) | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  | ERROR_CODE=$? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  | # handle mismatched checksums and errors | 
					
						
							| 
									
										
										
										
											2019-12-29 23:11:36 +00:00
										 |  |  | if (($ERROR_CODE)); then | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |     printf "\nSome SHA-256 sums do not match (or an error occurred):\n\n" | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     # display differing files and errors (highlighted in red) | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |     printf "\033[0;31m" | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     while IFS= read -r line; do | 
					
						
							| 
									
										
										
										
											2019-12-29 14:53:01 +00:00
										 |  |  |         # beautify output and get rid of unnecessary lines | 
					
						
							|  |  |  |         line="${line%%*: [Oo][Kk]}" | 
					
						
							|  |  |  |         line="${line%%: [Ff][Aa][Ii][Ll][Ee][Dd]}" | 
					
						
							|  |  |  |         line="${line##*WARNING:*did NOT match}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if [ -n "$line" ]; then | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |             printf "%s\n" "$line" | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  |         fi | 
					
						
							|  |  |  |     done <<< "$YADM_CHECKSUM_OUTPUT" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # reset output color | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |     printf "\033[0m" | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     # display advice for differing files and signal error | 
					
						
							| 
									
										
										
										
											2019-12-29 23:43:24 +00:00
										 |  |  |     printf "\nConsider running either \"yadm encrypt\" or \"yadm decrypt\".\n" | 
					
						
							| 
									
										
										
										
											2019-12-28 21:35:55 +00:00
										 |  |  |     exit $ERROR_CODE | 
					
						
							| 
									
										
										
										
											2019-12-27 22:25:45 +00:00
										 |  |  | fi |