blob: a490c9bf5d27c0fb71beed4a504c59ae2410be31 [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#
Tim Edwards8877f8f2021-06-10 21:35:36 -040011# <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 Edwards6ee11532021-02-11 12:29:33 -050014# <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 Edwardscdfec5e2021-04-22 20:59:13 -040018# <strip> is the number of directory levels to strip off the front of the
Tim Edwards8877f8f2021-06-10 21:35:36 -040019# tarball contents. Defaults to 1 if not specified (only
20# applicable if <url> points to a tarball).
Tim Edwards6ee11532021-02-11 12:29:33 -050021#
22
Tim Edwards8877f8f2021-06-10 21:35:36 -040023# 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 Edwardsdebc0a42020-12-28 22:11:40 -050026
Donnf41154c2021-06-22 13:10:06 +020027set -e
28
29if [ "${1: -3}" == ".gz"; ] then
Tim Edwardsdebc0a42020-12-28 22:11:40 -050030
Tim Edwards8877f8f2021-06-10 21:35:36 -040031 # 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 Edwardsdebc0a42020-12-28 22:11:40 -050033
Tim Edwards8877f8f2021-06-10 21:35:36 -040034 DL_CMD=
35 if type "wget" > /dev/null; then
36 DL_CMD="wget -qO"
37 fi
Tim Edwardsdebc0a42020-12-28 22:11:40 -050038
Tim Edwards8877f8f2021-06-10 21:35:36 -040039 if type "curl" > /dev/null; then
40 DL_CMD="curl -sLo"
41 fi
Tim Edwards6ee11532021-02-11 12:29:33 -050042
Tim Edwards8877f8f2021-06-10 21:35:36 -040043 if [ "$DL_CMD" = "" ]; then
44 echo "ERROR: Either curl or wget are required to automatically install tools."
45 exit 1
46 fi
Tim Edwards6ee11532021-02-11 12:29:33 -050047
Tim Edwards8877f8f2021-06-10 21:35:36 -040048 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 Edwardscdfec5e2021-04-22 20:59:13 -040066else
Tim Edwards8877f8f2021-06-10 21:35:36 -040067
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 Edwardscdfec5e2021-04-22 20:59:13 -040075fi
76
Tim Edwards6ee11532021-02-11 12:29:33 -050077exit 0