blob: 2eb59b08a9a16e4a3e1a794d1d2bb6db21bbd67b [file] [log] [blame]
#!/bin/bash
# Expansions of unset variables cause an error
set -u
# Given a script and it's content SHA-1, determine if it should run based upon
# whether its content has changed.
#
# Returns 0 if the script should run, other values indicate it should not run.
#
function should-run-script {
local script="$1"; shift
local script_sha1="$1"; shift
local script_basename=$(basename "${script}")
if [[ -f /var/cache/runonce/$script_basename ]]; then
previous_sha1=$(cat /var/cache/runonce/$script_basename)
if [[ $script_sha1 == $previous_sha1 ]]; then
return 1
fi
fi
return 0
}
function main {
local script=""
for script in /etc/runonce.d/*; do
local script_sha1=$(sha1sum $script |awk '{ print $1 }')
if ! should-run-script $script $script_sha1; then
continue;
fi
$script
if [[ $? == 0 ]]; then
local script_basename=$(basename $script)
echo $script_sha1 > /var/cache/runonce/$script_basename
sync /var/cache/runonce/$script_basename
fi
done
}
main