2019-12-27 11:13:25 -05:00
|
|
|
#!/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-28 10:09:19 -05:00
|
|
|
YADM_DIRECTORY=$(dirname "$YADM_HOOK_REPO")
|
2019-12-27 11:13:25 -05:00
|
|
|
YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums
|
|
|
|
|
2019-12-28 10:09:19 -05: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 16:35:55 -05:00
|
|
|
WARNING_MESSAGE="No checksums were created"
|
|
|
|
|
|
|
|
|
|
|
|
function get_checksum_command {
|
|
|
|
# check if "shasum" exists and supports the algorithm (which is
|
|
|
|
# tested by sending an empty string to "shasum")
|
|
|
|
if command -v "shasum" > /dev/null && echo -n | shasum --algorithm "256" &> /dev/null; then
|
|
|
|
echo "shasum --algorithm 256"
|
|
|
|
# check if "sha256sum" exists
|
|
|
|
elif command -v "sha256sum" > /dev/null; then
|
|
|
|
echo "sha256sum"
|
|
|
|
# check if "gsha256sum" exists
|
|
|
|
elif command -v "gsha256sum" > /dev/null; then
|
|
|
|
echo "gsha256sum"
|
|
|
|
else
|
|
|
|
# display warning in bright yellow
|
|
|
|
echo -e "\033[1;33m" >&2
|
|
|
|
echo -n "WARNING: \"shasum\", \"sha256sum\" and \"gsha256sum\" not found. $WARNING_MESSAGE." >&2
|
|
|
|
|
|
|
|
# reset output color
|
|
|
|
echo -e "\033[0m" >&2
|
|
|
|
|
|
|
|
# signal error
|
|
|
|
return 1
|
2019-12-27 17:25:45 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-28 16:35:55 -05:00
|
|
|
# get checksum command
|
|
|
|
CHECKSUM_COMMAND=$(get_checksum_command)
|
|
|
|
ERROR_CODE=$?
|
2019-12-27 11:13:25 -05:00
|
|
|
|
2019-12-28 16:35:55 -05:00
|
|
|
# no command found
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# return original exit status of yadm command
|
|
|
|
exit "$YADM_HOOK_EXIT"
|
|
|
|
fi
|
2019-12-27 11:13:25 -05:00
|
|
|
|
2019-12-28 16:35:55 -05:00
|
|
|
# empty (or create) checksum file
|
2019-12-28 10:09:19 -05:00
|
|
|
echo -n > "$YADM_CHECKSUMS"
|
2019-12-27 11:13:25 -05:00
|
|
|
|
2019-12-27 17:25:45 -05:00
|
|
|
# calculate checksums for encrypted files
|
2019-12-28 10:09:19 -05:00
|
|
|
for included in ${YADM_ENCRYPT_INCLUDE_FILES[*]}; do
|
2019-12-28 16:35:55 -05:00
|
|
|
# highlight any errors in red
|
|
|
|
echo -en "\033[0;31m"
|
|
|
|
|
|
|
|
# calculate checksums
|
|
|
|
$CHECKSUM_COMMAND "$included" >> "$YADM_CHECKSUMS"
|
|
|
|
ERROR_CODE=$?
|
2019-12-27 17:25:45 -05:00
|
|
|
|
2019-12-28 16:35:55 -05:00
|
|
|
# reset output color
|
|
|
|
echo -ne "\033[0m"
|
|
|
|
|
|
|
|
# handle errors
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# display warning in bright yellow
|
|
|
|
echo -e "\033[1;33m" >&2
|
|
|
|
echo -n "WARNING: an error occurred. Please inspect the checksum file." >&2
|
|
|
|
|
|
|
|
# reset output color
|
|
|
|
echo -e "\033[0m" >&2
|
|
|
|
|
|
|
|
# exit and signal error
|
|
|
|
exit $ERROR_CODE
|
2019-12-27 17:25:45 -05:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2019-12-28 16:35:55 -05:00
|
|
|
# announce success and return original exit status of yadm command
|
|
|
|
echo "Wrote SHA-256 checksums: $YADM_CHECKSUMS"
|
2019-12-28 10:09:19 -05:00
|
|
|
exit "$YADM_HOOK_EXIT"
|