#!/bin/sh # contains functions related to the intentions-queue # shellcheck disable=SC2039 if [ -z "${base_dir}" ]; then # just to make shellcheck happy . '../lib/load-configuration' fi # intent_something [-n] # create an intention, provided on stdin - or just show the intention (-n) # note, that this function is not thread-safe - use the lock # $package_database_lock_file externally intent_something() { local next_number if [ "x$1" = 'x-n' ]; then cat return fi next_number=$(( $( find "${intentions_directory}" \ -maxdepth 1 \ -type f \ -name 'intention.*' \ -printf '%f\n' \ | sed ' s/^intention\.// t d ' \ | sort -nr \ | grep -xm1 '[0-9]\+' \ || echo 0 )+1 )) { # shellcheck disable=SC2016 printf '%s\n' \ '#!/bin/sh' \ '[ -n "${base_dir}" ] || . '"${base_dir}"'/lib/load-configuration' cat printf 'rm "%s"\n' "${intentions_directory}/intention.${next_number}" } > "${intentions_directory}/intention.${next_number}" chmod +x "${intentions_directory}/intention.${next_number}" } # execute_intention # executes the next intention execute_intention() { local next_number next_number=$( find "${intentions_directory}" \ -maxdepth 1 \ -type f \ -name 'intention.*' \ -printf '%f\n' \ | sed ' s/^intention\.// t d ' \ | sort -n \ | grep -xm1 '[0-9]\+' ) || return 0 "${intentions_directory}/intention.${next_number}" } # intentions_left [-n] # check if there are undone intentions left # return 0 if there is something left to do # return 1 if nothing is queued # with -n: return immediately # without -n: wait until nothing more to do intentions_left() { if [ ! "x$1" = 'x-n' ]; then while intentions_left -n; do sleep 1 done fi find "${intentions_directory}" \ -maxdepth 1 \ -type f \ -name 'intention.*' \ -printf '%f\n' \ | grep -qxm1 'intention\.[0-9]\+' } # execute_all_intentions # executes all intentions execute_all_intentions() { while intentions_left -n; do execute_intention done }