#!/bin/bash # # Download all ZWiki pages and push changes into # the subversion repository. # PROG="$(basename $0)" ## costumize these settings to suit your environment # URL to the Wiki frontpage; must have installed the # DTML method from file ``list_all_pages.dtml`` ZWIKI_FRONTPAGE_URL='http://www.egrid.it/doc/gme/FrontPage' ## other settings, not for customization # regular expression matching the way ZWiki reformats page titles ZWIKI_PAGENAME_REGEX='^[A-Z][0-9A-Za-z]$' # date for inclusion in the commit message DATE="$(date -R)" ## useful functions die () { local rc="$1"; shift echo -n "$PROG: " (if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2 exit $rc } have_command () { type -a "$1" >&/dev/null } require_command () { if ! have_command "$1"; then die 1 "Cannot find required command '$1' in the system PATH - aborting." fi } ## generic VCS operations vcs_is_checkout () { svn info >&/dev/null } vcs_register_file () { svn add "$1" } vcs_unregister_file () { svn rm --force "$1" } vcs_revert_file () { svn revert "$1" } vcs_commit_all () { local msg="$1"; shift svn commit -m "$msg" } ## web interface require_command curl download () { local url="$1" local dest="${2:-$(basename $url)}" curl --silent --fail --show-error "$url" -o "$dest" } ## main # check that current working directory is a checkout if ! vcs_is_checkout; then die 3 "Current directory '`pwd`' is not a repository checkout - aborting." fi # make a temporary file for holding page list require_command mktemp tmp="$(mktemp -t $PROG.XXXXXX)" \ || die 1 "Cannot make temporary file - aborting." # download page list PAGE_LIST_URL="${ZWIKI_FRONTPAGE_URL}/list_all_pages" download "$PAGE_LIST_URL" "$tmp" \ || die 2 "Error downloading page list from '$PAGE_LIST_URL' - aborting." # remove pages that no longer exist on the web from the repository. for page in $(ls -1 [A-Z]* | egrep "$ZWIKI_PAGENAME_REGEX"); do if ! egrep -q "^$page" "$tmp"; then vcs_unregister_file "$page" fi done # push web changes into the repository ZWIKI_BASE_URL="$(dirname $ZWIKI_FRONTPAGE_URL)" (while read page; do # check if page is already in the repository, # otherwise assume it has been created on the web if test -e "$page"; then page_already_in_repo=yes else page_already_in_repo=no fi if download "${ZWIKI_BASE_URL}/${page}/text" "$page"; then if [ "$page_already_in_repo" = no ]; then vcs_register_file "$page" fi else # download failed for some reason, # make sure that no changes to this page # are committed to the repository if [ "$page_already_in_repo" = yes ]; then vcs_revert_file "$page" fi fi done) <$tmp rm -f "$tmp" vcs_commit_all "snapshot by $PROG at $DATE"