#!/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}

EXTRACT_DIR=${EXTRACT_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 "REPLACE=${REPLACE}"
  echo "VERBOSE=${VERBOSE}"

  echo "EXTRACT_DIR=${EXTRACT_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
  elif [ "$filename" != "${filename%.tar.xz}" ]; then
    extname=.tar.xz
  else
    stderr extension of $filename is not supported
    exit 1
  fi

  target_dir=`expand_path "$EXTRACT_DIR"`
  mkdir -p "$target_dir"
  sh cd "$target_dir"
 
  [ "$REPLACE" -ne 1 ] && tarargs="--skip-old-files" || tarargs=""
  case "$extname" in
    .tar.gz|.tgz)    
      sh tar "$tarargs" -xzvf "$cache_file" || sh tar -xzvf "$cache_file" 
    ;;
    .tar.bz2)
      sh tar "$tarargs" -xjvf "$cache_file" || sh tar -xjvf "$cache_file"
    ;;
    .tar.xz)
      sh tar "$tarargs" -xJvf "$cache_file" || sh tar -xJvf "$cache_file"
    ;;
    *)
      stderr BUG, this should not happen
      exit 1
    ;;
  esac
fi

