repo: webmention-tools
action: blob
revision: 
path_from: webmention-discover.sh
revision_from: refs/heads/master:
path_to: 
revision_to: 
git.thebackupbox.net
webmention-tools
git clone git://git.thebackupbox.net/webmention-tools

blob of:

webmention-tools

/ webmention-discover.sh

blob_plain of this file

refs/heads/master:/webmention-discover.sh

 #!/usr/bin/env bash

 target="$1"


 temp="$(curl -LsI "$target" | egrep -i '^location:|^link:' | tr -d '\r' | while read line;do
   lower="$(printf "%s\n" "$line" | tr 'A-Z' 'a-z')"
   case "$lower" in
 location:*)
     relocation="$(printf "$line" | cut -d: -f2- | tr -d ' ')"
     echo "relocation: $relocation" >&2
     target="$(printf "<%s>\n" "$relocation" | uri-absolution "$target")"
     printf "location: %s\n" "$target"
   ;;
 link:*)
     if grep "webmention" <<< "$line" >/dev/null 2>/dev/null;then
       printf "%s\n" "$line"
     fi
   ;;
   esac
 done)"

 #echo "$temp"

 relocation="$(echo "$temp" | grep -i ^location: | tail -n1 | cut -d: -f2- | tr -d ' ')"
 if [ "$relocation" ];then
   target=$relocation
 fi

 line="$(echo "$temp" | grep -i ^link: | tail -n1 | cut -d: -f2- | tr ',' '\n' | egrep 'rel=webmention|rel="webmention' | tr -d ' ')"

 if [ "$line" ];then
 	uri="$(printf "%s\n" "$line" | tr '>' '<' | cut '-d<' -f2)"
 else
 ### of course you can't trust html to be xml, so this doesn't work a lot of the time.
 #	uri="$(curl -s "$target" | tee \
 #		>(xmlstarlet sel -t -v '//_:link[@rel="webmention"]/@href' -t -n) \
 #		>(xmlstarlet sel -t -v '//_:a[@rel="webmention"]/@href' -t -n) \
 #		>/dev/null )"
 #	echo "no link header!" >&2
 	uri="$(curl -s "$target" | tr -d '\n' | sed 's|||g' | tr '<' '\n' | egrep '^link|^a' | cut '-d>' -f1 | tr '\t' ' ' | tr -s ' ' | grep ' rel="webmention' | tr ' ' '\n' | grep ^href= | cut '-d"' -f2 | head -n1)"
 fi

 printf "<%s>\n" "${uri}" | uri-absolution "$target"