summaryrefslogtreecommitdiff
path: root/savelocaltweaks
blob: aff588a85180cc180e5042f83419c064b5ec340e (plain)
  1. #!/bin/sh
  2. set -e
  3. PRG=$(basename "$0")
  4. TEMP="`getopt -s sh -o t:f -l target:,force -n "$PRG" -- "$@"`"
  5. if [ $? != 0 ] ; then echo >&2 "ERROR: Internal getopt error." ; exit 1 ; fi
  6. eval set -- "$TEMP"
  7. realtargetdir=''
  8. force='no'
  9. while true ; do
  10. case "$1" in
  11. -t|--target) realtargetdir="$2" ; shift 2 ;;
  12. -f|--force) force="yes" ; shift ;;
  13. --) shift ; break ;;
  14. *) echo >&2 "ERROR: Internal error resolving options." ; exit 1 ;;
  15. esac
  16. done
  17. basedir="/etc"
  18. targetdir="$(mktemp -td localtweaks.XXXXXX)"
  19. includefile="/etc/local/localtweaks.include"
  20. excludefile="/etc/local/localtweaks.exclude"
  21. extensions=".orig .old"
  22. if [ -n "$realtargetdir" ] && [ -e "$realtargetdir" ]; then
  23. if [ "$force" = "yes" ]; then
  24. rm -rf "$realtargetdir"
  25. else
  26. echo >&2 "Error: Target directory exists already."
  27. exit 1
  28. fi
  29. fi
  30. for ext in $extensions; do
  31. extfiles="$(find $basedir -name "*$ext")"
  32. for extfile in $extfiles; do
  33. xextfile="${extfile%$ext}"
  34. files="$files ${xextfile#$basedir}"
  35. done
  36. done
  37. includes=""
  38. if [ -d "$(dirname "$includefile")" ] && [ -r "$includefile" ]; then
  39. includes="$(cat "$includefile")"
  40. for include in $includes; do
  41. files="$files ${include#$basedir}"
  42. done
  43. fi
  44. excludes=""
  45. if [ -d "$(dirname "$excludefile")" ] && [ -r "$excludefile" ]; then
  46. xexcludes="$(cat "$excludefile")"
  47. for exclude in $xexcludes; do
  48. excludes="$excludes ${exclude#$basedir}"
  49. done
  50. fi
  51. # process all files once each
  52. files="$(for x in $files; do echo "$x"; done | uniq | sort)"
  53. for file in $files; do
  54. # file were chosen based on backups - it may not exist itelf
  55. [ -s "$basedir/$file" ] || continue
  56. for exclude in $excludes; do
  57. [ "$file" = "$exclude" ] && continue 2
  58. done
  59. ext=""
  60. for xext in $extensions; do
  61. if [ -e $basedir/$file$xext ]; then
  62. ext="$xext"
  63. continue
  64. fi
  65. done
  66. newdir="$targetdir/$basedir/$(dirname "$file")"
  67. mkdir -p "$newdir"
  68. cp -af "$basedir/$file" "$newdir"
  69. # Only diff against existing and non-empty files.
  70. if [ -n "$ext" ] && [ -s "$basedir/$file$ext" ]; then
  71. diff -ruN "$basedir/$file$ext" "$basedir/$file" > "$targetdir/$basedir/$file.diff" || [ $? -lt 2 ]
  72. chown --reference="$basedir/$file" "$targetdir/$basedir/$file.diff"
  73. chmod --reference="$basedir/$file" "$targetdir/$basedir/$file.diff"
  74. chmod a-x "$targetdir/$basedir/$file.diff"
  75. fi
  76. done
  77. if [ -n "$realtargetdir" ]; then
  78. mv "$targetdir" "$realtargetdir"
  79. rm -rf "$targetdir"
  80. fi