#!/bin/bash
# This script is meant to find duplicate files in a directory, same file names,
# same MD5 hash of extracted content.

set -o pipefail

session="${RANDOM}"
home_dir='/home/lucifer'
tmp="/${home_dir}/compare_dirs-${session}"
declare -A md5_bn identical

empty='d41d8cd98f00b204e9800998ecf8427e'

if [[ $(whoami) != 'root' ]]; then
	echo -e "You need to be root to run this script!\n"
	exit
fi

declare -a library

for arg in "$@"; do
	if [[ -d $arg ]]; then
		library+=( "$(readlink -f "$arg")" )
	fi
done

if [[ -z ${library[0]} ]]; then
	echo -e "Usage: $(basename "$0") [dirs...]\n"
	exit
fi

mapfile -t files < <(find "${library[@]}" -type f -iname "*.7z")

for (( i = 0; i < ${#files[@]}; i++ )); do
	bn=$(basename "${files[${i}]}")
	bn_lc=$(tr '[:upper:]' '[:lower:]' <<<"$bn" | sed -E 's/[^ a-zA-Z ]//g')

	md5=$(md5sum -b <<<"${bn_lc:0:4}" | cut -d' ' -f1)

	md5_bn[${md5}]+="${files[${i}]}\n"
done

for key in "${!md5_bn[@]}"; do
	lines=$(echo -e "${md5_bn[${key}]}" | wc -l | tr -d '[:blank:]')

	if [[ $lines -gt 2 ]]; then
		unset -v dirs
		declare -a dirs

		mapfile -t files < <(echo -e "${md5_bn[${key}]}")

		for (( i = 0; i < ${#files[@]}; i++ )); do
			if [[ ! -f ${files[${i}]} ]]; then
				continue
			fi

			dn="${tmp}/${key}-${RANDOM}-${RANDOM}"

			dirs+=("$dn")

			if [[ ${#dirs[@]} -ne 0 ]]; then
				element=$(( ${#dirs[@]} - 1 ))
			fi

			mkdir -p "$dn"
			cd "$dn"

			echo "${files[${i}]}"
			${home_dir}/bin/arch_unpack.sh "${files[${i}]}"
			${home_dir}/bin/md5db_fast.pl -index "$dn" &>/dev/null

			md5_2=$(md5sum -b "${dn}/md5.db" | cut -d' ' -f1)

			if [[ -z $md5_2 || $md5_2 == $empty ]]; then
				continue
			fi

			if [[ ${#dirs[@]} -gt 1 ]]; then
				for (( j = 0; j < ${#dirs[@]}; j++ )); do
					if [[ $j -eq $element ]]; then
						continue
					fi

					dn_if="${dirs[${j}]}"

					md5_1=$(md5sum -b "${dn_if}/md5.db" | cut -d' ' -f1)

					if [[ $md5_1 == $md5_2 ]]; then
						identical[${key}]+="${files[${i}]}\n"
						rm "${files[${i}]}"
						break
					fi
				done
			fi
		done

		rm -rf "${tmp}"/*
	fi
done

if [[ -d $tmp ]]; then
	rm -rf "$tmp"
fi

for key in "${!identical[@]}"; do
	mapfile -t files < <(echo -e "${md5_bn[${key}]}")
	echo "*** ${key}"
	echo -e "${identical[${key}]}"
done
