blob: aff588a85180cc180e5042f83419c064b5ec340e (
plain)
- #!/bin/sh
- set -e
- PRG=$(basename "$0")
- TEMP="`getopt -s sh -o t:f -l target:,force -n "$PRG" -- "$@"`"
- if [ $? != 0 ] ; then echo >&2 "ERROR: Internal getopt error." ; exit 1 ; fi
- eval set -- "$TEMP"
- realtargetdir=''
- force='no'
- while true ; do
- case "$1" in
- -t|--target) realtargetdir="$2" ; shift 2 ;;
- -f|--force) force="yes" ; shift ;;
- --) shift ; break ;;
- *) echo >&2 "ERROR: Internal error resolving options." ; exit 1 ;;
- esac
- done
- basedir="/etc"
- targetdir="$(mktemp -td localtweaks.XXXXXX)"
- includefile="/etc/local/localtweaks.include"
- excludefile="/etc/local/localtweaks.exclude"
- extensions=".orig .old"
- if [ -n "$realtargetdir" ] && [ -e "$realtargetdir" ]; then
- if [ "$force" = "yes" ]; then
- rm -rf "$realtargetdir"
- else
- echo >&2 "Error: Target directory exists already."
- exit 1
- fi
- fi
- for ext in $extensions; do
- extfiles="$(find $basedir -name "*$ext")"
- for extfile in $extfiles; do
- xextfile="${extfile%$ext}"
- files="$files ${xextfile#$basedir}"
- done
- done
- includes=""
- if [ -d "$(dirname "$includefile")" ] && [ -r "$includefile" ]; then
- includes="$(cat "$includefile")"
- for include in $includes; do
- files="$files ${include#$basedir}"
- done
- fi
- excludes=""
- if [ -d "$(dirname "$excludefile")" ] && [ -r "$excludefile" ]; then
- xexcludes="$(cat "$excludefile")"
- for exclude in $xexcludes; do
- excludes="$excludes ${exclude#$basedir}"
- done
- fi
- # process all files once each
- files="$(for x in $files; do echo "$x"; done | uniq | sort)"
- for file in $files; do
- # file were chosen based on backups - it may not exist itelf
- [ -s "$basedir/$file" ] || continue
- for exclude in $excludes; do
- [ "$file" = "$exclude" ] && continue 2
- done
- ext=""
- for xext in $extensions; do
- if [ -e $basedir/$file$xext ]; then
- ext="$xext"
- continue
- fi
- done
- newdir="$targetdir/$basedir/$(dirname "$file")"
- mkdir -p "$newdir"
- cp -af "$basedir/$file" "$newdir"
- # Only diff against existing and non-empty files.
- if [ -n "$ext" ] && [ -s "$basedir/$file$ext" ]; then
- diff -ruN "$basedir/$file$ext" "$basedir/$file" > "$targetdir/$basedir/$file.diff" || [ $? -lt 2 ]
- chown --reference="$basedir/$file" "$targetdir/$basedir/$file.diff"
- chmod --reference="$basedir/$file" "$targetdir/$basedir/$file.diff"
- chmod a-x "$targetdir/$basedir/$file.diff"
- fi
- done
- if [ -n "$realtargetdir" ]; then
- mv "$targetdir" "$realtargetdir"
- rm -rf "$targetdir"
- fi
|