Sequential commands implemented reasonably well.
authorCameron Ball <c.ball1729@gmail.com>
Sun, 7 Jun 2015 16:26:52 +0000 (00:26 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Sun, 7 Jun 2015 16:26:52 +0000 (00:26 +0800)
cache-update-adapter.sh [new file with mode: 0755]
menu.json
menu.sh
rsync-adapter.sh

diff --git a/cache-update-adapter.sh b/cache-update-adapter.sh
new file mode 100755 (executable)
index 0000000..e6737a5
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/bash
+#
+# widget:      gauge
+# description: Modifies updates ITG's cache with output
+#              tailiored to dialogs gauge widget.
+
+while getopts ":d:" opt; do
+  case $opt in
+    d)
+      cache_dir=$OPTARG
+      ;;
+    \?)
+      echo "Invalid option: -$OPTARG" >&2
+      exit 1
+      ;;
+    :)
+      echo "Option -$OPTARG requires an argument." >&2
+      exit 1
+      ;;
+  esac
+done
+
+if [[ -z "$cache_dir" ]] || [[ "${cache_dir}xxx" = "xxx" ]]; then
+       echo "Cache directory not supplied.";
+       exit 1
+fi
+
+num=$(wc -l < /tmp/newsongs.txt)
+num_processed=0
+
+if [[ $num -gt 0 ]]; then
+       while read line; do
+               read -r changes filename <<< "$line"
+               #todo: change directory to be arg'able
+               cacheline=$(grep -F "$filename" ${cache_dir}/*)
+               IFS=":" read -r cachepath otherjunk <<< "$cacheline"
+
+               if [[ ! -z "$cachepath" ]]; then
+                       rm "$cachepath"
+               fi
+               ((num_processed++))
+
+               printf '%i %i' $num_processed $num | mawk -Winteractive '{ pc=100*$1/$2; i=int(pc); print (pc-i<0.5)?i:i+1 }'
+       done < /tmp/newsongs.txt
+else
+       echo 100
+fi
+
index a184b01..afb6d39 100644 (file)
--- a/menu.json
+++ b/menu.json
             "type": "task",
             "description": "Update the ITG song list",
             "commands": [
-               "./rsync-adapter.sh -ia --delete --exclude 'A is for Cool Shit' /mnt/linkstation/Music/SkyHill /home/cameron/rsync-adapter-test",
-               "another command",
-               "and another"
+               {
+                       "command": "./rsync-adapter.sh -ia --delete --exclude 'A is for Cool Shit' /mnt/linkstation/Test/Songs /mnt/linkstation/Test/itg",
+                       "title": "Syncing songs..."
+               },
+               {
+                       "command": "./cache-update-adapter.sh -d /mnt/linkstation/Test/itg/Cache/Songs/",
+                       "title": "Updating cache..."
+               }
            ],
             "user": "itg"
           }
diff --git a/menu.sh b/menu.sh
index f1b20dc..7985ce2 100755 (executable)
--- a/menu.sh
+++ b/menu.sh
@@ -16,9 +16,19 @@ function debug()
        >&2 echo "$1"
 }
 
+function get_key_from_line()
+{
+       key_re="(.+?)[[:space:]]+\""
+       while read -r line; do
+               [[ "$1" =~ $key_re ]] && echo ${BASH_REMATCH[1]} && break
+       done <<< "$menu_json"
+}
+
 #Pass something like MainMenu.type and it'll
 #give the value
-function get_key()
+#nice bonus: If passed a full line from menu_json
+#this will return the value for it
+function get_value_from_key()
 {
        value_re="[[:space:]]+\"(.+)\"$"
        while read -r line; do
@@ -48,20 +58,20 @@ function short_path_to_full_path()
 function get_item_type()
 {
        full_path=$(short_path_to_full_path $1)
-       get_key "$full_path.type"
+       get_value_from_key "$full_path.type"
 }
 
 function get_item_description()
 {
         full_path=$(short_path_to_full_path $1)
-        get_key "$full_path.description"
+        get_value_from_key "$full_path.description"
 }
 
 #(dot-delim, key)
 function get_item_key()
 {
        full_path=$(short_path_to_full_path $1)
-       get_key "$full_path.$2"
+       get_value_from_key "$full_path.$2"
 }
 
 #Returns true if line is a child
@@ -92,6 +102,16 @@ function is_child_of()
        fi
 }
 
+#file, #detail
+function get_adapter_detail()
+{
+        value_re="# ${2}:[[:space:]]+(.+)$"
+
+        while read -r line; do
+                [[ $line =~ $value_re ]] &&  echo ${BASH_REMATCH[1]} && break
+        done < "$1"
+}
+
 #Use a dot delimited path to the menu
 #e.g.,
 #      MainMenu.Services
@@ -153,10 +173,26 @@ function run_task()
        full_path=$(short_path_to_full_path $1)
        while read -r line; do
                if is_child_of "$line" "${full_path}.commands"; then
-                       debug "$(get_key $line)"
-                       #logic to read the commands to work out widgets etc
-                       #$task_command | dialog --clear --backtitle "$backtitle" --title "Running $1" --gauge "Processing..." 6 50
+                       command_re=".\/(.+.sh)"
+                       if [[ $line =~ $command_re ]]; then
+                               widget=$(get_adapter_detail "${BASH_REMATCH[1]}" "widget")
+                               key=$(get_key_from_line "$line")
+                               command="$(get_value_from_key $key)"
+                               title=$(get_value_from_key "${key%.*}.title")
+
+                               #todo: check exit status and display an error message if one
+                               #command fails (and break the loop)
+                               case "$widget" in
+                                       gauge) eval "$command" | dialog --clear --backtitle "$backtitle" --title "Running $1" --gauge "$title" 6 50;;
+                               esac
+                       fi
                 fi
+
+               #todo: Figure out a nice way to display a success message that can be customised
+               #for example after updating the songlist it would be nice for it to say how many
+               #song were updated
+               #
+               #Perhaps the easiest way is just to add another task (i.e., write another script) to do that?
         done <<< "$menu_json"
 
         echo "Back" > "${INPUT}"
index 2d3e6f8..4e48453 100755 (executable)
@@ -1,5 +1,8 @@
 #!/bin/bash
+#
+# widget:      gauge
+# description: Modifies rsync's output to be used
+#              with dialog's --progress option
 
-
-rsync --progress "$@" | tee >(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-check=") > 0) { split($0, pieces, "to-check="); split(pieces[2], term, ")"); split(term[1], division, "/"); print (1-(division[1]/division[2]))*100 } }' \
                      | sed --unbuffered 's/\([0-9]*\).*/\1/'