summaryrefslogtreecommitdiff
path: root/savelocaltweaks
blob: 3ce2bd0806ddf82c62809559df90cd842e6bf604 (plain)
  1. #!/bin/sh
  2. set -e
  3. basedir="/etc"
  4. targetdir="$(mktemp -td localtweaks.XXXXXX)"
  5. includefile="/etc/local/localtweaks.include"
  6. excludefile="/etc/local/localtweaks.exclude"
  7. extensions=".orig .old"
  8. for ext in $extensions; do
  9. extfiles="$(find $basedir -name "*$ext")"
  10. for extfile in $extfiles; do
  11. xextfile="${extfile%$ext}"
  12. files="$files ${xextfile#$basedir}"
  13. done
  14. done
  15. includes=""
  16. if [ -d "$(dirname "$includefile")" ] && [ -r "$includefile" ]; then
  17. includes="$(cat "$includefile")"
  18. for include in $includes; do
  19. files="$files ${include#$basedir}"
  20. done
  21. fi
  22. excludes=""
  23. if [ -d "$(dirname "$excludefile")" ] && [ -r "$excludefile" ]; then
  24. xexcludes="$(cat "$excludefile")"
  25. for exclude in $xexcludes; do
  26. excludes="$excludes ${exclude#$basedir}"
  27. done
  28. fi
  29. # process all files once each
  30. files="$(for x in $files; do echo "$x"; done | uniq | sort)"
  31. for file in $files; do
  32. # file were chosen based on backups - it may not exist itelf
  33. [ -s "$basedir/$file" ] || continue
  34. for exclude in $excludes; do
  35. [ "$file" = "$exclude" ] && continue 2
  36. done
  37. ext=""
  38. for xext in $extensions; do
  39. if [ -e $basedir/$file$xext ]; then
  40. ext="$xext"
  41. continue
  42. fi
  43. done
  44. newdir="$targetdir/$basedir/$(dirname "$file")"
  45. mkdir -p "$newdir"
  46. cp -af "$basedir/$file" "$newdir"
  47. # Only diff against existing and non-empty files.
  48. if [ -n "$ext" ] && [ -s "$basedir/$file$ext" ]; then
  49. diff -ruN "$basedir/$file$ext" "$basedir/$file" > "$targetdir/$basedir/$file.diff" || [ $? -lt 2 ]
  50. chown --reference="$basedir/$file" "$targetdir/$basedir/$file.diff"
  51. chmod --reference="$basedir/$file" "$targetdir/$basedir/$file.diff"
  52. chmod a-x "$targetdir/$basedir/$file.diff"
  53. fi
  54. done