#!/bin/sh -e
#
# archiveorg - search <https://archive.org/> snapshots of an URL

if test "$1" = ""; then
  printf '%s\n' "Usage: $(basename "$0") <url>" >&2
  exit 1
fi

curl -s -X GET "https://web.archive.org/cdx/search/cdx?url=$1" \
  | sort -k2,2rn -t ' ' \
  | LC_ALL=C awk -F ' ' '{
  ts=$2
  url=$3
  wurl="https://web.archive.org/web/" ts "/" url;

  fts = substr(ts, 1, 4) "-" substr(ts, 5, 2) "-" substr(ts, 7, 2);
  print fts ": " wurl;
}'
