#!/bin/bash ROOTDIR="${HOME}/.w3watch" CACHEDIR="${ROOTDIR}/cache" LOCKFILE="${ROOTDIR}/lock" CONFIGFILE="${ROOTDIR}/config" CONFIGSAMPLE='/usr/share/doc/w3watch/config.sample' if [ ! -d "${ROOTDIR}" ]; then install -D "${CONFIGSAMPLE}" "${CONFIGFILE}" mkdir -p "${CACHEDIR}" eval "${EDITOR} ${CONFIGFILE}" exit $? fi if [ ! -r "${CONFIGFILE}" ]; then echo "${CONFIGFILE} not found!" exit 1 fi if [ ! -d "${CACHEDIR}" ]; then mkdir -p "${CACHEDIR}" fi if [ -r "${LOCKFILE}" ]; then echo "w3watch is locked by ${LOCKFILE}" exit 1 fi if [ "${1}" == "e" ]; then eval "${EDITOR} ${CONFIGFILE}" exit $? fi if [ "${1}" == "l" ]; then cat "${CONFIGFILE}" exit $? fi if [ "${1}" == "d" ]; then DUMP=true else DUMP=false if [ "${1}" != "c" ]; then echo "Usage: ${0} [e|l|d|c]" echo -e "\te:\tedit config" echo -e "\tl:\tlist config" echo -e "\td:\tdump output and discard any changes" echo -e "\tc:\tcheck for updates" exit -1 fi fi touch "${LOCKFILE}" while read line; do if echo "${line}" | grep -q -v '^#'; then data=(${line}) url="${data[0]}" filter="${data[@]:1}" dump=$(lynx -dump "$url") if [ $? -ne 0 ]; then rm -f "${LOCKFILE}" exit 1 fi if [ "${filter}" != "" ]; then dump=$(echo "${dump}" | eval "${filter}") fi if $DUMP; then echo "$dump" else sum=$(echo "${line}" | sha1sum | awk '{print $1;}') cachefile="${CACHEDIR}/${sum}" if [ -f "$cachefile" ]; then echo "$dump" | diff -u \ --label "local copy from $(/usr/bin/stat --printf='%y' ${cachefile})" "$cachefile" \ --label "${url}" - \ || echo fi echo "$dump" > "$cachefile" fi fi done < "${CONFIGFILE}" # remove old cache entries for cacheEntry in "${CACHEDIR}/"*; do cacheSum=$(basename "$cacheEntry") while read configEntry; do if echo "${configEntry}" | grep -q -v '^#'; then configSum=$(echo "$configEntry" | sha1sum | awk '{print $1;}') if [ "$cacheSum" == "$configSum" ]; then continue 2 fi fi done < "${CONFIGFILE}" rm -f $cacheEntry done rm -f touch "${LOCKFILE}"