Fixes for new menu.json format plus simlinking in place
authorCameron Ball <cameron@getapproved.com.au>
Mon, 22 Jun 2015 07:54:55 +0000 (15:54 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Mon, 22 Jun 2015 07:54:55 +0000 (15:54 +0800)
menu.json
menu.sh
rsync-adapter.sh
simlink-adapter.sh [new file with mode: 0755]
simlink.sh [deleted file]

index b9e4138..5df2735 100644 (file)
--- a/menu.json
+++ b/menu.json
                 "command": "./rsync-adapter.sh -ia --delete --exclude 'A is for Cool Shit' /home/cameron/PreComTestRepo/Songs /home/cameron/Games/OpenITG",
                 "title": "Syncing songs..."
             },
+           {
+               "command": "./simlink-adapter.sh -d /home/cameron/Games/OpenITG/Songs/ -f 240 -s 5 -r 13",
+               "title": "Sorting songs..."
+           },
             {
                 "command": "./cache-update-adapter.sh -d /itg/Cache/Songs/",
                 "title": "Updating cache..."
@@ -54,7 +58,7 @@
         "description": "Run presets",
         "items": [
         {
-            "name": "Sync and Launch",
+            "name": "Sync_and_Launch",
             "type": "preset",
             "description": "Sync songs and launch ITG",
             "itemsToRun": [
diff --git a/menu.sh b/menu.sh
index 45f054a..d45cf86 100755 (executable)
--- a/menu.sh
+++ b/menu.sh
@@ -14,6 +14,7 @@ box_height=30
 function debug()
 {
        >&2 echo "$1"
+       sleep 2
 }
 
 function get_key_from_line()
@@ -49,7 +50,6 @@ function get_relative_child_path()
                echo "$1"
        else
                re="${2//./\\.}\.items\.([0-9]+?)\.name"
-               debug "$re"
                while read -r line; do
                        if [[ "$line" =~ $re ]]; then
                                sub_path=${BASH_REMATCH[1]}
@@ -80,7 +80,7 @@ function short_path_to_full_path()
                                path_so_far="${path_so_far}.${relative_path}"
                        fi
                done
-       done <<< "$1"
+       done <<< "${1//_/\ }"
 
        echo "$path_so_far"
 }
@@ -174,7 +174,7 @@ function render_menu()
                                key=$(get_key_from_line "$line")
                                desc=$(get_value_from_key "${key%.*}.description")
                                name=$(get_value_from_key "${key%.*}.name")
-                               options+=("$name" "$desc")
+                               options+=("${name//_/\ }" "$desc")
                        fi
                fi
        done <<< "$menu_json"
@@ -219,6 +219,7 @@ function run_task()
        #I tried $(short_path_to_full_path $1).commands
        #but it produced a weird result.
        local full_path=$(short_path_to_full_path $1)
+
        while read -r line; do
                if is_child_of "$line" "${full_path}.commands"; then
                        command_re=".\/(.+.sh)"
@@ -229,8 +230,8 @@ function run_task()
                                title=$(get_value_from_key "${key%.*}.title")
 
                                case "$widget" in
-                                       gauge) eval "$command" | dialog --clear --backtitle "$backtitle" --title "Running $1" --gauge "$title" "$box_height" "$box_width";;
-                                       msgbox) eval "$command" | dialog --clear --backtitle "$backtitle" --title "$title" --msgbox "$(eval \"$command\")" "$box_height" "$box_width";;
+                                       gauge) eval "$command" | dialog --clear --backtitle "$backtitle" --title "Running $1" --gauge "$title" 6 60;;
+                                       msgbox) eval "$command" | dialog --clear --backtitle "$backtitle" --title "$title" --msgbox "$(eval \"$command\")" 8 40;;
                                esac
                        fi
                fi
@@ -246,8 +247,6 @@ function run_preset()
                 if is_child_of "$line" "${full_path}.itemsToRun"; then
                        key=$(get_key_from_line "$line")
                        item=$(get_value_from_key "$key")
-                       debug "$item"
-                       sleep 3
                        process_item "$item"
                fi
        done <<< "$menu_json"
@@ -258,6 +257,7 @@ function run_preset()
 function process_item()
 {
        type=$(get_item_type $1)
+
        case $type in
                menu) render_menu $1 $2;;
                service) toggle_service $1 $2;;
@@ -278,7 +278,7 @@ while true; do
         case $selection in
                 Quit) break;;
                 Back) current_item="${current_item%.*}";;
