File: //usr/local/mailchannels/bin/lib/check_version.sh
#!/usr/bin/env bash
#check a piece of software meets the minimum version requirements. Requires the command to run to get the version, and regex
#to parse the min and major parts of the version. Option parameters are the major and minor versions to require against
#Example checkVersion "php -v" "PHP\s([0-9]+)\.([0-9]+)\." 5 6 would check that the current php version is at least 5.6
if [[ -z $1 ]]; then
echo "You must provide a command to retrieve the version"
return 2
fi
if [[ -z $2 ]]; then
echo "You must provide a regex to parse the version"
return 2
fi
VERSION_CMD=$1
VERSION_REGEX=$2
REQUIRED_MAJOR=$3
REQUIRED_MINOR=$4
VERSION_STR=$(${VERSION_CMD})
if [[ ${VERSION_STR} =~ ${VERSION_REGEX} ]]; then
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
if [[ ${REQUIRED_MAJOR} != "" ]] && [[ ${REQUIRED_MAJOR} -gt ${MAJOR} ]]; then
exit 1
fi
if [[ ${REQUIRED_MAJOR} -eq ${MAJOR} ]] && [[ ${REQUIRED_MINOR} != "" ]] && [[ ${REQUIRED_MINOR} -gt ${MINOR} ]]; then
exit 1
fi
else
exit 2
fi
exit 0