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 | # |
| 11 | # <url> is the URL of the repository to download, in gzipped tarball format |
| 12 | # <target_dir> is the local name to call the untarred directory. The |
| 13 | # tarball will be downloaded to the directory above this, |
| 14 | # untarred while renaming to <target_dir>, and then the tarball |
| 15 | # file will be deleted. |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 16 | # <strip> is the number of directory levels to strip off the front of the |
| 17 | # tarball contents. Defaults to 1 if not specified. |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 18 | # |
| 19 | |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 20 | # Neither curl or wget are guaranteed to be included in all *nix systems, |
| 21 | # (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] | 22 | |
| 23 | DL_CMD= |
| 24 | if type "wget" > /dev/null; then |
| 25 | DL_CMD="wget -qO" |
| 26 | fi |
| 27 | |
| 28 | if type "curl" > /dev/null; then |
| 29 | DL_CMD="curl -sLo" |
| 30 | fi |
| 31 | |
| 32 | if [ "$DL_CMD" = "" ]; then |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 33 | echo "ERROR: Either curl or wget are required to automatically install tools." |
Tim Edwards | debc0a4 | 2020-12-28 22:11:40 -0500 | [diff] [blame] | 34 | exit 1 |
| 35 | fi |
| 36 | |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 37 | pdir=`dirname $2` |
| 38 | mkdir -p $pdir |
| 39 | cd $pdir |
| 40 | |
| 41 | echo "Downloading $1 to $2" |
| 42 | $DL_CMD $2.tar.gz $1 |
| 43 | |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 44 | if [ $# -gt 2 ]; then |
| 45 | snum=$3 |
| 46 | else |
| 47 | snum=1 |
| 48 | fi |
| 49 | |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 50 | mkdir -p $2 |
Tim Edwards | cdfec5e | 2021-04-22 20:59:13 -0400 | [diff] [blame] | 51 | echo "Untarring and removing $2.tar.gz" |
| 52 | tar -xf $2.tar.gz --strip-components $snum -C $2 |
Tim Edwards | 6ee1153 | 2021-02-11 12:29:33 -0500 | [diff] [blame] | 53 | rm $2.tar.gz |
| 54 | exit 0 |