summaryrefslogtreecommitdiffstats
path: root/.root/usr/local/bin/group-media
blob: c6d214c8ebcfa99061d73492a5e8854a229c2f0d (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
#!/bin/bash

set -e

DIR="."
INTERACTIVE=0
VERBOSE=0
DO_THING=true

while [ $# -gt 0 ]; do
	case "$1" in 
		-h | --help)
			echo -e "this is a script for grouping songs in 1 directory into subdirectories based on album."
			echo -e "command line options:"
			echo -e "\t-h, --help: print this message"
			echo -e "\t-i, -ii: interactiveness. -i asks whether to create a directory, -ii asks before moving each file"
			echo -e "\t -v, -vv: verboseness. -v prints all albums, -vv prints all moved files"
			echo -e "\t-r, --dry-run: do not change anything, only print what would have been done. useless without verboseness."
			echo -e "\t-d [DIR], --directory [DIR]: working directory for the script"
			echo -e "I AM NOT responsible for anything that might go wrong in this script!"
			echo -e "I did test this on my library and it did a good job, though."
			exit 0
			;;
		-d | --directory)
			if ! test -z "$2"; then
				DIR="$2"
			else
				echo "-d requires a value"
				exit 1
			fi
			shift 2
			;;
		-r | --dry-run)
			DO_THING=false
			shift	
			;;
		-i)
			((++INTERACTIVE))
			shift
			;;
		-ii)
			((INTERACTIVE += 2))
			shift
			;;
		-v)
			((++VERBOSE))
			shift
			;;
		-vv)
			((VERBOSE += 2))
			shift
			;;
		*)
			echo "unknown argument: $1"
			exit 1
			;;
	esac
done

if ! which eyeD3 &> /dev/null; then
	echo "eyeD3 must be available in PATH for this script to work. try installing it throug python-pip (or pipx)"
	exit 1
fi

for i in "$DIR"/*; do
	if test -f "$i"; then
		ALB="$DIR"/$(eyeD3 "$i" | awk '/album:/{$1="";print $0}' | xargs);
		if test -z "$ALB"; then
			echo "song \"$i\" does not have an album."
			continue
		fi
		if test 0 -eq $INTERACTIVE; then
			if $DO_THING; then mkdir -p "$ALB"; fi
			if test 1 -le $VERBOSE; then
				echo "made directory $ALB"
			fi
		else
			if ! test -d "$ALB"; then
				unset cont; read -n 1 -p "mkdir \"$ALB\" ? [Y/n]"
				echo
				if [[ $cont =~ ^[Yy]$ ]] || [ -z $cont ]; then
					if $DO_THING; then mkdir "$ALB"; fi
					if test 1 -le $VERBOSE; then
						echo "made directory \"$ALB\""
					fi
				fi
			fi
		fi
		
		if test 2 -eq $INTERACTIVE; then
			unset cont; read -n 1 -p "mv \"$i\" -> \"$ALB\" ? [Y/n]" cont
			echo
			if [[ $cont =~ ^[nN]$ ]]; then 
				continue 
			fi
		fi
		
		if $DO_THING; then mv "$i" "$ALB"; fi
		if test 2 -eq $VERBOSE; then
			echo "moved \"$i\" to \"$ALB\""
		fi
	fi
done

# vim: filetype=bash