summaryrefslogtreecommitdiff
path: root/iedupes.sh
blob: f70e5933d1093e2cdb60c7bcf7fa3fe0b118bfcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash


# trap ctrl-c and call ctrl_c()

function ctrl_c() {
        echo "ABORTING DUPE REVIEW"
        cat "$exceptionsfile" | (read -r; printf "%s\n" "$REPLY"; sort -u) > "${sorted_exceptionsfile}"
        mv -f "${sorted_exceptionsfile}" "$exceptionsfile"
        [[ -f "${optimized_exceptionsfile}" ]] && rm -f "${optimized_exceptionsfile}"
        exit -1
}

trap ctrl_c SIGINT SIGTERM


IE="$(pwd)"
exceptionsfile="${IE}/exceptions.txt"
optimized_exceptionsfile=""
sorted_exceptionsfile="$( mktemp /tmp/sorted_exceptions_XXXXXXXX.txt )"

optimization_period="7"
optimized_curr="$(( $(date +"%s") / 86400 ))"
skip_flag=false
DEFAULT_SIMILARITY="Small"

while getopts "hy" opt; do
    case $opt in
    h) echo -e "usage: $0 [-h help] [-y automatically add exceptions]";  exit ;;
    y) skip_flag=true ;;
    ?) echo "error: option -$OPTARG is not implemented"; exit ;;
    esac
done

( [ -e "$exceptionsfile" ] || (touch "$exceptionsfile" && echo "##### LAST TIMESTAMP OF EXCEPTION FILE OPTIMIZATION (IN DAYS SINCE UNIX EPOCH): ${optimized_curr} #####" > "$exceptionsfile") ) && [ ! -w "$exceptionsfile" ] && echo "ERROR: Cannot write to $exceptionsfile" && exit 1

optimized_last="$(cat "$exceptionsfile" | head -n 1 | grep -E "##### LAST TIMESTAMP OF EXCEPTION FILE.*#####" | grep -oE "[0-9]+")"
[ -z "$optimized_last" ]  && echo "ERROR: Cannot parse last optimization date of $exceptionsfile" && exit 1

[[ -n $(find "$(pwd)" -regextype awk -iregex ".*[\[\]\(\)\{\}\*\+\?]+.*") ]] && echo -e "REGEX SYMBOLS FOUND IN FILENAMES, IE IN SHAMBLES. ABORTING.\n" && exit -1

function optimize_exceptions {

    optimized_exceptionsfile="$( mktemp /tmp/optimized_exceptions_XXXXXXXX.txt )"

    optimized_date_rel=$(( optimized_curr - optimized_last ))
    [[ $optimized_date_rel -ge $optimization_period ]] || return 1

    echo "##### LAST TIMESTAMP OF EXCEPTION FILE OPTIMIZATION (IN DAYS SINCE UNIX EPOCH): ${optimized_curr} #####" > "${optimized_exceptionsfile}"

    while read line; do
        lineexpr=$(echo -e "$line" | sed -r "s| \/|\.*\/|g; s|^|\.*|g; s|$|\.*|g")
        [[ -n $( grep -E "##### LAST TIMESTAMP OF EXCEPTION FILE.*#####" <<< "$line" ) || $(grep -a "$lineexpr" exceptions.txt | wc -l) -ge 2 ]] && continue || echo "$line" >> "${optimized_exceptionsfile}"
    done < "$exceptionsfile"

    exceptions_lines=$(wc -l < "$exceptionsfile")
    optimized_exceptions_lines=$(wc -l < "${optimized_exceptionsfile}")
    optimization_diff=$(( exceptions_lines - optimized_exceptions_lines ))
    [[ $optimization_diff -gt 0 ]] || return 1

    while true; do
        read -p "Exceptions file (${exceptionsfile}) has been optimized (${optimization_diff} new optimizations, ${optimized_date_rel} days since last optimization). Would you like to overwrite the existing exceptions file (y/n)? " yn

        case $( tr '[A-Z]' '[a-z]' <<< "$yn" ) in
            y|yes) rm "$exceptionsfile" && cp "${optimized_exceptionsfile}" "$exceptionsfile" && echo "Exceptions file updated." ; break;;
            n|no) echo "Update aborted." ; break;;
            *) echo "Invalid response." ;;
        esac
    done

    rm -f "${optimized_exceptionsfile}"
}

