Specify paths correctly
[GrooveNet.git] / FaveSync.sh
1 #!/bin/bash
2
3 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4 source "$DIR/config.sh"
5
6 if [ -z $1 ]; then
7 exit
8 fi
9
10 songs_file=$(ls "$DIR" | grep songs | tail -n1)
11
12 if [ -z "$songs_file" ]; then
13 exit
14 fi
15
16
17 if [ "$1" = "push" ]; then
18 if find "$path_to_songs/$faves_folder" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
19 find -L "$path_to_songs/$faves_folder" -name \*.sm -exec md5sum {} \; | cut -sd ' ' -f1 > "$DIR/$me.favourites.txt"
20 scp "$DIR/$me.favourites.txt" groovenet@cameron1729.xyz:/mnt/media/GrooveNet
21 fi
22 fi
23
24 if [ "$1" = "apply" ]; then
25 if find "${DIR}"/*.favourites.txt -type f -print -quit 2>/dev/null | grep -q .; then
26 find "${DIR}"/*.favourites.txt -type f -print0 | while read -d $'\0' file; do
27 IFS=$'\n' read -d '' -r -a lines < $file
28 file=$(basename "$file")
29 person="${file%.*.*}"
30
31 if [ "$person" = "$me" ]; then
32 persons_folder="$faves_folder"
33 else
34 formatted_person="$(tr '[:lower:]' '[:upper:]' <<< ${person:0:1})${person:1}"
35 persons_folder=${external_favourite_naming_scheme/\%person\%/$formatted_person}
36 fi
37
38 rm -rf "$path_to_songs/$persons_folder"
39 mkdir "$path_to_songs/$persons_folder"
40
41 pattern=""
42 for i in "${lines[@]}"; do
43 pattern="${pattern}|$i"
44 done
45
46 pattern="${pattern:1}"
47 song_paths=$(grep -E "$pattern" "${DIR}/${songs_file}" | sort -u -k1,1 | cut -sd / -f5-6)
48
49 while read -r song_path; do
50 just_song=$(echo $song_path | cut -sd / -f2)
51 crc32="$(echo -n "/Songs/${persons_folder}/${just_song}/" | gzip -c | tail -c8 | hexdump -n4 -e '"%u"')"
52
53 echo "${just_song} -> ${persons_folder}"
54 echo -e "\t${path_to_song_cache}/${crc32}"
55
56 rm "${path_to_song_cache}/${crc32}"
57 ln -s "$path_to_songs/$song_path" "$path_to_songs/$persons_folder/$just_song"
58 done <<< "$song_paths"
59 done
60 fi
61 fi