--- /dev/null
+#!/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
+
"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"
}
>&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
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
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
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}"
#!/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/'