Home | History | Annotate | Download | only in scripts
      1 #!/bin/bash
      2 
      3 get_pnum () {
      4     echo $1 | sed -e 's/^\(.*\)-\([0-9][0-9]\)-\(.*\)$/\2/'
      5 }
      6 
      7 get_comp () {
      8     echo $1 | sed -e 's/^\(.*\)-\([0-9][0-9]\)-\(.*\)$/\1/'
      9 }
     10 
     11 get_pname () {
     12     echo $1 | sed -e 's/^\(.*\)-\([0-9][0-9]\)-\(.*\)$/\3/'
     13 }
     14 
     15 rename () {
     16     svn rename $1 $2
     17     if [ -f ../base-specs/$3.spec ]; then
     18 	perl -pi -e "s/Patch(.*):(\s*)$1/Patch\$1:\$2$2/" ../base-specs/$3.spec
     19     else
     20 	echo "WARNING: $3.spec not found"
     21     fi
     22 }
     23 
     24 # ask "question" variable_name "default answer"
     25 ask () {
     26     echo -n "$1"
     27     if [ ! -z $3 ]; then
     28 	echo -n " [$3]: "
     29     else
     30 	echo -n ": "
     31     fi
     32 
     33     read -e val
     34     if [ "x$val" = x ]; then
     35 	eval "$2=\"$3\""
     36     else
     37 	eval "$2=\"$val\""
     38     fi
     39 }
     40 
     41 # ask_yes_no "question" variable_name "default answer"
     42 ask_yes_no () {
     43     yes_no_repeat=yes
     44     while [ $yes_no_repeat = yes ]; do
     45 	yes_no_repeat=no
     46 	ask "${@}"
     47 	eval "the_ans=\"\$$2\""
     48 	case "$the_ans" in
     49 	    [yY]|[yY][eE][sS] )
     50                 eval "$2=yes"
     51 		;;
     52  	    [nN]|[nN][oO] )
     53 		eval "$2=no"
     54 		;;
     55 	    * )
     56 	        echo "Please answer yes or no"
     57 		yes_no_repeat=yes
     58 	esac
     59     done
     60 }
     61 
     62 usage () {
     63     echo "Usage: $0 [options] [component...]"
     64     echo
     65     echo "Run this script in the patches subdirectory to reorder"
     66     echo "the patch numbers to be continuous and starting from 01"
     67     echo
     68     echo "If no components are specified, it'll check all of them."
     69     echo "It does not change the Patch<n> and %patch<n> numbers"
     70     echo "in the spec files, but updates the file names with the"
     71     echo "new patch numbers."
     72     echo
     73     echo "Options:"
     74     echo
     75     echo "    -f, --force           don't ask for confirmation"
     76     echo "    -h, --help            print this usage info"
     77 }
     78 
     79 FORCE=0
     80 while [ $# -gt 0 ]; do
     81     case $1 in
     82 	-f|--force )
     83 	    FORCE=1
     84 	    ;;
     85 	-h|--help )
     86 	    usage
     87 	    exit 0
     88 	    ;;
     89 	-* )
     90 	    echo "Unknown option: $1"
     91 	    usage
     92 	    exit 1
     93 	    ;;
     94 	* )
     95 	    break
     96     esac
     97 done
     98 
     99 mybasename=$(basename $(pwd))
    100 
    101 if [ $mybasename != patches ]; then
    102     echo "Run this script in the patches subdirectory"
    103     exit 1
    104 fi
    105 
    106 if [ $# -gt 0 ]; then
    107     PLIST=
    108     for comp in $*; do
    109 	comp_PLIST=$(eval echo $comp-[0-9][0-9]-*.diff)
    110 	n_p_1st=$(echo $comp_PLIST | cut -f1 -d' ')
    111 	if [ -f $n_p_1st ]; then
    112 	    PLIST="$PLIST $comp_PLIST"
    113 	else
    114 	    echo "No patches found for component $comp"
    115 	fi
    116     done
    117 else
    118     PLIST=$(eval echo *-[0-9][0-9]-*.diff)
    119 fi
    120 
    121 prev_comp=xxNoNexx
    122 patches_renamed=0
    123 for patch in $PLIST; do
    124     comp=`get_comp $patch`
    125     pnum=`get_pnum $patch`
    126     pname=`get_pname $patch`
    127 
    128     if [ $comp != $prev_comp ]; then
    129 	ord=01
    130 	if [ $prev_comp != xxNoNexx -a $patches_renamed = 0 ]; then
    131 	    echo "No patches need renumbering for component $prev_comp"
    132 	fi
    133 	patches_renamed=0
    134     else
    135 	ord=`expr $ord + 1`
    136 	ord=`echo 0$ord | sed -e 's/.*\(..\)$/\1/'`
    137     fi
    138 
    139     if [ $pnum != $ord ]; then
    140 	if [ $FORCE = 0 ]; then
    141 	    ask_yes_no "Rename $patch to $comp-$ord-$pname?" ans "yes"
    142 	    if [ $ans = yes ]; then
    143 		rename $patch $comp-$ord-$pname $comp
    144 	    fi
    145 	else
    146 	    rename $patch $comp-$ord-$pname $comp
    147 	fi
    148     fi
    149     prev_comp=$comp
    150 done
    151 if [ $prev_comp != xxNoNexx -a $patches_renamed = 0 ]; then
    152     echo "No patches need renumbering for component $prev_comp"
    153 fi
    154