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