printErrorHelpAndExit "Environment Error: $1 not found on the path or not executable"$invalidEnvironmentExitCode
fi
}
# Do not use this from directly. Due to a bug in bash, array assignments do not work when the function is used with command substitution
function createTemporaryFile
{
local temporaryFile="$(mktemp"$temporaryDirectory/$$.$1.XXXXXXXX")"|| printErrorHelpAndExit "Environment Error: Could not create a temporary file. Please check you /tmp folder permissions allow files and folders to be created and disc space."$invalidEnvironmentExitCode
local length="${#temporaryFiles[@]}"
temporaryFiles[$length]="$temporaryFile"
}
function mostRecentTemporaryFile
{
local length="${#temporaryFiles[@]}"
local lastIndex
((lastIndex =--length))
echo"${temporaryFiles[$lastIndex]}"
}
function deleteTemporaryFile
{
rm-f"$1"|| printErrorHelpAndExit "Environment Error: Could not delete a temporary file ($1)."$invalidEnvironmentExitCode
}
function removeTemporaryFiles
{
length="${#temporaryFiles[@]}"
if[$length-eq 0 ];then
return
fi
for temporaryFile in${temporaryFiles[@]};do
deleteTemporaryFile "$temporaryFile"
done
temporaryFiles=()
length="${#temporaryFiles[@]}"
}
function checkEnvironment
{
programs=(openssl curl od dd printf sed awk sort mktemp rm grep cp ls env bash)
for program in"${programs[@]}";do
checkProgramIsInEnvironment "$program"
done
local temporaryFolder="${TMPDIR:-/tmp}"
if[!-x"$temporaryFolder"];then
printErrorHelpAndExit "Environment Error: The temporary directory ($temporaryFolder) does not exist. Please set the TMPDIR environment variable to your temporary directory"$invalidEnvironmentExitCode
mkdir-p"$temporaryDirectory"|| printErrorHelpAndExit "Environment Error: Could not create a temporary directory ($temporaryDiectory). Please check you /tmp folder permissions allow files and folders to be created and you have sufficient disc space"$invalidEnvironmentExitCode
#Check we can create and delete temporary files
createTemporaryFile "check"
temporaryFileCheck="$(mostRecentTemporaryFile)"
echo"Checking we can write to temporary files. If this is still here then we could not delete temporary files.">"$temporaryFileCheck"
removeTemporaryFiles
}
function setErrorTraps
{
trap"removeTemporaryFiles; exit $internalErrorExitCode" INT TERM EXIT
}
function unsetErrorTraps
{
trap - INT TERM EXIT
}
function verifyUrl
{
if[-z"$url"];then
printErrorHelpAndExit "URL not specified"$userSpecifiedDataErrorExitCode
elif echo$url | grep-q http://;then
printErrorHelpAndExit "URL starts with http://"$userSpecifiedDataErrorExitCode
elif echo$url | grep-q https://;then
printErrorHelpAndExit "URL starts with https://"$userSpecifiedDataErrorExitCode
elif echo$url | grep-v ^/;then
printErrorHelpAndExit "URL does not start with /"$userSpecifiedDataErrorExitCode
fi
}
function appendHash
{
local fileToHash="$1"
local fileToWriteTo="$2"
$sha1"$fileToHash">>"$fileToWriteTo"
}
function writeHash
{
local fileToHash="$1"
local fileToWriteTo="$2"
$sha1-out"$fileToWriteTo""$fileToHash"
}
function checkAwsKey
{
local originalKeyFile="$1"
local keySize="$(ls-l"$originalKeyFile" | awk'{ print $5 }')"
if[!$keySize-eq 40 ];then
printErrorHelpAndExit "We do not understand Amazon AWS secret keys which are not 40 bytes long. Have you included a carriage return or line feed by mistake at the end of the secret key file?"$userSpecifiedDataErrorExitCode
fi
}
function padDecodedKeyTo
{
local originalKeyFile="$1"
local keyFile="$2"
cp"$originalKeyFile""$keyFile"
local keySize=$(ls-l"$keyFile" | awk'{ print $5 }')