#!/bin/bash

set -eux

PATH=~/bin:$PATH

pushd `dirname $0`

curl -sSL http://www.colbertnation.com/full-episodes/ > full.txt

xsltproc \
--stringparam site 'http://www.colbertnation.com/' \
--html full2id.xsl full.txt 2> /dev/null > frags.txt


cat frags.txt | xargs curl -sS > raw.txt
         
# Clean up any malformed HTML
#xmllint --html --nowarning --recover raw.txt > clean.txt 2> /dev/null || true

xsltproc \
--stringparam feed-title 'The Colbert Report Full Episode RSS Feed' \
--stringparam feed-url http://extechops.net/full-episode-feeds/colbert-report.rss \
--stringparam showtime '23:30 EST' \
--stringparam month-offset 48 \
--html colbert-raw2rss.xsl raw.txt 2> /dev/null  | \
sed -e 's/<content:encoded>/<content:encoded><![CDATA[/g' \
    -e 's/<\/content:encoded>/]]><\/content:encoded>/g' \
    > colbert-report.rss

curl -sSL http://www.thedailyshow.com/full-episodes/ > full.txt

xsltproc \
--html full2id.xsl full.txt 2> /dev/null > frags.txt


cat frags.txt | xargs curl -sS > raw.txt

xsltproc \
--stringparam feed-title 'The Daily Show Full Episode RSS Feed' \
--stringparam feed-url http://extechops.net/full-episode-feeds/daily-show.rss \
--stringparam showtime '23:00 EST' \
--stringparam month-offset 47 \
--html daily-raw2rss.xsl raw.txt 2> /dev/null | \
sed -e 's/<content:encoded>/<content:encoded><![CDATA[/g' \
    -e 's/<\/content:encoded>/]]><\/content:encoded>/g' \
    > daily-show.rss


#rm raw.txt

popd
