#!/bin/csh -f # diffquick # ----------------------------------------------------------------------------- # Shell script to compare the specified file with the recent versions of it # in my backup directories. # ----------------------------------------------------------------------------- # Usage: See Usage section below or run with -h or --help to see usage. # Assumptions: # Effects: # - Compares file # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- # Collect command line options set option_quick = "false" set option_interactive = "false" set option_all = "false" while ($#argv > 0) if ("$1:q" == "-h" || "$1:q" == "--help") then echo "Usage: $0:t [options] filename" echo "Options:" echo " -i = Interactive. Use windiff instead of diff or asking." echo " -q = Quick. Use diff instead of windiff or asking." echo " Overrides -i option." echo " -a = All. Show diffs for all versions w/o prompting or pausing." echo " Implies -q option. Overrides -i option." exit 1 else if ("$1:q" == "-i") then shift if ("$option_quick" == "true") then echo "${0:t}: Option -i overridden by option -q" else if ("$option_all" == "true") then echo "${0:t}: Option -i overridden by option -a" else set option_interactive = "true" endif else if ("$1:q" == "-q") then shift set option_quick = "true" if ("$option_interactive" == "true") then echo "${0:t}: Option -i overridden by option -q" endif set option_interactive = "false" else if ("$1:q" == "-a") then shift set option_all = "true" set option_quick = "true" if ("$option_interactive" == "true") then echo "${0:t}: Option -i overridden by option -a" endif set option_interactive = "false" else if ("-" == "`echo $1:q | cut -c 1`") then echo "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first parameter break endif end set diff_cmd = "diffreview" if ("$option_interactive" == "true") then set diff_cmd = "${diff_cmd} -i" else if ("${option_quick}" == "true") then # Note: Use diffreview here also, not just plain "diff -s". # Gains all the secondary advantages of diffreview, even though # we use the -d option to suppress the primary feature of # diffreview which is to prompt the user for whether to use an # interactive diff. For example, diffreview echos the diff # command it executes, so we don't need to use "set echo" below. set diff_cmd = "${diff_cmd} -d" endif #set local_file = "$PWD/$1:q" # Modeled after the remotepub script. See notes there for why pwd -P is used. set local_dir = "`pwd -P | normalize_filename`" set local_file = "${local_dir}/$1:q" # Get list of backup dirs from local /var/Quick tree and if USB drive is # plugged in, additional backup dirs from /Volumes/SEAGATE2TB/Quick tree. set timestamp = `date "+%Y_%m_%d__%H_%M_%S"` set backup_dirs = "/tmp/$0:t_$$_${timestamp}" (cd /var/Quick; ls > ${backup_dirs:q}) if (-e /Volumes/SEAGATE2TB) then (cd /Volumes/SEAGATE2TB/Quick; ls >> ${backup_dirs:q}) else echo "Skipping USB drive. Not plugged in." endif # Compare each pair of backups over time set newer_file = "${local_file}" foreach backup_dir (`sort -r ${backup_dirs:q} | uniq`) set relative_file = "`echo ${local_file:q} | sed -e 's*${HOME}/**'`" if (-e "/var/Quick/${backup_dir}") then set backup_dir = "/var/Quick/${backup_dir}" else if (-e "/Volumes/SEAGATE2TB/Quick/${backup_dir}") then set backup_dir = "/Volumes/SEAGATE2TB/Quick/${backup_dir}" else beep "Unexpected error: ${backup_dir:q} not found locally or on USB." rm ${backup_dirs:q} exit 1 endif set older_file = "${backup_dir}/${relative_file}" if (-e ${older_file:q}) then if ("${backup_dir}" == "/var/Quick/full") then set do_it = "y" else if ("${option_all}" == "true") then set do_it = "y" else # Prompt the user for whether to compare this pair of backups. set do_it = `promptloop "Compare with ${backup_dir} (y/n)? " y n` endif if ("${do_it}" == "y") then # Note: No need to use "set echo" here, as: # (set echo; ${diff_cmd} "${newer_file}" "${older_file}") # because diffreview does that internally. ${diff_cmd} "${newer_file}" "${older_file}" # Pause if any diffs. Otherwise, the diffs may scroll by # without the user getting to see them as we loop through lots # of subsequent pairs of backups that didn't backup the file # being diffed, and so do not prompt above for whether to # compare. # Note: Status value depends on $diff_cmd. # diff returns 1 if diffs. # diffreview may return 1 or 3 if diffs if ($status != 0 && "$option_all" == "false") then pause endif set newer_file = "${older_file}" else break endif else echo "$1:q not found in ${backup_dir}" endif end rm ${backup_dirs:q} #??# Compare latest file against backups over time #??foreach backup_dir (`ls -r /var/Quick`) #?? set relative_file = "`echo ${local_file} | sed -e 's*${HOME}/**'`" #?? set backup_file = "/var/Quick/${backup_dir}/${relative_file}" #?? if (-e ${backup_file}) then #?? if (${backup_dir} == "full") then #?? set do_it = "y" #?? else #?? set do_it = `promptloop "Compare with ${backup_dir} (y/n)? " y n` #?? endif #?? if ("${do_it}" == "y") then #?? diffreview "${local_file}" "${backup_file}" #?? else #?? exit 0 #?? endif #?? else #?? echo "$1 not found in /var/Quick/${backup_dir}" #?? endif #??end