#!/bin/sh # contains functions related to the intentions-queue # shellcheck disable=SC2039,SC2119,SC2120 if [ -z "${base_dir}" ]; then # just to make shellcheck happy . '../lib/load-configuration' fi # TODO: Create an intentions-queue: It should handle all mysql and # master mirror interactions which interact with each other or other # local actions (repo-add, repo-remove, ...). # Design idea: Create enumerated shell scripts which do the right thing # and remove themself after successful execution. # return-assignment would create the following scripts: # 1. download database from mirror # 2. update package database # 3. update mysql database # 4. upload database and package to mirror # 5. clean up temporary files # db-update would create the following scripts (after getting the usual # database lock and determining what needs to be done): # 1. download database from mirror # 2. update package database # 3. update mysql database # 4. upload database to mirror # 5. update packages on mirror # 6. clean up temporary files # intent_something # create an intention, provided on stdin # note, that this function is not thread-safe - use the lock # $package_database_lock_file externally intent_something() { local next_number 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 )) { printf '%s\n' \ '#!/bin/sh' \ 'set -e' 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 # check if there are undone intentions left # return 0 if there is something left to do # return 1 if nothing is queued intentions_left() { find "${intentions_directory}" \ -maxdepth 1 \ -type f \ -name 'intention.*' \ -printf '%f\n' \ | sed ' s/^intention\.// t d ' \ | sort -n \ | grep -qxm1 '[0-9]\+' }