-                *) current_item="$current_item.$selection";;
+                *) current_item="$current_item.${selection//\ /_}";;
         esac
 done
 
index 41518a5..c4d2d59 100755 (executable)
@@ -5,5 +5,5 @@
 #              with dialog's --progress option
 
 #XXX: Some versions of rsync say to-chk instead of to-check, be careful
-rsync --progress "$@" | tee >(grep -v "*deleting" | grep -i ".sm" > /tmp/newsongs.txt) | mawk -Winteractive '{ if (index($0, "to-check=") > 0) { split($0, pieces, "to-check="); split(pieces[2], term, ")"); split(term[1], division, "/"); print (1-(division[1]/division[2]))*100 } }' \
+rsync --progress "$@" | tee >(grep -v "*deleting" | grep -i ".sm" > /tmp/newsongs.txt) | mawk -Winteractive '{ if (index($0, "to-chk=") > 0) { split($0, pieces, "to-chk="); split(pieces[2], term, ")"); split(term[1], division, "/"); print (1-(division[1]/division[2]))*100 } }' \
                      | sed --unbuffered 's/\([0-9]*\).*/\1/'
diff --git a/simlink-adapter.sh b/simlink-adapter.sh
new file mode 100755 (executable)
index 0000000..4322cfe
--- /dev/null
@@ -0,0 +1,142 @@
+#!/bin/bash
+#
+# widget:       gauge
+# description:  Sorts simfiles in to footspeed and stamina folders
+#              based on user defined thresholds.
+#              Input is tailored to suit dialog's gauge widget.
+#
+
+while getopts ":d:f:s:r:" opt; do
+  case $opt in
+    d)
+      songs_dir=$OPTARG
+      ;;
+    f)
+      footspeed_cutoff=$OPTARG
+      ;;
+    s)
+      stamina_cutoff=$OPTARG
+      ;;
+    r)
+      rating_cutoff=$OPTARG
+      ;;
+    \?)
+      echo "Invalid option: -$OPTARG" >&2
+      exit 1
+      ;;
+    :)
+      echo "Option -$OPTARG requires an argument." >&2
+      exit 1
+      ;;
+  esac
+done
+
+if [[ -z "$songs_dir" ]] || [[ "${songs_dir}xxx" = "xxx" ]]; then
+        echo "Songs directory not supplied.";
+        exit 1
+fi
+
+if [[ -z "$footspeed_cutoff" ]] || [[ "${footspeed_cutoff}xxx" = "xxx" ]]; then
+        echo "Footspeed cutoff not supplied.";
+        exit 1
+fi
+
+if [[ -z "$stamina_cutoff" ]] || [[ "${stamina_cutoff}xxx" = "xxx" ]]; then
+        echo "Stamina cutoff not supplied.";
+        exit 1
+fi
+
+if [[ -z "$rating_cutoff" ]] || [[ "${rating_cutoff}xxx" = "xxx" ]]; then
+        echo "Rating cutoff not supplied.";
+        exit 1
+fi
+
+[[ ! $footspeed_cutoff =~ ^-?[0-9]+$ ]] && echo "Footspeed cutoff must be integer" && exit 1
+[[ ! $stamina_cutoff =~ ^-?[0-9]+$ ]] && echo "Stamina cutoff must be integer" && exit 1
+[[ ! $rating_cutoff =~ ^-?[0-9]+$ ]] && echo "Rating cutoff must be integer" && exit 1
+
+[[ ! -d "$songs_dir" ]] && echo 100 && exit 0
+
+num=$(wc -l < /tmp/newsongs.txt)
+
+[[ ! $num -gt 0 ]] && echo 100 && exit 0
+
+IFS=$'\n'
+
+#(file, key)
+function extract_key()
+{
+       key=$(grep $2 $1 | cut -d ':' -f 2 | cut -d ';' -f 1 | tr -d $'\r')
+       echo "$key"
+}
+
+function song_length_minutes()
+{
+       path=$1
+       music_file=$(extract_key $1 "MUSIC")
+       music_path=${path%/*}
+
+       length=$(ffmpeg -i "$music_path/$music_file" 2>&1 | grep Duration |awk '{print $2}' | cut -d ':' -f 2 | tr -d ,)
+       length=${length##0}
+
+       echo "$length"
+}
+
+function max_bpm()
+{
+       bpms=$(extract_key $1 "BPMS")
+       IFS=',' read -a array <<< "$bpms"
+
+       max300=0.00
+        for bpm in "${array[@]}"
+       do
+               bpm=$(cut -d '=' -f 2 <<< "$bpm")
+               comp=$(echo "$bpm > $max300" | bc)
+               ((comp > 0)) && max300=$bpm
+       done
+
+       echo "$max300"
+}
+
+function difficulty()
+{
+       noteslines=$(grep -n "NOTES" $1)
+       readarray -t array <<<"$noteslines"
+       max300=0
+
+        for noteline in "${array[@]}"
+       do
+               linenum=$(cut -d ':' -f 1 <<< "$noteline")
+               linenum=$((linenum+4))
+               line=$(sed -n "${linenum}p" $1 | cut -d ':' -f 1)
+               diff="$(echo -e "${line}" | tr -d '[[:space:]]')"
+               comp=$(echo "$diff > $max300" | bc)
+               ((comp > 0)) && max300=$diff
+       done
+
+       echo "$max300"
+}
+
+mkdir -p "${songs_dir}/Footspeed"
+mkdir -p "${songs_dir}/Stamina"
+
+total_charts=$(find "$songs_dir" -name *.sm | wc -l)
+num_processed=0
+
+for file in $(find "$songs_dir" -name *.sm)
+do
+       difficulty=$(difficulty $file)
+       maxbpm=$(max_bpm $file)
+       songlength=$(song_length_minutes $file)
+
+       if [[ "$difficulty" > $rating_cutoff ]]; then
+               path_to_chart="$(dirname "$file")"
+               folder_name="$(basename "$path_to_chart")"
+               [[ "$maxbpm" > "$footspeed_cutoff" ]] && ln -s "${path_to_chart}" "${songs_dir}/Footspeed/${folder_name}" > /dev/null  2>&1
+               [[ "$songlength" > "$stamina_cutoff" ]] &&  ln -s "${path_to_chart}" "${songs_dir}/Stamina/${folder_name}" > /dev/null 2>&1
+       fi
+
+        ((num_processed++))
+
+        printf '%i %i' $num_processed $total_charts | mawk -Winteractive '{ pc=100*$1/$2; i=int(pc); print (pc-i<0.5)?i:i+1 }'
+done
diff --git a/simlink.sh b/simlink.sh
deleted file mode 100644 (file)
index c4a9208..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#TITLE: Organise Simfiles
-#PARENT: ITG Options
-#WIDGET: progress_bar
-
-#!/bin/bash
-
-songs_dir=/home/cameron/Games/OpenITG/Songs
-symlink_dir=/home/cameron/Games/OpenITG/Songs/Fast
-
-IFS=$'\n'
-
-#(file, key)
-function extract_key()
-{
-       key=$(grep $2 $1 | cut -d ':' -f 2 | cut -d ';' -f 1 | tr -d $'\r')
-       echo "$key"
-}
-
-function song_length_minutes()
-{
-       path=$1
-       music_file=$(extract_key $1 "MUSIC")
-       music_path=${path%/*}
-
-       length=$(ffmpeg -i "$music_path/$music_file" 2>&1 | grep Duration |awk '{print $2}' | cut -d ':' -f 2 | tr -d ,)
-       length=${length##0}
-
-       echo "$length"
-}
-
-function max_bpm()
-{
-       bpms=$(extract_key $1 "BPMS")
-       IFS=',' read -a array <<< "$bpms"
-
-       max300=0.00
-        for bpm in "${array[@]}"
-       do
-               bpm=$(cut -d '=' -f 2 <<< "$bpm")
-               comp=$(echo "$bpm > $max300" | bc)
-               ((comp > 0)) && max300=$bpm
-       done
-
-       echo "$max300"
-}
-
-function difficulty()
-{
-       noteslines=$(grep -n "NOTES" $1)
-       readarray -t array <<<"$noteslines"
-       max300=0
-
-        for noteline in "${array[@]}"
-       do
-               linenum=$(cut -d ':' -f 1 <<< "$noteline")
-               linenum=$((linenum+4))
-               line=$(sed -n "${linenum}p" $1 | cut -d ':' -f 1)
-               diff="$(echo -e "${line}" | tr -d '[[:space:]]')"
-               comp=$(echo "$diff > $max300" | bc)
-               ((comp > 0)) && max300=$diff
-       done
-
-       echo "$max300"
-}
-
-for file in $(find "$songs_dir" -name *.sm)
-do
-       difficulty=$(difficulty $file)
-       maxbpm=$(max_bpm $file)
-       songlength=$(song_length_minutes $file)
-
-       echo "$maxbpm"
-       echo "$difficulty"
-       echo "$songlength"
-done