1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2024-10-27 20:34:27 +00:00
TheLocehiliosan_yadm/contrib/hooks/post_encrypt

97 lines
2.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# yadm - Yet Another Dotfiles Manager
# Copyright (C) 2015-2019 Tim Byrne and Martin Zuther
# 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="No checksums were created"
# 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"
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"
# check if "sha256sum" exists
elif command -v "sha256sum" > /dev/null; then
2019-12-29 23:43:24 +00:00
printf "sha256sum"
# check if "gsha256sum" exists
elif command -v "gsha256sum" > /dev/null; then
2019-12-29 23:43:24 +00:00
printf "gsha256sum"
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
# reset output color
2019-12-29 23:43:24 +00:00
printf "\033[0m" >&2
# signal error
return 1
fi
}
# get checksum command
CHECKSUM_COMMAND=$(get_checksum_command)
# no command found
2019-12-29 23:11:36 +00:00
if (($?)); then
# return original exit status of yadm command
exit "$YADM_HOOK_EXIT"
fi
# empty (or create) checksum file
2019-12-29 23:43:24 +00:00
true > "$YADM_CHECKSUMS"
# calculate checksums for encrypted files
2019-12-29 23:11:36 +00:00
for included in "${YADM_ENCRYPT_INCLUDE_FILES[@]}"; do
# highlight any errors in red
2019-12-29 23:43:24 +00:00
printf "\033[0;31m"
# calculate checksums
$CHECKSUM_COMMAND "$included" >> "$YADM_CHECKSUMS"
ERROR_CODE=$?
# reset output color
2019-12-29 23:43:24 +00:00
printf "\033[0m"
# handle errors
2019-12-29 23:11:36 +00:00
if (($ERROR_CODE)); then
# display warning in bright yellow
2019-12-29 23:43:24 +00:00
printf "\033[1;33m" >&2
printf "\nWARNING: an error occurred. Please inspect the checksum file.\n" >&2
# reset output color
2019-12-29 23:43:24 +00:00
printf "\033[0m" >&2
# exit and signal error
exit $ERROR_CODE
fi
done
# announce success and return original exit status of yadm command
2019-12-29 23:43:24 +00:00
printf "Wrote SHA-256 checksums: %s\n" "$YADM_CHECKSUMS"
exit "$YADM_HOOK_EXIT"