2019-12-27 16:13:25 +00: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 15:09:19 +00:00
|
|
|
YADM_DIRECTORY=$(dirname "$YADM_HOOK_REPO")
|
2019-12-27 16:13:25 +00:00
|
|
|
YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums
|
|
|
|
|
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 10:17:52 +00:00
|
|
|
CHECKSUM_ALGORITHM="512"
|
|
|
|
CHECKSUM_ALGORITHM_NAME="SHA-512"
|
2019-12-27 22:25:45 +00:00
|
|
|
WARNING_MESSAGE="No checksums were created."
|
2019-12-27 16:13:25 +00:00
|
|
|
|
2019-12-27 22:25:45 +00:00
|
|
|
|
|
|
|
function print_warning_and_exit {
|
|
|
|
MESSAGE=$1
|
|
|
|
|
|
|
|
# set output color to yellow
|
|
|
|
echo -e "\033[1;33m"
|
|
|
|
echo "WARNING: $MESSAGE $WARNING_MESSAGE"
|
|
|
|
|
|
|
|
# reset output color
|
|
|
|
echo -e "\033[0m"
|
|
|
|
|
2019-12-28 15:09:19 +00:00
|
|
|
exit "$YADM_HOOK_EXIT"
|
2019-12-27 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function ensure_command {
|
|
|
|
COMMAND_NAME=$1
|
|
|
|
|
|
|
|
# check if command exists
|
|
|
|
if ! command -v "$COMMAND_NAME" > /dev/null; then
|
|
|
|
print_warning_and_exit "command \"$COMMAND_NAME\" not found."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function ensure_algorithm {
|
2019-12-28 15:09:19 +00:00
|
|
|
# check if "shasum" supports algorithm by hashing an empty string
|
2019-12-27 22:25:45 +00:00
|
|
|
echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null
|
2019-12-27 16:13:25 +00:00
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
2019-12-27 22:25:45 +00:00
|
|
|
print_warning_and_exit "\"shasum\" does not support $CHECKSUM_ALGORITHM_NAME."
|
2019-12-27 16:13:25 +00:00
|
|
|
fi
|
2019-12-27 22:25:45 +00:00
|
|
|
}
|
2019-12-27 16:13:25 +00:00
|
|
|
|
|
|
|
|
2019-12-27 22:25:45 +00:00
|
|
|
# check if "shasum" exists and supports algorithm
|
|
|
|
ensure_command shasum
|
|
|
|
ensure_algorithm
|
2019-12-27 16:13:25 +00:00
|
|
|
|
2019-12-27 22:25:45 +00:00
|
|
|
# empty checksum file
|
2019-12-28 15:09:19 +00:00
|
|
|
echo -n > "$YADM_CHECKSUMS"
|
2019-12-27 16:13:25 +00:00
|
|
|
|
2019-12-27 22:25:45 +00:00
|
|
|
# calculate checksums for encrypted files
|
2019-12-28 15:09:19 +00:00
|
|
|
for included in ${YADM_ENCRYPT_INCLUDE_FILES[*]}; do
|
|
|
|
shasum --algorithm $CHECKSUM_ALGORITHM "$included" >> "$YADM_CHECKSUMS"
|
2019-12-27 22:25:45 +00:00
|
|
|
|
|
|
|
# signal errors
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "Wrote checksums: $YADM_CHECKSUMS ($CHECKSUM_ALGORITHM_NAME)"
|
2019-12-27 16:13:25 +00:00
|
|
|
|
|
|
|
# return exit status of the yadm command
|
2019-12-28 15:09:19 +00:00
|
|
|
exit "$YADM_HOOK_EXIT"
|