#! /bin/sh # This script uses rsync to create an rsync batch file to re-create # the first argument out of the remaining arguments. It also creates # a shell script to re-create the file. If --verify is the first # argument, then the resulting rsync file will be verified to be # usable to re-create the original file. # Copyright 2005 Alexandre Oliva # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA verify=false case $1 in --verify) verify=: shift ;; esac target=$1 shift || exit 1 if test $# = 0; then echo no input files for the rsync batch >&2 fi if test ! -f "$target"; then echo "$target" does not exist >&2 exit 1 fi if test -f "$target".orig; then echo "$target".orig already exists >&2 exit 1 fi if test -f "$target".new; then echo "$target".new already exists >&2 exit 1 fi if test -f "$target".rsync; then echo "$target".rsync already exists >&2 exit 1 fi set -e if test -f "$target".cat; then echo "$target".cat already exists, assuming it is correct >&2 else case $# in 1) ln "$@" "$target".cat ;; *) cat "$@" > "$target".cat ;; esac fi ln "$target".cat "$target".new rsync -av --progress --write-batch="$target".rsync "$target" "$target".new bzip2 -9 < "$target".rsync > "$target".rsync.bz2 rm -f "$target".new "$target".rsync.sh "$target".cat "$target".rsync cat > "$target".sh << EOF #! /bin/sh set -e cd \`dirname \$0\` target=\`basename \$0 .sh\` if test -f "\$target"; then echo "\$target" already exists, entering test mode >&2 fi if test -f "\$target".new; then echo "\$target".new already exists >&2 exit 1 fi if test -f "\$target".cat; then echo using existing "\$target".cat, will remove at the end >&2 else EOF case $# in 1) echo " ln \"$@\" \"\$target\".cat" ;; *) echo " cat" "$@" \> \"\$target\".cat ;; esac >> "$target".sh cat >> "$target".sh <&2 else status=$? echo cmp "\$target".new "\$target" failed >&2 fi rm -f "\$target".new else mv "\$target".new "\$target" fi rm -f "\$target".cat exit $status EOF chmod +x "$target".sh echo "$target".sh and "$target".rsync.bz2 created successfully >&2 if $verify; then mv "$target" "$target".orig echo "$target" renamed to "$target".orig if sh "$target".sh && cmp "$target".orig "$target"; then status=0 else echo "$target".rsync.bz2 appears to be bad >&2 status=1 fi else status=0 fi exit $status