#!/bin/ksh

# move-to-front - an example of how flipdiff can be used

if [ "x$1" == "x--version" ]
then
  echo "move-to-front - patchutils version 0.4.5"
  exit 0
fi

if [ "$#" -eq 0 ] || [ "x$1" == "x--help" ]
then
  echo "usage: move-to-front [PATCHES..] PATCH"
  exit 0
fi

# Dry-run.
eval "patch2=\${$#}"
i=$(($# - 1))
while [ $i -gt 0 ]
do
  eval "patch1=\${$i}"
  flipdiff $patch1 $patch2 >/dev/null || exit $?
  i=$(($i - 1))
done

# Do it for real.
i=$(($# - 1))
while [ $i -gt 0 ]
do
  eval "patch1=\${$i}"
  (set -x; flipdiff --in-place $patch1 $patch2) || exit $?
  i=$(($i - 1))
done
