2018-10-25 15:23:16 +00:00
|
|
|
#!/bin/sh
|
2017-06-16 07:24:54 +00:00
|
|
|
#
|
2019-02-14 12:57:10 +00:00
|
|
|
# Copyright (C) 2017-2019 Hamish Coleman
|
2018-11-30 01:27:50 +00:00
|
|
|
#
|
2017-06-16 07:24:54 +00:00
|
|
|
# Look up the given filename in the description file and confirm that
|
|
|
|
# the checksum matches
|
|
|
|
#
|
|
|
|
# FIXME
|
|
|
|
# - the description file is expected to be in the current dir
|
2017-07-03 06:12:42 +00:00
|
|
|
# - does not handle description file entries with more than just the sha1 tag
|
2017-06-16 07:24:54 +00:00
|
|
|
|
2017-06-26 22:10:50 +00:00
|
|
|
DESCRIPTIONS='Descriptions.txt'
|
2017-06-16 07:24:54 +00:00
|
|
|
|
2018-10-25 15:23:16 +00:00
|
|
|
if [ "$1" = "--mv_on_fail" ]; then
|
2018-02-15 03:19:39 +00:00
|
|
|
MV_ON_FAIL=yes
|
|
|
|
shift
|
2018-10-25 15:23:16 +00:00
|
|
|
elif [ "$1" = "--rm_on_fail" ]; then
|
2017-06-16 07:24:54 +00:00
|
|
|
RM_ON_FAIL=yes
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
FILE="$1"
|
2017-06-30 03:35:33 +00:00
|
|
|
if [ -z "$FILE" ]; then
|
2017-06-26 22:10:50 +00:00
|
|
|
echo "Need filename" 2>&1
|
2017-06-16 07:24:54 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2017-06-30 03:35:33 +00:00
|
|
|
shift
|
2017-06-16 07:24:54 +00:00
|
|
|
|
2017-06-30 03:36:36 +00:00
|
|
|
# The name of the Descriptions.txt entry to use
|
|
|
|
CHECKNAME="$1"
|
|
|
|
if [ -z "$CHECKNAME" ]; then
|
|
|
|
# default to the same as the filename
|
|
|
|
CHECKNAME="$FILE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
do_exit_fail() {
|
2018-02-15 03:19:39 +00:00
|
|
|
if [ "$MV_ON_FAIL" ]; then
|
|
|
|
BADNAME="${FILE}.bad"
|
|
|
|
echo "ERROR: moved file to $BADNAME" 2>&1
|
|
|
|
mv -f "$FILE" "$BADNAME"
|
|
|
|
elif [ "$RM_ON_FAIL" ]; then
|
|
|
|
echo "ERROR: removed file" 2>&1
|
2017-06-30 03:36:36 +00:00
|
|
|
rm -f "$FILE"
|
|
|
|
fi
|
|
|
|
exit 1
|
|
|
|
}
|
2017-06-30 03:04:48 +00:00
|
|
|
|
2018-10-25 15:23:16 +00:00
|
|
|
if ! LINE=$(grep -E "^$CHECKNAME " "$DESCRIPTIONS"); then
|
2017-06-30 03:36:36 +00:00
|
|
|
echo "Unknown file $CHECKNAME" 2>&1
|
|
|
|
do_exit_fail
|
2017-06-16 07:24:54 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -e "$FILE" ]; then
|
2017-06-26 22:10:50 +00:00
|
|
|
echo "File $FILE does not exist" 2>&1
|
2017-06-30 03:36:36 +00:00
|
|
|
do_exit_fail
|
2017-06-16 07:24:54 +00:00
|
|
|
fi
|
|
|
|
|
2017-07-02 09:20:58 +00:00
|
|
|
SUM_FIELD=$(echo "$LINE" | awk -- '{print $2}')
|
|
|
|
|
|
|
|
TYPE=$(echo "$SUM_FIELD" | cut -d: -f1)
|
2018-10-25 15:23:16 +00:00
|
|
|
EXPECT=$(echo "$SUM_FIELD" | cut -d: -f2 |tr '[:upper:]' '[:lower:]')
|
2017-07-02 09:20:58 +00:00
|
|
|
|
|
|
|
case "$TYPE" in
|
|
|
|
sha1)
|
|
|
|
GOT=$(sha1sum "$FILE" | cut -d" " -f1)
|
|
|
|
;;
|
2017-09-05 02:57:18 +00:00
|
|
|
sha256)
|
|
|
|
GOT=$(sha256sum "$FILE" | cut -d" " -f1)
|
|
|
|
;;
|
2017-07-02 09:20:58 +00:00
|
|
|
*)
|
|
|
|
echo "Unknown checksum type $TYPE for $CHECKNAME" 2>&1
|
|
|
|
do_exit_fail
|
|
|
|
;;
|
|
|
|
esac
|
2017-06-16 07:24:54 +00:00
|
|
|
|
|
|
|
if [ "$EXPECT" != "$GOT" ]; then
|
2017-06-26 22:10:50 +00:00
|
|
|
echo "File $FILE failed checksum check" 2>&1
|
2017-06-16 07:24:54 +00:00
|
|
|
|
2017-06-30 03:36:36 +00:00
|
|
|
do_exit_fail
|
2017-06-16 07:24:54 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|