diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2009-08-26 23:50:56 +0200 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2009-08-26 23:50:56 +0200 |
commit | 20abe65e17fbea99795af2d9d5015db3650abc31 (patch) | |
tree | 41e021f7d77070918f07aa175aaffd8869c2d265 | |
parent | 8ec5e93d309f6a25789881db3f414d16dd017cd9 (diff) | |
download | w3watch-20abe65e17fbea99795af2d9d5015db3650abc31.tar.gz w3watch-20abe65e17fbea99795af2d9d5015db3650abc31.tar.xz |
move logic into functions and make use of getopts
-rwxr-xr-x | w3watch | 156 |
1 files changed, 83 insertions, 73 deletions
@@ -1,17 +1,15 @@ #!/bin/bash +DRYRUN=false 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 @@ -28,90 +26,102 @@ if [ -r "${LOCKFILE}" ]; then exit 1 fi -if [ "${1}" == "e" ]; then - eval "${EDITOR} ${CONFIGFILE}" - exit $? -fi +touch "${LOCKFILE}" -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 +quit() { + rm -f "${LOCKFILE}" + exit $1 +} + +usage() { + echo "usage: ${0} [option]" + echo 'options:' + echo ' -e edit config' + echo ' -l list config' + echo ' -d dump output and discard any changes' + echo ' -c check for updates' + echo '' +} + +edit() { + ${EDITOR} "${CONFIGFILE}" + return $? +} + +list() { + grep -v '^#' "${CONFIGFILE}" +} + +check() { + while read line; do + if echo "${line}" | grep -q -v '^#'; then + data=(${line}) + url="${data[0]}" + filter="${data[@]:1}" + + if echo "$url}" | grep -q '^@'; then + url="$(echo "${url}" | cut -c 1 --complement -)" + dump=$(lynx -source "$url") + else + dump=$(lynx -dump "$url") + fi + if [ $? -ne 0 ]; then + quit 1 + fi -touch "${LOCKFILE}" + if [ "${filter}" != "" ]; then + dump=$(echo "${dump}" | eval "${filter}") + fi + if $DRYRUN; then + echo "$dump" + else + sum=$(echo "${line}" | sha1sum | awk '{print $1;}') + cachefile="${CACHEDIR}/${sum}" -while read line; do - if echo "${line}" | grep -q -v '^#'; then - data=(${line}) - url="${data[0]}" - filter="${data[@]:1}" + if [ -f "$cachefile" ]; then + echo "$dump" | diff -u \ + --label "local copy from $(/usr/bin/stat --printf='%y' ${cachefile})" "$cachefile" \ + --label "${url}" - \ + || echo + fi - if echo "$url}" | grep -q '^@'; then - url="$(echo "${url}" | cut -c 1 --complement -)" - dump=$(lynx -source "$url") - else - dump=$(lynx -dump "$url") + echo "$dump" > "$cachefile" + fi fi + done < "${CONFIGFILE}" +} - if [ $? -ne 0 ]; then - rm -f "${LOCKFILE}" - exit 1 - fi +collectGarbage() { + for cacheEntry in "${CACHEDIR}/"*; do + cacheSum=$(basename "$cacheEntry") - if [ "${filter}" != "" ]; then - dump=$(echo "${dump}" | eval "${filter}") - fi + while read configEntry; do + if echo "${configEntry}" | grep -q -v '^#'; then + configSum=$(echo "$configEntry" | sha1sum | awk '{print $1;}') - 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 + if [ "$cacheSum" == "$configSum" ]; then + continue 2 + fi fi + done < "${CONFIGFILE}" - echo "$dump" > "$cachefile" - fi - fi -done < "${CONFIGFILE}" - -# remove old cache entries -for cacheEntry in "${CACHEDIR}/"*; do - cacheSum=$(basename "$cacheEntry") + rm -f $cacheEntry + done +} - 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 +while getopts 'eldc' option; do + case $option in + e) edit; quit $?;; + l) list; quit 0;; + d) DRYRUN=true; check; quit 0;; + c) check; collectGarbage; quit 0;; + *) usage; quit -1;; + esac done -rm -f touch "${LOCKFILE}"
\ No newline at end of file +usage +quit -1 |