#!/bin/sh -ue # # Add Google Analytics code to web pages if missing. # - Cameron Simpson 16jul2009 # cmd=`basename "$0"` usage="Usage: $cmd -g ganalytics.txt [-s] htmlfiles... -g ganalytics.txt Text file containing the Google Analytics HTML code. -s Silent. Disables tracing and is also passed to bsed." txtfile= trace=set-x bsedopts= badopts= while [ $# -gt 0 ] do case "$1" in -g) txtfile=$2; shift ;; -s) trace= bsedopts="$bsedopts $1" ;; --) shift; break ;; -?*)echo "$cmd: unrecognised option: $1" >&2 badopts=1 ;; *) break ;; esac shift done if [ -z "$txtfile" ] then echo "$cmd: missing -g ganalytics.txt" >&2 badopts=1 else if [ ! -f "$txtfile" ] then echo "$cmd: -g $txtfile: not a regular file" >&2 badopts=1 else urchin=`sed -n 's/.* pageTracker = _gat._getTracker("\(UA-[^"]*\)").*/\1/p' <"$txtfile"` if [ -n "$urchin" ] then sedf='/< *\/ *[Bb][Oo][Dd][Yy] *>/i\ '`sed '$!s/$/\\\\/' "$txtfile"` else echo "$cmd: $txtfile: can't local urchin string" >&2 badopts=1 fi fi fi if [ $# = 0 ] then echo "$cmd: missing htmlfiles" >&2 badopts=1 fi [ $badopts ] && { echo "$usage" >&2; exit 2; } xit=0 for htmlfile do [ -s "$htmlfile" ] \ || { echo "$cmd: $htmlfile: not a file" >&2 xit=1 continue } fgrep -i '' <"$htmlfile" >/dev/null \ || { echo "$cmd: $htmlfile: no , skipped" >&2 xit=1 continue } fgrep "_getTracker(\"$urchin\")" <"$htmlfile" >/dev/null \ && { echo "$cmd: $htmlfile: already has urchin" >&2 continue } $trace bsed $bsedopts "$sedf" "$htmlfile" done exit $xit