$skip_flag || optimize_exceptions

re='^Minimal|VerySmall|Small|Medium|High|VeryHigh$'

declare SIMILARITY

if $skip_flag ; then
    SIMILARITY="$DEFAULT_SIMILARITY"
else
    while read -p "Enter similarity preset [Default: Small; Allowed: Minimal, VerySmall, Small, Medium, High, VeryHigh]: " SIMILARITY; do

        SIMILARITY=${SIMILARITY:-"$DEFAULT_SIMILARITY"}

        if ! [[ $SIMILARITY =~ $re ]]; then
            echo "error: Invalid input" >&2; continue
        fi

        break
    done
fi


mapfile -t matches < <( czkawka-cli image -d "$IE" -s "$SIMILARITY" | sed -r "s| \- [0-9]*x[0-9]* \- [0-9]*(\.[0-9]*)? [KMGTP]iB \- [a-zA-Z ]*$||g ; s|[0-9]+ images which have similar friends||g ; s|\"||g ; s|^Found ||g ; s|.*-------MESSAGES-------.*||g ; s|.*Properly loaded.*||g ; s|.*Properly saved to file.*||g " \
| head -n -5 | awk -v RS=  '{$1=$1; t=!/\//; if(NR>1 && t) print ""; print; if(t) print ""}' )


count=0

for match in "${matches[@]}"
do
    matches[$count]=$( echo "$match" | sed -r "s|\ \/|\n\/|g" | sort | tr '\n' ' ' | sed -r "s| $|\n|" )
    ((count++))
done


printf '%s\n' "${matches[@]}" | while IFS= read -r line
do
    lineexpr=$(echo -e "$line" | sed -r "s| \/|\.*\/|g; s|^|\.*|g; s|$|\.*|g")
    [[ -n $(grep -a -E "$lineexpr" "$exceptionsfile") ]] && continue || echo "$lineexpr"
    if $skip_flag ; then
        echo "$line" >> "$exceptionsfile"
    else
        echo -e "$line" | sed -r "s|\ \/home\/|\n\/home\/|g" | /bin/feh --info "printf '%S     %wx%h'" --zoom max --scale-down -g 1280x720 -B black -d --action1 "gio trash %F" --action2 "cat %L | sed -r 's|^\.|$(pwd)|g' | sort | tr '\n' ' ' | paste -s -d ' ' | sed 's| $||g' >> \"$exceptionsfile\"" --action3 "$HOME/stuf/scripts/scriptlets/feh_action3.sh %F %L" -f -
    fi
done

cat "$exceptionsfile" | (read -r; printf "%s\n" "$REPLY"; sort -u) > "${sorted_exceptionsfile}"
mv -f "${sorted_exceptionsfile}" "$exceptionsfile"

# EVERYTHING IS HOW IT'S SUPPOSED TO BE, THERE CAN BE NO IMPROVEMENTS, NOTHING ELSE WORKED
# | sed -e "s|\(\/.*\)|\"\1\"|g"
# ; s|\ |\\\ |g

#THIS IS BEST PRACTICE FOR ENVIRONMENT VARIABLES, CONSIDER CORRECTING ~/stuf STUFF
# -t 95% WORKS FOR ELIMINATING THE CHAFF, BUT IT MIGHT MISS SOME

#feh . --action1 "cat %L | sed -r 's|^\.|$(pwd)|g' | sort | tr '\n' ' ' "
#czkawka_cli image -d "/home/jay/Tempsktop" -s "High" | sed -r "s| \- [0-9]*x[0-9]* \- [0-9]*\.[0-9]* [KMGTP]iB \- [a-zA-Z ]*$||g ; s|Found [0-9]+ images which have similar friends||g " | head -n -5 | awk -v RS=  '{$1=$1; t=!/\//; if(NR>1 && t) print ""; print; if(t) print ""}' | xargs -n 1| sort | xargs


# IF IT WORKS WITH QUOTES, ADD QUOTES