|
| 1 | +# |
| 2 | +# Copyright (c) 2025 SkyLabs AI, Inc. |
| 3 | +# |
| 4 | + |
| 5 | +set -euf -o pipefail |
| 6 | + |
| 7 | +# Check that the expected variables are defined and non-empty. |
| 8 | +for VAR in PROG URL MIN; do |
| 9 | + if [[ ! -v ${VAR} ]] ; then |
| 10 | + echo "Bug: variable ${VAR} not defined." |
| 11 | + exit 2 |
| 12 | + fi |
| 13 | +done |
| 14 | + |
| 15 | +# Check that the print_ver function is defined. |
| 16 | +if [[ ! $(type -t print_ver) == "function" ]]; then |
| 17 | + echo "Bug: function print_ver not defined." |
| 18 | + exit 2 |
| 19 | +fi |
| 20 | + |
| 21 | +# Usage: available PROG |
| 22 | +# Description: indicates whether PROG exists in PATH. |
| 23 | +available() { |
| 24 | + type $1 2> /dev/null > /dev/null |
| 25 | +} |
| 26 | + |
| 27 | +# Usage: appropriate_version MIN VER [MAX] |
| 28 | +# Description: indicates whether MIN ≤ VER < MAX, using version order. |
| 29 | +appropriate_version() { |
| 30 | + if [[ "$2" == "$1" ]]; then |
| 31 | + # Version equal to the (included) lower bound. |
| 32 | + true |
| 33 | + elif [[ "$2" != "$(echo -e "$1\n$2" | sort -V | tail -n1)" ]]; then |
| 34 | + # Lower bound constraint violated. |
| 35 | + false |
| 36 | + elif [[ "$#" != "3" ]]; then |
| 37 | + # No upper-bound constraint. |
| 38 | + true |
| 39 | + elif [[ "$2" == "$3" ]]; then |
| 40 | + # Version equal to (excluded) upper bound. |
| 41 | + false |
| 42 | + else |
| 43 | + [[ "$2" == "$(echo -e "$2\n$3" | sort -V | head -n1)" ]] |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +instructions() { |
| 48 | + echo -e "\033[0;31m$1" |
| 49 | + if [[ -v MAX ]]; then |
| 50 | + echo -e "Install a version VER such that: ${MIN} ≤ VER < ${MAX}." |
| 51 | + else |
| 52 | + echo -e "Install a version VER such that: ${MIN} ≤ VER." |
| 53 | + fi |
| 54 | + if [[ -v RECOMMENDED ]]; then |
| 55 | + echo -e "Recommended version: ${RECOMMENDED}." |
| 56 | + fi |
| 57 | + echo -e "See ${URL} for instructions.\033[0m" |
| 58 | + exit 1 |
| 59 | +} |
| 60 | + |
| 61 | +if ! available "${PROG}"; then |
| 62 | + instructions "Could not find ${PROG}." |
| 63 | +fi |
| 64 | + |
| 65 | +VER=$(print_ver) |
| 66 | +if [[ -v MAX ]]; then |
| 67 | + if ! appropriate_version ${MIN} ${VER} ${MAX}; then |
| 68 | + instructions "Your version of ${PROG} (${VER}) is not supported." |
| 69 | + fi |
| 70 | +else |
| 71 | + if ! appropriate_version ${MIN} ${VER}; then |
| 72 | + instructions "Your version of ${PROG} (${VER}) is not supported." |
| 73 | + fi |
| 74 | +fi |
| 75 | + |
| 76 | +if [[ $(type -t extra_checks) == "function" ]]; then |
| 77 | + extra_checks |
| 78 | +fi |
| 79 | + |
| 80 | +echo "Using ${PROG} version ${VER}." |
0 commit comments