feat(user/triml.sh):Defronts shared filename prefixes
[BK-2020-03.git] / user / bkmml
1 #!/bin/bash
2 # Desc: Make music (sym)links via find and grep
3 # Usage: bkmml [str arg1] [str arg2] [str arg3]
4 # Input: arg1 find iname expression
5 # arg2 grep -Ei expression
6 # arg3 grep -Eiv expression
7 # Version: 0.0.3
8 # Depends: GNU Grep 3.7; GNU find utils 4.8.0; GNU Parallel 20210822
9
10 # Load env vars
11 ## Search this directory.
12 if [[ ! -v BKMML_TARGET_DIR ]]; then BKMML_TARGET_DIR="$HOME"; fi;
13
14 FIND_SEARCH="$1";
15 GREP_EXP="$2";
16 GREP_VEXP="$3";
17 DIR_OUT="./links_music_results";
18
19 # Check input
20 if [[ $# -eq 0 ]]; then echo "FATAL:Insufficient arguments." 1>&2; exit 1; fi;
21 if [[ -z "$GREP_EXP" ]]; then GREP_EXP=".+"; fi; # passthrough grep -Ei
22 if [[ -z "$GREP_VEXP" ]]; then GREP_VEXP="^$"; fi; # passthrough grep -Eiv
23
24 # Check env
25 if ! command -v parallel &>/dev/random; then alias parallel='xargs'; fi;
26
27 # Check output dir
28 if [[ ! -d "$DIR_OUT" ]]; then mkdir -p "$DIR_OUT"; fi;
29
30 n=0;
31 while read -r line; do
32 if [[ -z "$line" ]]; then echo "FATAL:No results." 1>&2; exit 1; fi;
33 if [[ ! -f "$line" ]]; then echo "ERROR:Cannot read file path:$(declare -p line)"; continue; fi;
34 id="$(b2sum -l64 "$line" | awk '{print $1}')";
35 fn="$(basename "$line")";
36 ln -sfn "$line" ./"$DIR_OUT/$id..$fn";
37 ((n++));
38 done < <(find -L "$(readlink -f "$BKMML_TARGET_DIR")" \
39 -maxdepth 10 -type f -size +100000c \
40 \( -iname "*.mp3" \
41 -o -iname "*.flac" \
42 -o -iname "*.m4a" \
43 -o -iname "*.aac" \
44 -o -iname "*.opus" \
45 -o -iname "*.wav" \
46 -o -iname "*.aif" \
47 -o -iname "*.ape" \
48 -o -iname "*.ogg" \) \
49 -a -iname "*$FIND_SEARCH*" \
50 2>/dev/random | \
51 grep -Ei "$GREP_EXP" | \
52 grep -Eiv "$GREP_VEXP" | \
53 parallel readlink -f "{}" | \
54 parallel b2sum "{}" | sort | uniq -w 128 | awk '{$1=""; print substr($0,2)}'
55 );
56
57 # Author: Steven Baltakatei Sandoval
58 # License: GPLv3+