#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/04-RAW/
# OR
#   /home/*/.gnome2/nautilus-sctipts/04-RAW/ (deprecated)
#
# FILE:
#   03-md5-and-sha1
#
# USAGE:
#   Right klick on a RAW image (.dd) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Calculates MD5 and SHA1 hash values from an image (or other file) in one pass.
#
# REQUIREMENTS:
#   bash, zenity, coreutils and openssl
#
# BUGS:
#   ---
#
# NOTES:
#   Tested on
#   - Debian 8+
#   - Arch Linux
#
# AUTHOR:
#   Patrick Neumann, patrick@neumannsland.de
#
# COMPANY:
#   (privately)
#
# VERSION:
#   0.9 (beta)
#
# LINK TO THE MOST CURRENT VERSIONS:
#   https://
#
# CREATED:
#   02.04.2016
#
# COPYRIGHT (C):
#   2015-2020 - Patrick Neumann
#
# LICENSE:
#   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.
#
# WARRANTY:
#   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 <http://www.gnu.org/licenses/>.
#
# TODO:
#   ---
#
# HISTORY:
#   0.9 - Patrick Neumann - Initial (public) release
#
#===============================================================================

#-------------------------------------------------------------------------------
# Additional supported Distribution(s) (add before Library!).
#-------------------------------------------------------------------------------
SUPPORTED_OSR="arch"

#-------------------------------------------------------------------------------
# Check for library (casualscripter_nautilus-scripts_functions.sh).
#-------------------------------------------------------------------------------
readonly LIBRARY="${0%/*/*}/.casualscripter_nautilus-scripts_functions.sh"
if [ ! -f "${LIBRARY}" ] ; then
  zenity --error \
         --text \
         "ERROR: casualscripter_nautilus-scripts_functions.sh MISSING!"
  exit 1
fi

source "${LIBRARY}"

#-------------------------------------------------------------------------------
# Checks (see library "casualscripter_nautilus-scripts_functions.sh").
#-------------------------------------------------------------------------------
check_dep "${DC3DD_BIN}" "dc3dd"
check_dep "${TEE_BIN}" "coreutils"
check_dep "${OPENSSL_BIN}" "openssl"
check_dep "${CAT_BIN}" "coreutils"
check_dep "${RM_BIN}" "coreutils"
check_dep "${SED_BIN}" "sed"
check_dep "${BASENAME_BIN}" "coreutils"

# If we do not want to calculate digests for every type of file...
#check_ext "${SOURCE}" "dd|DD|raw|RAW|img|IMG"

check_tmp

#-------------------------------------------------------------------------------
# Generate file for md5 and sha1 checksums, if necessary.
#-------------------------------------------------------------------------------
readonly MD5_SHA1="${TMP}/md5_and_sha1.txt"

if [ ! -f "${MD5_SHA1}" ] ; then
  echo "MD5:" > "${MD5_SHA1}"
  echo -e "\nSHA1:" > "/tmp/openssl_sha1.tmp"
  ${GTERMINAL_BIN} --hide-menubar -- \
    ${BASH_BIN} -c \
      "${DC3DD_BIN} if='${SOURCE}' \
      | ${TEE_BIN} >( ${OPENSSL_BIN} dgst -md5 -r >> '${MD5_SHA1}' ) \
      >( ${OPENSSL_BIN} dgst -sha1 -r >> '/tmp/openssl_sha1.tmp' ) \
      > /dev/null ; sleep .1"

  ${SLEEP_BIN} 3

  while ${PGREP_BIN} --full "${DC3DD_BIN}" > /dev/null 2>&1; do
    ${SLEEP_BIN} 1
  done

  ${CAT_BIN} "/tmp/openssl_sha1.tmp" >> "${MD5_SHA1}"
  ${RM_BIN} "/tmp/openssl_sha1.tmp"
  ${SED_BIN} --in-place "s/*stdin/*$( ${BASENAME_BIN} "${SOURCE}" )/" "${MD5_SHA1}"
fi

#-------------------------------------------------------------------------------
# Display content of the resultfile "md5_and_sha1.txt".
#-------------------------------------------------------------------------------
display_resultfile "${MD5_SHA1}"

exit 0