|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +ARGS=("$@") |
| 6 | +PROGNAME=$(basename "$0") |
| 7 | +readonly ARGS PROGNAME |
| 8 | + |
| 9 | +function usage { |
| 10 | + cat <<-EOF |
| 11 | + usage: $PROGNAME options |
| 12 | +
|
| 13 | + Downloads the file at the given url to the download cache folder. |
| 14 | +
|
| 15 | + Does not re-download the file it already exists and matches the given checksum. |
| 16 | +
|
| 17 | + Unpacks the file if the destination option is given. |
| 18 | +
|
| 19 | + Download is placed in the directory ${DOWNLOAD_CACHE_DIRECTORY}. |
| 20 | +
|
| 21 | + OPTIONS: |
| 22 | + -u --url The url of the file to download. |
| 23 | + -c --sha256 The sha256 checksum to use to validate the download. |
| 24 | + -d --dest The location to unpack file into (optional). |
| 25 | + -s --strip Exclude the root folder when unpacking (optional, not supported with gzip or jar). |
| 26 | + -h --help Show this help. |
| 27 | + -x --debug Debug this script. |
| 28 | +
|
| 29 | + Examples: |
| 30 | + $PROGNAME \\ |
| 31 | + --url https://github.com/just-containers/s6-overlay/releases/download/v1.22.1.0/s6-overlay-amd64.tar.gz |
| 32 | + --sha256 7f3aba1d803543dd1df3944d014f055112cf8dadf0a583c76dd5f46578ebe3c2 \\ |
| 33 | + --dest /opt/s6-overlay |
| 34 | +EOF |
| 35 | +} |
| 36 | + |
| 37 | +function cmdline { |
| 38 | + local arg= |
| 39 | + local args= |
| 40 | + for arg; do |
| 41 | + local delim="" |
| 42 | + case "$arg" in |
| 43 | + # Translate --gnu-long-options to -g (short options) |
| 44 | + --url) args="${args}-u " ;; |
| 45 | + --sha256) args="${args}-c " ;; |
| 46 | + --dest) args="${args}-d " ;; |
| 47 | + --strip) args="${args}-s " ;; |
| 48 | + --help) args="${args}-h " ;; |
| 49 | + --debug) args="${args}-x " ;; |
| 50 | + # Pass through anything else |
| 51 | + *) |
| 52 | + [[ "${arg:0:1}" == "-" ]] || delim="\"" |
| 53 | + args="${args}${delim}${arg}${delim} " |
| 54 | + ;; |
| 55 | + esac |
| 56 | + done |
| 57 | + |
| 58 | + # Reset the positional parameters to the short options |
| 59 | + eval set -- "${args}" |
| 60 | + |
| 61 | + while getopts "u:c:d:shx" OPTION; do |
| 62 | + case $OPTION in |
| 63 | + u) |
| 64 | + readonly URL=${OPTARG} |
| 65 | + ;; |
| 66 | + c) |
| 67 | + readonly CHECKSUM=${OPTARG} |
| 68 | + ;; |
| 69 | + d) |
| 70 | + readonly DEST=${OPTARG} |
| 71 | + ;; |
| 72 | + s) |
| 73 | + readonly STRIP=true |
| 74 | + ;; |
| 75 | + h) |
| 76 | + usage |
| 77 | + exit 0 |
| 78 | + ;; |
| 79 | + x) |
| 80 | + set -x |
| 81 | + ;; |
| 82 | + *) |
| 83 | + echo "Invalid Option: $OPTION" >&2 |
| 84 | + usage |
| 85 | + exit 1 |
| 86 | + ;; |
| 87 | + esac |
| 88 | + done |
| 89 | + |
| 90 | + if [[ -z $URL || -z $CHECKSUM ]]; then |
| 91 | + echo "Missing one or more required options: --url --sha256" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + # All remaning parameters are files to be removed from the installation. |
| 96 | + shift $((OPTIND-1)) |
| 97 | + readonly REMOVE=("$@") |
| 98 | + |
| 99 | + return 0 |
| 100 | +} |
| 101 | + |
| 102 | +function validate { |
| 103 | + local file=${1} |
| 104 | + sha256sum "${file}" | cut -f1 -d' ' | xargs test "${CHECKSUM}" == |
| 105 | +} |
| 106 | + |
| 107 | +function unpack { |
| 108 | + local file="${1}" |
| 109 | + local dest="${2}" |
| 110 | + local args=() |
| 111 | + local filename= |
| 112 | + mkdir -p "${dest}" |
| 113 | + if [[ -v STRIP ]]; then |
| 114 | + args+=("--strip-components" "1") |
| 115 | + fi |
| 116 | + filename=$(basename "${file}") |
| 117 | + case "${file}" in |
| 118 | + *.tar.xz | *.txz) |
| 119 | + tar -xf "${file}" -C "${dest}" "${args[@]}" |
| 120 | + ;; |
| 121 | + *.tar.gz | *.tgz) |
| 122 | + tar -xzf "${file}" -C "${dest}" "${args[@]}" |
| 123 | + ;; |
| 124 | + *.gz | *.gzip) |
| 125 | + gunzip "${file}" -f -c > "${dest}/${filename%.*}" |
| 126 | + ;; |
| 127 | + *.zip | *.war) |
| 128 | + if [[ -v STRIP ]]; then |
| 129 | + mkdir -p /tmp/unpack |
| 130 | + unzip "${file}" -d /tmp/unpack |
| 131 | + mv "$(find /tmp/unpack/ -type d -mindepth 1 -maxdepth 1)"/* "${dest}" |
| 132 | + rm -fr /tmp/unpack |
| 133 | + else |
| 134 | + unzip "${file}" -d "${dest}" |
| 135 | + fi |
| 136 | + ;; |
| 137 | + *.jar) |
| 138 | + cp "${file}" "${dest}" |
| 139 | + ;; |
| 140 | + *) |
| 141 | + echo "Unable to unpack ${file} please update script to support additional formats." >&2 |
| 142 | + exit 1 |
| 143 | + ;; |
| 144 | + esac |
| 145 | + # Remove extraneous files. |
| 146 | + for i in "${REMOVE[@]}"; do |
| 147 | + rm -fr "${dest:?}/${i}" |
| 148 | + done |
| 149 | +} |
| 150 | + |
| 151 | +function main { |
| 152 | + local file |
| 153 | + cmdline "${ARGS[@]}" |
| 154 | + |
| 155 | + DOWNLOAD_CACHE_DIRECTORY=${DOWNLOAD_CACHE_DIRECTORY:-/tmp} |
| 156 | + file="${DOWNLOAD_CACHE_DIRECTORY:?}/$(basename "${URL}")" |
| 157 | + # Remove the downloaded file if it exist and does not match the checksum so that it can be downloaded again. |
| 158 | + if [ -f "${file}" ] && ! validate "${file}"; then |
| 159 | + rm "${file}" |
| 160 | + fi |
| 161 | + curl \ |
| 162 | + -o "${DOWNLOAD_CACHE_DIRECTORY}/$(basename "${URL}")" \ |
| 163 | + -z "${DOWNLOAD_CACHE_DIRECTORY}/$(basename "${URL}")" \ |
| 164 | + -L "${URL}" |
| 165 | + # Return non-zero if the checksum does not match the downloaded file. |
| 166 | + validate "${file}" |
| 167 | + if [[ -v DEST ]]; then |
| 168 | + unpack "${file}" "${DEST}" |
| 169 | + fi |
| 170 | +} |
| 171 | +main |
0 commit comments