#!/bin/sh
# Extract details of declared hero Twitter video player from directives.
#   $1 must be the .*.HTML body source or some head portion of it.
#   $2 must be the name of the host .*.html body source for error messages.
if [ $# -lt 2 ]; then
    echo "Missing hero/player HTML input and source name." 1>&2
    exit 2
fi

# Returns off-site embeddable player URL width height uploadDate
# as four space-separated tokens, eg:
#     https://www.youtube.com/embed/QGY4NLH-29g 854 400 2011-11-31 PT10.5s

# <!-- TWPLAYER ... --> tags with player URL, width and height, eg;
#     <!-- TWPLAYER https://www.youtube.com/embed/QGY4NLH-29g 854 400 PT10s -->
# or:
#     <!-- TWPLAYER https://www.youtube.com/embed/xb0U2RjxRHI 560 315 2007-01-15 PT4.238S -->
#
#  1) URL to embedded player
#  2) width
#  3) height
#  4) upload/creation date
#  5) duration as ISO 8601 period
#
# Player URL should be https.

# File to read the input HTML file (directives) from.
INPUTHTML=$1

TWPLAYER=`sed < $INPUTHTML -n -e 's/^ *<!-- *TWPLAYER *\(https:\/\/[^ "]*\) *\([1-9][0-9]*\) *\([1-9][0-9]*\) *\([1-2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\) *\(PT[0-9][.0-9DHMS]*\) *--> *$/\1 \2 \3 \4 \5/p'`
if [ "X" != "X$TWPLAYER" ]; then
    echo "$TWPLAYER"
    exit 0
fi

if egrep < $INPUTHTML -q '^ *<!-- *TWPLAYER'; then
    echo "ERROR: mangled TWPLAYER hero/video player directive for $2..." 1>&2
    exit 1
fi

# Hero Twitter player not found.
exit 0
