mac80211: add a maintenance script for upstream backport commits
[openwrt/openwrt.git] / package / kernel / mac80211 / scripts / import-backports.sh
1 #!/usr/bin/env bash
2 BASE=$1; shift
3
4 usage() {
5 echo "Usage: $0 NNN <file>..."
6 exit 1
7 }
8
9 check_number() {
10 case "$1" in
11 [0-9][0-9][0-9]) return 0;;
12 esac
13 return 1;
14 }
15
16 patch_header()
17 {
18 awk '
19 /^(---|\*\*\*|Index:)[ \t][^ \t]|^diff -/ \
20 { exit }
21 { print }
22 '
23 }
24
25 strip_diffstat()
26 {
27 awk '
28 /#? .* \| / \
29 { eat = eat $0 "\n"
30 next }
31 /^#? .* files? changed(, .* insertions?\(\+\))?(, .* deletions?\(-\))?/ \
32 { eat = ""
33 next }
34 { print eat $0
35 eat = "" }
36 '
37 }
38
39 strip_trailing_whitespace() {
40 sed -e 's:[ '$'\t'']*$::'
41 }
42
43 fixup_header() {
44 awk '
45 /^From / { next }
46 /^Subject: / {
47 sub("Subject: \\[[^\]]*\\]", "Subject: [PATCH]")
48 }
49 { print }
50 '
51 }
52
53 check_number "$BASE" || usage
54
55 quilt series > /dev/null || {
56 echo "Not in quilt directory"
57 exit 2
58 }
59
60 get_next() {
61 NEW=$BASE
62 quilt series | while read CUR; do
63 [ -n "$CUR" ] || break
64 CUR=${CUR%%-*}
65 check_number "$CUR" || continue
66 [ "$CUR" -lt "$NEW" ] && continue
67 [ "$CUR" -ge "$(($BASE + 100))" ] && continue
68 NEW="$(($CUR + 1))"
69 echo $NEW
70 done | tail -n1
71 }
72
73 CUR=`get_next`
74 CUR="${CUR:-$BASE}"
75
76 while [ -n "$1" ]; do
77 FILE="$1"; shift
78 NAME="$(basename $FILE)"
79 NAME="${NAME#[0-9]*-}"
80 echo -n "Processing patch $NAME: "
81
82 [ -e "$FILE" ] || {
83 echo "file $FILE not found"
84 exit 1
85 }
86
87 grep -qE "$NAME$" patches/series && {
88 echo "already applied"
89 continue
90 }
91
92 quilt new "$CUR-$NAME" || exit 1
93 patch_header < "$FILE" |
94 strip_diffstat |
95 strip_trailing_whitespace |
96 fixup_header > "patches/$CUR-$NAME"
97
98 quilt fold < "$FILE" || {
99 cp "$FILE" ./cur_patch
100 echo "patch $FILE failed to apply, copied to ./cur_patch"
101 exit 1
102 }
103
104 quilt refresh -p ab --no-index --no-timestamps
105
106 CUR="$(($CUR + 1))"
107 done
108
109 exit 0