#!/bin/bash #---------------------------------------------------------------------- # Called when system configuration changes and also at reboot. # # Detects when location has changed. # # Selects appropriate Retrospect configuration # file, then restarts Retrospect daemon. # # Parameter 1 should be either 'reboot' or 'location changed'. # #---------------------------------------------------------------------- # This file (update_retrospect_config) is a Unix shell script. It must # be saved with Unix line endings, and must have execute permissions. # It can be located anywhere, but for several reasons the suggested # location is /usr/local/bin (create this directory if it doesn't # exist). It relies on two additional files to call it, both of which # should be placed in the /Library/LaunchDaemons/ directory: # update_retrospect_config.plist # update_retrospect_config_reboot.plist # # The two .plist files in the LaunchDaemons directory instruct launchd # (part of OS X Tiger) to invoke them whenever the system configuration # changes (first file) or the system boots up (second file). Each of # these files then calls this script with a parameter indicating why it # was called. # # This script examines the current location, compares it to the location # when this file was last called, and if different, figures out which # Retrospect configuration file should be made active. It copies the # current configuration file back where it came from, and copies the # desired one as the current one, and restarts the Retrospect daemon. #---------------------------------------------------------------------- # # Version 2.0 and later use OS X's extended attribute functionality # to save the current configuration file's source, using the handy # 'xattr' utility from # (see Ars Technica article ) # # For this to work, you need 'xattr' installed somewhere (I suggest # /usr/local/bin, which is where this script looks for it). You also # need to run it once on each saved Retrospect configuration file, to # store its full path in a new 'randy_FileWhence' extended attribute. # # For example, if you have two saved configuration files, # retroclient.state.qc # retroclient.state.home # # Then run the following commands: # /usr/local/bin/xattr --set "randy_FileWhence" # "/Library/Preferences/retroclient.state.qc" # /Library/Preferences/retroclient.state.qc # # /usr/local/bin/xattr --set "randy_FileWhence" # "/Library/Preferences/retroclient.state.home" # /Library/Preferences/retroclient.state.home # #---------------------------------------------------------------------- # Customization: # # Look for the line 'case $LOCATION in' below. Following this line are # the specific names or partial names of locations (in my version of the # file these are 'Pensive*' and 'QC*'). These match the computer # network Location name that you've created in Network Preferences. # # Following each network name is a line that assigns the variable # 'RetroConfigSrc' to a file name, such as: # '/Library/Preferences/retroclient.state.qc' # This file name is the name of the specific Retrospect coniguration # file to be used for this location. To create this file, install # Retrospect as usual, then copy the file # '/Library/Preferences/retroclient.state' as this file, then # run the 'xattr' utility to set the 'randy_FileWhence' attribute to # the file name (as described above). # # So, if you want to have two Retrospect configurations, one for work # and one for home: # 1: Set your Location to one of them, say work. # 2: Uninstall Retrospect, then re-install Retrospect. # 3: Copy the default Retrospect conifguration file (as above) to # a name specific to this location. # 4: Run the 'xattr' utility to set the 'randy_FileWhence' attribute to # the file name (as described above). # 5: Repeat for each location. # # #----------------------------------------------- # Version 2.1 # February 5, 2007 # Randall Gellens #----------------------------------------------- #----------------------------------------------- # Timestamped messages #----------------------------------------------- function msg () { ts=`date +"%Y-%m-%d %H:%M:%S"` echo "$ts $1" } #----------------------------------------------- # main script #----------------------------------------------- # Identify ourselves and list first parameter, which # indicates if we were launched on reboot or because # system configuration changed (for documentation in log). X=`/usr/bin/basename $0` msg "$X $1" # Get current location if [ -x /usr/local/bin/usleep ]; then /usr/local/bin/usleep 250000 # make sure location is stable else /bin/sleep 1 fi LOCATION=`echo show Setup:/ | scutil | awk -F ": " '/UserDefinedName/{print $2}'` # get PID of Retrospect daemon RETROSPECT=`ps auxww | grep -i pitond | fgrep -v "grep -i $1" | fgrep -v "fgrep" | fgrep -v "pfind" | awk '{print $2}'` # Static file locations RetroConfigDst="/Library/Preferences/retroclient.state" LocationSaveFile="/tmp/last_location" # If we have a saved location, extract it if [ -f "$LocationSaveFile" ] ; then PREV_LOCATION=`cat "$LocationSaveFile"` msg "Last location: $PREV_LOCATION" else msg "No previous saved location" fi # If called because location may have changed, see if it did if [ "$1" != "reboot" ] ; then if [ "$LOCATION" != "$PREV_LOCATION" ] ; then msg "Location changed from $PREV_LOCATION" else msg "Location unchanged from $PREV_LOCATION" exit 0 fi else msg "Called on boot, not location change" fi if [ -n "$RETROSPECT" ] ; then msg "Retrospect daemon is running: pid $RETROSPECT" else msg "Retrospect daemon not running" fi msg "Current location is \"$LOCATION\"" case $LOCATION in Pensive*) RetroConfigSrc="/Library/Preferences/retroclient.state.pensive" msg "Location is at home; will use $RetroConfigSrc" ;; QC*) RetroConfigSrc="/Library/Preferences/retroclient.state.qc" msg "Location is at work; will use $RetroConfigSrc" ;; *) msg "Location isn't home nor work" exit 0 ;; esac # stop Retrospect if it is running /sbin/SystemStarter stop RetroClient # Be sure Retrospect is stopped RETROSPECT=`ps auxww | grep -i pitond | fgrep -v "grep -i $1" | fgrep -v "fgrep" | fgrep -v "pfind" | awk '{print $2}'` if [ -n "$RETROSPECT" ] ; then msg "Retrospect daemon is still running: pid $RETROSPECT" kill -TERM $RETROSPECT fi # Copy current Retrospect configuration file from whence it came WHENCE=`/usr/local/bin/xattr --get-parseable "randy_FileWhence" $RetroConfigDst | fgrep "randy_FileWhence" | awk -F "=" '{print $2}'` if [ -n "$WHENCE" ] ; then msg "Current config file came from: $WHENCE" cp $RetroConfigDst $WHENCE msg "Copied current config file back to $WHENCE" else msg "Current config file randy_FileWhence not set; not copied back" fi # Copy desired Retrospect configuration file if [ -f "$RetroConfigSrc" ] ; then cp $RetroConfigSrc $RetroConfigDst msg "Copied Retrospect Config ($RetroConfigSrc)" else msg "No such file: $RetroConfigSrc" exit 1 fi # Restart Retrosoect /sbin/SystemStarter start RetroClient RETROSPECT=`ps auxww | grep -i pitond | fgrep -v "grep -i $1" | fgrep -v "fgrep" | fgrep -v "pfind" | awk '{print $2}'` if [ -n "$RETROSPECT" ] ; then msg "Retrospect daemon restarted: pid $RETROSPECT" else msg "Retrospect daemon not started" exit 1 fi # Save location for next time echo "$LOCATION" > "$LocationSaveFile" exit 0