summarylogtreecommitdiffstats
path: root/customizepkg-patching
blob: 34f58476ff78f544008d35a063ebbd23bcc6c1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
ver=20
#-----------------------------------------------------------------------#
#
# customizepkg-patching
# Applies a patch to PKGBUILD before building.
#
#  Maintainer: Michael Duell <michael.duell@rub.de>
#
#  Old maintainer:
#  Daniel Oertwig <Daniel.Oertwig@googlemail.com>
#  homepage: http://www.proggen.org
#
# A modification of the original customizepkg from
# <wain@archlinux.fr>
# homepage: http://archlinux.fr
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#-----------------------------------------------------------------------#



#-----------------------------------------------------------------------#
## Some help ...
#-----------------------------------------------------------------------#
print_usage()
{
  echo "customizepkg - patching version $ver"
  echo
  echo "Without any option, this script reads files from the current directory and shows a diff"
  echo "between the original and the customized version of the files."
  echo "The patch to apply the changes is read from /etc/customizepkg.d/\$pkgname"
  echo
  echo "usage: customizepkg <option>"
  echo
  echo "customizepkg --version, -V       Shows version"
  echo "customizepkg --help, -h          Shows this help"
  echo "customizepkg --edit, -e          Edit files marked for customization"
  echo "                                 (add -v to edit with vimdiff)"
  echo "customizepkg --patch, -p         Review patch (add -m to save)"
  echo "customizepkg --modify, -m        Apply the saved modifications"
  echo "customizepkg --vimdiff, -v       Show diff using vimdiff"
  echo
  echo "To customize a file, use:"
  echo "$ cp file file.original"
  echo "then edit file."
  echo
  echo "To add a file, create and edit it, then use:"
  echo "$ touch file.original"
  echo
  echo "To remove a file, use:"
  echo "$ mv file file.original"
  echo
  echo "Written by Daniel Oertwig <Daniel.Oertwig@googlemail.com>"
  echo "      homepage: http://www.proggen.org"
  echo " as a modification of the original customizepkg from"
  echo " <wain@archlinux.fr>"
  echo "  homepage: http://archlinux.fr"
  echo
}



print_version()
{
  echo "customizepkg - patching version $ver"
}



#-----------------------------------------------------------------------#
## MAIN PROGRAMM
#-----------------------------------------------------------------------#
shopt -s nullglob
VIMDIFF=0
MODIFY=0
PATCH=0
EDIT=0



# Parse commandline options. Modifications are made only
# if the option -m or --modify are given!
while [[ "$#" != "0" ]]; do
  case $1 in
    -h|--help)
      print_usage
      exit 0
      ;;
    --version|-V)
      print_version
      exit 0
      ;;
    -e|--edit)
      EDIT=1
      ;;
    -p|--patch)
      PATCH=1
      ;;
    -m|--modify)
      MODIFY=1
      ;;
    --vimdiff|-v)
      VIMDIFF=1
      ;;
  esac
  shift
done



# Ensure the PKGBUILD file is there and read it to
# get the pkgname variable!
if [[ ! -r ./PKGBUILD ]]; then
  echo "PKGBUILD not found!"
  exit 1
fi
source ./PKGBUILD || exit 1



# Perform an action on each pair of original and
# changed files.
for_changes_do() {
  for f in ./*.original; do
    "$@" "$f" "${f%.original}"
  done
}



# Edit customized files, with vimdiff if the VIMDIFF
# option was given, or with the editor specified by
# EDITOR (falling back on vi) otherwise.
if [[ "$EDIT" == "1" ]]; then
  if [[ "$VIMDIFF" == "1" ]]; then
    edit="vim -d"
  else
    edit="${EDITOR:-vi}"
  fi
  for_changes_do $edit
  exit 0
fi



# Show a patch with the current modifications.
# If the MODIFY option was given, also write it to the
# configdir.
if [[ "$PATCH" == "1" ]]; then
  for_changes_do diff -upN |
    if [[ "$MODIFY" == "1" ]]; then
      tee "/etc/customizepkg.d/$pkgname"
    else
      cat
    fi
  exit 0
fi



# Ensure there is a patch available in the configdir!
if [[ ! -r "/etc/customizepkg.d/$pkgname" ]]; then
  echo "No configuration for $pkgname in /etc/customizepkg.d"
  exit 1
fi



# Apply the patch. Exit if something goes wrong.
patch -b -z .original -i "/etc/customizepkg.d/$pkgname"
if [[ "$?" -gt "1" ]]; then
  echo "Patching failed!"
  for f in *.rej; do
    echo "$f"
    cat "$f"
  done
  exit 1
fi



# Show the diffs
if [[ "$VIMDIFF" == "1" ]]; then
  for_changes_do vim -d
else
  for_changes_do diff -upN
fi



# Revert a modification.
# If the original file is empty, it means that
# a new file was created by the patch, so remove it.
revert() {
  mv -f "$1" "$2"
  [[ -s "$2" ]] || rm "$2"
}



# Only if the MODIFY option was given the 
# modified file stays. Otherwise, the original
# file is used.
if [[ "$MODIFY" == "0" ]]; then
  for_changes_do revert
  rm -f *.rej
fi



# Exit gracefully :)
exit 0