#!/bin/bash

# Author: Dean Wilson
# Created on: 2006/05/04
# Version: 0.1
# License: GPL - Do what you want
# Last Updated: 2006/05/04

# this script takes a url on the command line, downloads it (using wget)
# and then runs it through html tidy to show any warnings or errors. No
# temporary files are left on the system.

# if you pass in '-h' as the first argument it prints the url it will
# check. This was helpful for batch processing.

binaries=$(cat<<all_required_binaries
  basename
  mktemp
  tidy
  wget
all_required_binaries)

# app settings
APPNAME=$(basename $0)

#################################
# sanity check to ensure all binaries are present.
#################################

# sanity checks.
for required_binary in $binaries; do
  which $required_binary > /dev/null
  if [ "$?" != '0' ];then
    printf "$APPNAME: No usable '$required_binary' in '$PATH'\n"
    exit 1
  fi
done

#################################
# Command Line Processing
#################################

# show the url we're working on if asked to.

if [ $# -eq 2 ];then
  if [ "$1" = "-h" ];then
    showheader=1
  else
    echo "usage: $APPNAME [-h] url"
    exit 1
  fi
  url=$2
else
  showheader=0
  url=$1
fi

#################################
# Do the actual work
#################################

if [ -z "$url" ];then
  printf "$APPNAME: Please supply a URL to check...\n"
  exit 2
fi

# get the handle to use to hold the results
TMPFILE=$(mktemp -q /tmp/$APPNAME.XXXXXX)

if [ $? -ne 0 ]; then
  echo "$APPNAME: Can't create temp file, exiting..."
  exit 3
fi

# download the remote url
wget -q -O $TMPFILE $url

if [ $? -ne 0 ]; then
  echo "$APPNAME: wget couldn't get '$url'"
  rm $TMPFILE
  exit 4
fi

# see if it's valid
if [ "$showheader" -eq 1 ];then
  echo "Checking '$url'"
fi
tidy -q $TMPFILE  >/dev/null

# clean up on exit.
rm $TMPFILE
