#!/usr/bin/bash

# This script tries to recover damaged files using ddrescue
# Created: 2019.04.29 07:20
# Author: Kārlis Kalviškis <eko@lanet.lv>
# License: GPLv3

# For translation
export TEXTDOMAIN="karlo-scripts"
. gettext.sh

Usage (){
	echo
	echo `eval_gettext 'Missing parameters'`!
	echo
	echo `eval_gettext 'Usage'`:
	echo "$0" `eval_gettext 'source_directory'` `eval_gettext 'destination_directory'`  [`eval_gettext 'map_directory'` [--run-again]]
	echo --run-again ' ' `eval_gettext 'Continue the stopped data rescue'` 
}

if [[ $# -lt 2 ]]; then
	Usage; exit
fi

echo
echo `eval_gettext 'This script tries to recover damaged files using'` ddrescue.
type -P ddrescue &>/dev/null || { echo  "`eval_gettext 'Command'` 'ddrescue' `eval_gettext 'is missing'`"; exit 10; }

WhereAreFiles=$1
SafePlace=$2

# Relative  are absolute path?
WhereAmI=$PWD
if [[ ${WhereAreFiles:0:1} != '/' ]]; then
	WhereAreFiles=$WhereAmI/$WhereAreFiles
fi
if [[ ${SafePlace:0:1} != '/' ]]; then
	SafePlace=$WhereAmI/$SafePlace
fi
echo `eval_gettext 'Source directory'`: $WhereAreFiles.
echo `eval_gettext 'Destination directory'`: $SafePlace.

if [[ $# -ge 3 ]]; then
	MapFiles="$3"
	CONTINUE=$4
	if [[ ${MapFiles:0:1} != '/' ]]; then
		MapFiles=$WhereAmI/$MapFiles
	fi
	echo `eval_gettext 'Directory for map files'`: $MapFiles.
else
	echo `eval_gettext 'No map file will be used'` \(`eval_gettext 'Not recomended'`!\)
fi

echo
echo -e "\033[1;36m"
while true; do
	read -p "`eval_gettext 'Do you wish to copy the files'`? " yn
	case $yn in
		[YyJj]* ) break;;
		[Nn]* ) exit;;
		* ) echo `eval_gettext 'Please answer yes or no'`.;;
	esac
done
echo -e "\033[0;0m"

if [[ ! -d $WhereAreFiles ]]; then
	echo -e "\033[1;95m"
	echo "`eval_gettext 'Source directory'` '$WhereAreFiles' `eval_gettext 'does not exists'`!"
	echo -e "\033[0;0m"
	Usage; exit 2
fi

if [[ -d $SafePlace ]]; then
	if [[ $CONTINUE != '--run-again' ]]; then
		echo -e "\033[1;95m"
		echo "`eval_gettext 'Destination already exists'`!!!"
		echo "`eval_gettext 'Files will be overwritten'`!"
		echo -e "\033[0;0m"
		while true; do
			read -p "`eval_gettext 'Do you wish to copy the files'`? " yn
			case $yn in
				[YyJj]* ) break;;
				[Nn]* ) exit;;
				* ) echo `eval_gettext 'Please answer yes or no'`.;;
			esac
		done
	else
		if [[ ! -d $MapFiles ]]; then
			echo -e "\033[1;95m"
			echo "`eval_gettext 'Directory for map files'` '$MapFiles' `eval_gettext 'does not exists'`!"
			echo "`eval_gettext 'The interrupted rescue task can not be found'`!"
			echo -e "\033[0;0m"
			exit 5
		fi
	fi
else
	if [[ $CONTINUE == '--run-again' ]]; then
		echo -e "\033[1;95m"
		echo "`eval_gettext 'Destination directory'` '$SafePlace' `eval_gettext 'does not exists'`!"
		echo "`eval_gettext 'The interrupted rescue task can not be found'`!"
		echo -e "\033[0;0m"
		exit 5
	fi
fi

# Moves to the directory with corrupted files
cd "$WhereAreFiles"
if [[ $? -gt 0 ]]; then
	exit 2
fi

if [[ -z $CONTINUE ]]; then
	echo -e "\033[1;33m"
	echo `eval_gettext 'Creates the original directory structure'`
	echo -e "\033[0;0m"

	# Creates the directory for rescued files
	mkdir -p $SafePlace 
	if [[ $? -gt 0 ]]; then
		exit 3
	fi

	# Creates the directory for map files
	if [[ -n $MapFiles ]]; then
		mkdir -p $MapFiles 
		if [[ $? -gt 0 ]]; then
			exit 3
		fi
	fi

	# Creates the original directory structure
	if [[ -n $MapFiles ]]; then
		find . -type d -exec mkdir -p $SafePlace/{}  \; -exec mkdir -p $MapFiles/{}  \;
	else
		find . -type d -exec mkdir -p $SafePlace/{}  \; 
	fi
	if [[ $? -gt 0 ]]; then
		exit 4
	fi
fi

# Finds all files and tries to rescue them
if [[ -n $MapFiles ]]; then
	find . -type f \
	-exec echo -e "\033[1;32m" \; \
	-exec echo {} \; \
	-exec echo -e "\033[0;0m" \; \
	-exec ddrescue --no-scrape --no-trim {} $SafePlace/{} $MapFiles/{}.map \;
	find . -type f \
	-exec echo -e "\033[1;32m" \; \
	-exec echo {} \; \
	-exec echo -e "\033[0;0m" \; \
	-exec ddrescue {} $SafePlace/{} $MapFiles/{}.map \; 
	find . -type f \
	-exec echo -e "\033[1;32m" \; \
	-exec echo {} \; \
	-exec echo -e "\033[0;0m" \; \
	-exec ddrescue --retrim {} $SafePlace/{} $MapFiles/{}.map \; 
else
	find . -type f \
	-exec echo -e "\033[1;32m" \; \
	-exec echo {} \; \
	-exec echo -e "\033[0;0m" \; \
	-exec ddrescue {} $SafePlace/{} \; 
fi

cd $WhereAmI
