#!/usr/bin/env bash # guri - Get HTTP URIs from the Web # Usage: ./guri # License: GPL 2; share and enjoy! # Author: Sean B. Palmer, inamidst.com # Requirements: # @@ curl # http://inamidst.com/util/uriquote # Todo: @@ --help and usage curl=$(which curl) if [ $? != 0 ]; then echo "Couldn't find curl." >&2 exit 1 fi uri=$1 # Test that it's an HTTP URI case $uri in http://*) path=${uri#http://};; *) echo "Invalid HTTP URI: $uri" >&2; exit 1;; esac path=$(uriquote $path) # Split the path into directory and filename dir=${path%/*} fn=${path##*/} fn=${fn:-'index'} if [ ! "$GURI_OUTDIR" ]; then echo 'Sorry, you must set $GURI_OUTDIR first.' >&2 echo 'For example: export GURI_OUTDIR=~/http' >&2 exit 1 fi output=$GURI_OUTDIR/$dir/$fn if [ -f $output ]; then echo "@@ Already downloaded $uri" >&2 # @@ need to check mtime mtime=$(stat -c %Y $output) lastmod=$(curl -I -A 'Mozilla/5.0 (guri)' $uri | egrep '^Last-Modified') if [ $? != 0 ]; then echo "Couldn't get the Last-Modified date." >&2 exit 1 fi lastmod=$(date -d "${lastmod#*: }" +%s) if [ $? != 0 ]; then exit 1; fi echo Last-Modified: $lastmod if (("$lastmod" < "$mtime" )); then echo "Local file is more recent! Strange." >&2 elif (("$lastmod" > "$mtime")); then echo "Remote file is more recent. @@ Offer update." >&2 else echo "File modification time hasn't changed anyway." >&2 fi exit 1 fi if [ ! -d $GURI_OUTDIR/$dir ]; then mkdir -p $GURI_OUTDIR/$dir fi echo "Downloading using curl..." >&2 curl -R -A 'Mozilla/5.0 (guri)' -o $output $uri echo $output