#!/bin/sh -e
#
# wt - wget & tar
#
# Usage: wt 'https://example.com/blah.blah.tar.gz' [keep]
# If [keep] is presented, whatever it is, the tarball will be kept.
#
# (opt) dtrx: http://brettcsmith.org/2007/dtrx/
# ref: https://blog.yjl.im/2011/08/simplifying-wget-and-and-tar-and-and-cd.html

wt() {
  [ "$#" -eq 0 ] && return
  URL="$1"
  keep="$2"
  filename="$(basename "${URL}")"
  wget "${URL}" -O "${filename}"
  if type dtrx >/dev/null 2>&1; then
    dtrx -n "${filename}"
  else
    tar xf "${filename}"
  fi
  [ -z "${keep}" ] && rm "${filename}"
  # Guessing the directory
  cd "${filename%%.[a-z]*}"*
}
