blob: a4188d93c4b7da09b3bb6c0f9a2b4c07b7337f9a [file] [log] [blame]
Tim Edwardscdfec5e2021-04-22 20:59:13 -04001#!/bin/bash
Tim Edwardsdebc0a42020-12-28 22:11:40 -05002#
Tim Edwards6ee11532021-02-11 12:29:33 -05003# 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 Edwardscdfec5e2021-04-22 20:59:13 -04007# Usage: download.sh <url> <target_dir> [<strip>]
Tim Edwards6ee11532021-02-11 12:29:33 -05008#
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 Edwardscdfec5e2021-04-22 20:59:13 -040016# <strip> is the number of directory levels to strip off the front of the
17# tarball contents. Defaults to 1 if not specified.
Tim Edwards6ee11532021-02-11 12:29:33 -050018#
19
Tim Edwardsdebc0a42020-12-28 22:11:40 -050020# 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 Edwardsdebc0a42020-12-28 22:11:40 -050022
23DL_CMD=
24if type "wget" > /dev/null; then
25 DL_CMD="wget -qO"
26fi
27
28if type "curl" > /dev/null; then
29 DL_CMD="curl -sLo"
30fi
31
32if [ "$DL_CMD" = "" ]; then
Tim Edwards6ee11532021-02-11 12:29:33 -050033 echo "ERROR: Either curl or wget are required to automatically install tools."
Tim Edwardsdebc0a42020-12-28 22:11:40 -050034 exit 1
35fi
36
Tim Edwards6ee11532021-02-11 12:29:33 -050037pdir=`dirname $2`
38mkdir -p $pdir
39cd $pdir
40
41echo "Downloading $1 to $2"
42$DL_CMD $2.tar.gz $1
43
Tim Edwardscdfec5e2021-04-22 20:59:13 -040044if [ $# -gt 2 ]; then
45 snum=$3
46else
47 snum=1
48fi
49
Tim Edwards6ee11532021-02-11 12:29:33 -050050mkdir -p $2
Tim Edwardscdfec5e2021-04-22 20:59:13 -040051echo "Untarring and removing $2.tar.gz"
52tar -xf $2.tar.gz --strip-components $snum -C $2
Tim Edwards6ee11532021-02-11 12:29:33 -050053rm $2.tar.gz
54exit 0