Normalise JSON. Update todo
[PreCom.git] / simlink-adapter.sh
1 #!/bin/bash
2 #
3 # widget: gauge
4 # description: Sorts simfiles in to footspeed and stamina folders
5 # based on user defined thresholds.
6 # Input is tailored to suit dialog's gauge widget.
7 #
8
9 while getopts ":d:f:s:r:" opt; do
10 case $opt in
11 d)
12 songs_dir=$OPTARG
13 ;;
14 f)
15 footspeed_cutoff=$OPTARG
16 ;;
17 s)
18 stamina_cutoff=$OPTARG
19 ;;
20 r)
21 rating_cutoff=$OPTARG
22 ;;
23 \?)
24 echo "Invalid option: -$OPTARG" >&2
25 exit 1
26 ;;
27 :)
28 echo "Option -$OPTARG requires an argument." >&2
29 exit 1
30 ;;
31 esac
32 done
33
34 if [[ -z "$songs_dir" ]] || [[ "${songs_dir}xxx" = "xxx" ]]; then
35 echo "Songs directory not supplied.";
36 exit 1
37 fi
38
39 if [[ -z "$footspeed_cutoff" ]] || [[ "${footspeed_cutoff}xxx" = "xxx" ]]; then
40 echo "Footspeed cutoff not supplied.";
41 exit 1
42 fi
43
44 if [[ -z "$stamina_cutoff" ]] || [[ "${stamina_cutoff}xxx" = "xxx" ]]; then
45 echo "Stamina cutoff not supplied.";
46 exit 1
47 fi
48
49 if [[ -z "$rating_cutoff" ]] || [[ "${rating_cutoff}xxx" = "xxx" ]]; then
50 echo "Rating cutoff not supplied.";
51 exit 1
52 fi
53
54 [[ ! $footspeed_cutoff =~ ^-?[0-9]+$ ]] && echo "Footspeed cutoff must be integer" && exit 1
55 [[ ! $stamina_cutoff =~ ^-?[0-9]+$ ]] && echo "Stamina cutoff must be integer" && exit 1
56 [[ ! $rating_cutoff =~ ^-?[0-9]+$ ]] && echo "Rating cutoff must be integer" && exit 1
57
58 [[ ! -d "$songs_dir" ]] && echo 100 && exit 0
59
60 num=$(wc -l < /tmp/newsongs.txt)
61
62 [[ ! $num -gt 0 ]] && echo 100 && exit 0
63
64 IFS=$'\n'
65
66 #(file, key)
67 function extract_key()
68 {
69 key=$(grep $2 $1 | cut -d ':' -f 2 | cut -d ';' -f 1 | tr -d $'\r')
70 echo "$key"
71 }
72
73 function song_length_minutes()
74 {
75 path=$1
76 music_file=$(extract_key $1 "MUSIC")
77 music_path=${path%/*}
78
79 length=$(ffmpeg -i "$music_path/$music_file" 2>&1 | grep Duration |awk '{print $2}' | cut -d ':' -f 2 | tr -d ,)
80 length=${length##0}
81
82 echo "$length"
83 }
84
85 function max_bpm()
86 {
87 bpms=$(extract_key $1 "BPMS")
88 IFS=',' read -a array <<< "$bpms"
89
90 max300=0.00
91 for bpm in "${array[@]}"
92 do
93 bpm=$(cut -d '=' -f 2 <<< "$bpm")
94 comp=$(echo "$bpm > $max300" | bc)
95 ((comp > 0)) && max300=$bpm
96 done
97
98 echo "$max300"
99 }
100
101 function difficulty()
102 {
103 noteslines=$(grep -n "NOTES" $1)
104 readarray -t array <<<"$noteslines"
105 max300=0
106
107 for noteline in "${array[@]}"
108 do
109 linenum=$(cut -d ':' -f 1 <<< "$noteline")
110 linenum=$((linenum+4))
111 line=$(sed -n "${linenum}p" $1 | cut -d ':' -f 1)
112 diff="$(echo -e "${line}" | tr -d '[[:space:]]')"
113 comp=$(echo "$diff > $max300" | bc)
114 ((comp > 0)) && max300=$diff
115 done
116
117 echo "$max300"
118 }
119
120 rm -rf "${songs_dir}/Footspeed"
121 rm -rf "${songs_dir}/Stamina"
122
123 mkdir -p "${songs_dir}/Footspeed"
124 mkdir -p "${songs_dir}/Stamina"
125
126 total_charts=$(find "$songs_dir" -name *.sm | wc -l)
127 num_processed=0
128
129 for file in $(find "$songs_dir" -name *.sm)
130 do
131 difficulty=$(difficulty $file)
132 maxbpm=$(max_bpm $file)
133 songlength=$(song_length_minutes $file)
134
135 if [[ "$difficulty" > $rating_cutoff ]]; then
136 path_to_chart="$(dirname "$file")"
137 folder_name="$(basename "$path_to_chart")"
138 [[ "$maxbpm" > "$footspeed_cutoff" ]] && ln -s "${path_to_chart}" "${songs_dir}/Footspeed/${folder_name}" > /dev/null 2>&1
139 [[ "$songlength" > "$stamina_cutoff" ]] && ln -s "${path_to_chart}" "${songs_dir}/Stamina/${folder_name}" > /dev/null 2>&1
140 fi
141
142 ((num_processed++))
143 printf '%i %i' $num_processed $total_charts | mawk -Winteractive '{ pc=100*$1/$2; i=int(pc); print (pc-i<0.5)?i:i+1 }'
144 done