#!/bin/sh
#
# Quick to do sync only the /etc (most) changed entry to permanent storage.
#
# Rick van der Zwet <rick@wirelessleiden.nl>
#

: ${VERBOSE=false}

usage() {
  cat <<EOF
Usage: $0 [-v] <dryrun|sync>
Options:
  -v     - Verbose diffs
Arguments:
  dryrun - Check the pending changes
  sync   - Sync the pending changes
EOF
  exit ${1:-128}
}

while [ -n "$1" ]; do
  case "$1" in
    "-v")
      VERBOSE=true; shift
      ;;
    "dryrun")
      SYNC=false; shift
      ;;
    "sync")
      SYNC=true; shift
      ;;
    *)
      echo "Invalid Argument -- $1"
      usage
      ;;
   esac
done
[ -z "$SYNC" ] && usage

trap "umount /cfg; mount -ur /" 1 2 15 EXIT
mount -r /cfg
$SYNC && mount -uwo noatime /

for file in `find /conf/base/etc -type f`; do
  live_file=${file##/conf/base}
  conf_file=/cfg${file##/conf/base/etc}

  # Do not sync files stored on the config mount
  [ -r $conf_file ] && continue
  
  if ! cmp $file $live_file; then
    $VERBOSE && diff $file $live_file
    $SYNC && cp -v $live_file $file
  fi
done

umount /cfg
mount -ur /
trap 1 2 15 EXIT
