| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
| 4 | # Deduplicate files in a given destdir |
| 5 | # |
| 6 | |
| 7 | err() { |
| 8 | echo "ERROR: $*" |
| 9 | exit 1 |
| 10 | } |
| 11 | |
| 12 | verbose=: |
| 13 | destdir= |
| 14 | while test $# -gt 0; do |
| 15 | case $1 in |
| 16 | -v | --verbose) |
| 17 | # shellcheck disable=SC2209 |
| 18 | verbose=echo |
| 19 | shift |
| 20 | ;; |
| 21 | *) |
| 22 | if test -n "$destdir"; then |
| 23 | err "unknown command-line options: $*" |
| 24 | fi |
| 25 | |
| 26 | destdir="$1" |
| 27 | shift |
| 28 | ;; |
| 29 | esac |
| 30 | done |
| 31 | |
| 32 | if test -z "$destdir"; then |
| 33 | err "destination directory was not specified." |
| 34 | fi |
| 35 | |
| 36 | if ! test -d "$destdir"; then |
| 37 | err "provided directory does not exit." |
| 38 | fi |
| 39 | |
| 40 | if ! command -v rdfind >/dev/null; then |
| 41 | err "rdfind is not installed." |
| 42 | fi |
| 43 | |
| 44 | $verbose "Finding duplicate files" |
| 45 | rdfind -makesymlinks true -makeresultsfile true "$destdir" >/dev/null |
| 46 | |
| 47 | grep DUPTYPE_WITHIN_SAME_TREE results.txt | grep -o "$destdir.*" | while read -r l; do |
| 48 | target="$(realpath "$l")" |
| 49 | $verbose "Correcting path for $l" |
| 50 | ln --force --symbolic --relative "$target" "$l" |
| 51 | done |
| 52 | |
| 53 | rm results.txt |