Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 1 | #!/bin/bash |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 2 | # |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 3 | # download.sh -- |
| 4 | # Download a tarball from the specified URL to the specified target |
| 5 | # directory, untar it, and remove the tarball file. |
| 6 | # |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 7 | # Usage: download.sh <url> <target_dir> [<strip>] |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 8 | # |
| 9 | # where: |
| 10 | # |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 11 | # <url> is the URL of the repository to download. If the <url> ends |
| 12 | # in 'gz' then it is assumed to be a gzipped tarball format. |
| 13 | # Otherwise, it is assumed to be a clonable git repo. |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 14 | # <target_dir> is the local name to call the untarred directory. The |
| 15 | # tarball will be downloaded to the directory above this, |
| 16 | # untarred while renaming to <target_dir>, and then the tarball |
| 17 | # file will be deleted. |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 18 | # <strip> is the number of directory levels to strip off the front of the |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 19 | # tarball contents. Defaults to 1 if not specified (only |
| 20 | # applicable if <url> points to a tarball). |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 21 | # |
| 22 | |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 23 | # Check if <url> points to a tarball or a repository (note: this assumes |
| 24 | # the form on github where the filename is spelled out as ".tar.gz" and |
| 25 | # not ".tgz") |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 26 | |
Donn | f41154c | 2021-06-22 13:10:06 +0200 | [diff] [blame] | 27 | set -e |
| 28 | |
| 29 | if [ "${1: -3}" == ".gz"; ] then |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 30 | |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 31 | # Neither curl or wget are guaranteed to be included in all *nix systems, |
| 32 | # (but most have *one* of them). This tools tries its best to find one. |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 33 | |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 34 | DL_CMD= |
| 35 | if type "wget" > /dev/null; then |
| 36 | DL_CMD="wget -qO" |
| 37 | fi |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 38 | |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 39 | if type "curl" > /dev/null; then |
| 40 | DL_CMD="curl -sLo" |
| 41 | fi |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 42 | |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 43 | if [ "$DL_CMD" = "" ]; then |
| 44 | echo "ERROR: Either curl or wget are required to automatically install tools." |
| 45 | exit 1 |
| 46 | fi |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 47 | |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 48 | pdir=`dirname $2` |
| 49 | mkdir -p $pdir |
| 50 | cd $pdir |
| 51 | |
| 52 | echo "Downloading $1 to $2" |
| 53 | $DL_CMD $2.tar.gz $1 |
| 54 | |
| 55 | if [ $# -gt 2 ]; then |
| 56 | snum=$3 |
| 57 | else |
| 58 | snum=1 |
| 59 | fi |
| 60 | |
| 61 | mkdir -p $2 |
| 62 | echo "Untarring and removing $2.tar.gz" |
| 63 | tar -xf $2.tar.gz --strip-components $snum -C $2 |
| 64 | rm $2.tar.gz |
| 65 | |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 66 | else |
Tim Edwards | 8877f8f | 2021-06-10 21:35:36 -0400 | [diff] [blame] | 67 | |
| 68 | if type "git" > /dev/null; then |
| 69 | echo "Cloning $1 to $2" |
| 70 | git clone --depth 1 $1 $2 |
| 71 | else |
| 72 | echo "ERROR: \"git\" is required to automatically install tools." |
| 73 | exit 1 |
| 74 | fi |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 75 | fi |
| 76 | |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 77 | exit 0 |