Created
January 7, 2013 00:46
-
-
Save swooningfish/4471398 to your computer and use it in GitHub Desktop.
Small utility to fetch and unpack archives on the web (with cache)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Small utility to fetch and unpack archives on the web (with cache) | |
# | |
# Depends on : curl, tar | |
# | |
set -e | |
set +u | |
# ENV vars, inherited from external | |
CACHE=${CACHE:-1} | |
UNPACK=${UNPACK:-1} | |
VERBOSE=${VERBOSE:-0} | |
TARGET_DIR=${TARGET_DIR:-`pwd`} | |
if [ -n "$HOME" ]; then | |
CACHE_DIR=${CACHE_DIR:-$HOME/.cache/fetchurl} | |
else | |
CACHE_DIR=${CACHE_DIR:-} | |
fi | |
TMP_DIR=${TMP_DIR:-/tmp} | |
URL=$1 | |
set -u | |
stderr () { | |
echo $@ 1>&2 | |
} | |
sh () { | |
echo $ $@ | |
if [ "$VERBOSE" -ne 0 ]; then | |
$@ | |
else | |
$@ >/dev/null 2>&1 | |
fi | |
} | |
expand_path() { | |
here=`pwd` | |
cd $1 | |
echo `pwd -P` | |
cd "$here" | |
} | |
usage() { | |
echo "Usage: fetchurl url" | |
echo "CACHE=${CACHE}" | |
echo "UNPACK=${UNPACK}" | |
echo "VERBOSE=${VERBOSE}" | |
echo "TARGET_DIR=${TARGET_DIR}" | |
echo "CACHE_DIR=${CACHE_DIR}" | |
echo "TMP_DIR=${TMP_DIR}" | |
echo "URL=${URL}" | |
exit 1 | |
} | |
if [ -z "$URL" ]; then | |
stderr "ERROR: missing url" | |
usage | |
fi | |
if [ -z "$CACHE_DIR" ] && [ "$CACHE" -ne 0 ]; then | |
stderr "ERROR: missing cache dir" | |
usage | |
fi | |
filename=`basename "$URL" | sed 's/\?.*//'` | |
tmp_file="$TMP_DIR/$filename" | |
cache_file="$CACHE_DIR/$filename" | |
mkdir -p "$CACHE_DIR" | |
# Fetch | |
if [ "$CACHE" -eq 0 ] || [ ! -f "$cache_file" ]; then | |
rm -rf "$tmp_file" | |
sh curl -L -o "$tmp_file" "$URL" | |
sh mv "$tmp_file" "$cache_file" | |
fi | |
# TODO: checksums | |
# Unpack | |
if [ "$UNPACK" -ne 0 ]; then | |
if [ "$filename" != "${filename%.tar.gz}" ]; then | |
extname=.tar.gz | |
elif [ "$filename" != "${filename%.tgz}" ]; then | |
extname=.tgz | |
elif [ "$filename" != "${filename%.tar.bz2}" ]; then | |
extname=.tar.bz2 | |
else | |
stderr extension of $filename is not supported | |
exit 1 | |
fi | |
target_dir=`expand_path "$TARGET_DIR"` | |
mkdir -p "$target_dir" | |
sh cd "$target_dir" | |
case "$extname" in | |
.tar.gz|.tgz) | |
sh tar xzvf "$cache_file" | |
;; | |
.tar.bz2) | |
sh tar xjvf "$cache_file" | |
;; | |
*) | |
stderr BUG, this should not happen | |
exit 1 | |
;; | |
esac | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment