scripts/patch-renumber.sh
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Nov 10 19:32:11 2009 +0100 (2009-11-10)
changeset 1622 9ad2a3fd1fcc
parent 1608 3d5aead9e15d
child 1623 f935634ef900
permissions -rwxr-xr-x
scripts: output renumbered patches in a new directory

When renumbering patches, the original patches get removed and replaced
with the new ones. This can be annoying to loose the original patches.
Fix this by putting the new patchs in a directory of their own.
yann@756
     1
#!/bin/sh
yann@756
     2
# Yes, this intends to be a true POSIX script file.
yann@1175
     3
set -e
yann@756
     4
yann@756
     5
myname="$0"
yann@756
     6
yann@1175
     7
# Parse the tools' paths configuration
yann@1175
     8
. "paths.mk"
yann@1175
     9
yann@756
    10
doUsage() {
yann@756
    11
  cat <<_EOF_
yann@1622
    12
Usage: ${myname} <src_dir> <dst_dir> <base> <inc> [sed_re]
yann@1622
    13
    Renumbers all patches found in 'src_dir', starting at 'base', with an
yann@1622
    14
    increment of 'inc', and puts the renumbered patches in 'dst_dir'.
yann@1622
    15
    Leading digits are replaced with the new indexes, and a subsequent '_'
yann@1622
    16
    is replaced with a '-'.
yann@1608
    17
    If 'sed_re' is given, it is interpreted as a valid sed expression, and
yann@1622
    18
    is be applied to the patch name.
yann@1622
    19
    If the environment variable FAKE is set to 'y', then nothing gets done,
yann@1622
    20
    the command to run is only be printed, and not executed (so you can
yann@1622
    21
    check beforehand).
yann@1622
    22
    'dst_dir' must not yet exist.
yann@1608
    23
    Eg.:
yann@1622
    24
      patch-renumber.sh patches/gcc/4.2.3 patches/gcc/4.2.4 100 10
yann@1622
    25
      patch-renumber.sh /some/dir/my-patches patches/gcc/4.3.1 100 10 's/(all[_-])*(gcc[-_])*//;'
yann@756
    26
_EOF_
yann@756
    27
}
yann@756
    28
yann@1622
    29
[ $# -lt 4 -o $# -gt 5 ] && { doUsage; exit 1; }
yann@756
    30
yann@1622
    31
src="${1}"
yann@1622
    32
dst="${2}"
yann@1622
    33
cpt="${3}"
yann@1622
    34
inc="${4}"
yann@1622
    35
sed_re="${5}"
yann@1622
    36
if [ ! -d "${src}" ]; then
yann@1622
    37
    printf "%s: '%s': not a directory\n" "${myname}" "${src}"
yann@1622
    38
    exit 1
yann@1622
    39
fi
yann@1622
    40
if [ -d "${dst}" ]; then
yann@1622
    41
    printf "%s: '%s': directory already exists\n" "${myname}" "${dst}"
yann@1622
    42
    exit 1
yann@1608
    43
fi
yann@1608
    44
yann@1622
    45
Q=
yann@1622
    46
if [ -n "${FAKE}" ]; then
yann@1622
    47
    printf "%s: won't do anything: FAKE='%s'\n" "${myname}" "${FAKE}"
yann@1622
    48
    Q="echo"
yann@1622
    49
fi
yann@1622
    50
yann@1622
    51
${Q} mkdir -pv "${dst}"
yann@1622
    52
for p in "${src}/"*.patch*; do
yann@756
    53
    [ -e "${p}" ] || { echo "No such file '${p}'"; exit 1; }
yann@1608
    54
    newname="$(printf "%03d-%s"                                     \
yann@1608
    55
                      "${cpt}"                                      \
yann@1608
    56
                      "$( basename "${p}"                           \
yann@1608
    57
                          |"${sed}" -r -e 's/^[[:digit:]]+[-_]//'   \
yann@1608
    58
                                       -e "${sed_re}"               \
yann@1608
    59
                        )"                                          \
yann@1192
    60
              )"
yann@1622
    61
    ${Q} cp -v "${p}" "${dst}/${newname}"
yann@756
    62
    cpt=$((cpt+inc))
yann@756
    63
done