Whamcloud - gitweb
4dd40799b228ab4356f9af4e87a3736cca675fe6
[fs/lustre-release.git] / lustre / scripts / patch-if-change
1 #! /bin/sh
2 # patch-if-change - Copy FILES to the current directory, apply PATCH,
3 # and restore timestamps on files that weren't changed.
4 # Gordon Matzigkeit <gord@fig.org>, 2001-09-25
5
6 # Usage: patch-if-change [PATCHFLAGS]... PATCH FILES...
7 PATCHFLAGS=
8 PATCH=
9 FILES=
10
11 # Arguments beginning with a dash are interpreted as options for
12 # patch.
13 # The first non-option is the patch file, and the rest are files to be
14 # preserved.
15 for arg
16 do
17  case "$arg" in
18  -*) PATCHFLAGS="$PATCHFLAGS $arg" ;;
19  *)
20   if test -z "$PATCH"; then
21    PATCH="$arg"
22   else
23    FILES="$FILES $arg"
24   fi
25   ;;
26  esac
27 done
28
29 # Make backups of all the affected files.
30 for file in $FILES; do
31  oldfile=`echo "$file" | sed -e 'sX.*/XX'`
32  test -f $oldfile && { cp -p $oldfile $oldfile.backup || exit $?; }
33  cp $file . || exit $?
34 done
35
36 # Apply the patch.
37 patch $PATCHFLAGS < $PATCH
38
39 # Revert the files that didn't change.
40 for file in $FILES; do
41  oldfile=`echo "$file" | sed -e 'sX.*/XX'`
42  test -f $oldfile.backup || continue
43  if cmp -s $oldfile $oldfile.backup; then
44   mv -f $oldfile.backup $oldfile
45  else
46   rm -f $oldfile.backup
47  fi
48 done
49
50 exit 0