deleted subcommands file ; changed pkg working directories ; moved commands to comman...
[opensuse:pack-tools.git] / commands / spec
1 #!/bin/bash
2 #spec parser (read&write) support by dlovasko@suse.com
3
4 #<sbrabec> Po %setup mohou být:
5 #<sbrabec> translation-update-upstream
6 #<sbrabec> gnome-patch-translation-prepare
7 #<sbrabec> A po posledním patchi:
8 #<sbrabec> gnome-patch-translation-update
9 #<sbrabec> Poznámka:
10 #<sbrabec> Uvažuji o spojení obou nástrojů a přejmenování na translation-tool-prepare and translation-tool-update.
11
12 #un/comment for debug/release
13
14 #set -x
15
16 function usage()
17 {
18         echo "usage: spec subcommand [subcommand options]"
19         echo "subcommands:"
20         echo "   get [tag tagname] [specfile]"
21         echo "   set [tag tagname oldval newval] [specfile]"    
22         echo "   merge specfile1 specfile2 specfile_out"
23         echo "   add [patch [specfile|patch options [specfile]]]"
24         echo "   fixsles [specfile]"
25
26         exit 1
27 }
28
29 if [ "$#" = 0 ]
30 then
31         usage
32 fi
33
34 #argument 1 = specfile path
35 #argument 2 = what to get
36 function _pkg_spec_get
37 {
38         if [[ "$2" == %* ]]
39         then
40                 if [[ "$2" =~ (description|prep|build|install|clean|pre|post|preun|postun|verifyscript|files)$ ]]
41                 then
42                         sed -n '/^'"$2"'/,/^$/p' "$1"
43                 fi
44         fi
45         if [ "$2" = "Version" ] 
46         then
47                 grep -e "^$2" "$1" | head -n 1
48         else
49                 grep -e "^$2" "$1"
50         fi
51 }
52
53
54 case $1 in
55         get)
56                 if [ "$2" = "-h" -o "$2" = "--help" ]
57                 then
58                                 echo "usage: pkg spec get WHAT [SPECFILE]"
59                                 echo "when no specfile specified, specfile is deduced from .osc/_package"
60                                 exit 1
61                 fi
62
63                 if [ -z "$3" ]
64                 then
65                         specfile=`cat .osc/_package | sed 's/\..*//'`.spec
66                 else
67                         specfile="$3"
68                 fi
69                         
70                 _pkg_spec_get "$specfile" "$2"
71                 
72                 
73         ;;
74         rget)
75                 if [ "$2" = "-h" -o "$2" = "--help" ]
76                 then
77                                 echo "usage: pkg spec rget PACKAGE WHAT"
78                                 exit 1
79                 fi
80                 
81                 _spec=$(mktemp)
82
83                 _develproject=$(osc -A https://api.opensuse.org meta pkg openSUSE:Factory $2 | xpath /package/devel/@project 2> /dev/null | sed -e 's/project=//' -e 's/"//g' -e 's/^ //')
84                 osc -A https://api.opensuse.org cat $_develproject/$2/$2.spec > $_spec 2> /dev/null
85                 echo "$_develproject:"
86                 _pkg_spec_get "$_spec" "$3"
87                 echo
88
89                 osc -A https://api.opensuse.org cat openSUSE:Factory/$2/$2.spec > $_spec 2> /dev/null
90                 echo "Factory:"
91                 _pkg_spec_get "$_spec" "$3"
92                 echo
93
94                 osc -A https://api.opensuse.org cat openSUSE:12.1:Update/$2/$2.spec > $_spec 2> /dev/null
95                 echo "openSUSE 12.1: "
96                 _pkg_spec_get "$_spec" "$3"
97                 echo
98
99                 osc -A https://api.opensuse.org cat openSUSE:11.4:Update/$2/$2.spec > $_spec 2> /dev/null
100                 echo "openSUSE 11.4: "
101                 _pkg_spec_get "$_spec" "$3"
102                 echo
103
104                 osc -A https://api.suse.de cat SUSE:SLE-11-SP2:Update:Test/$2/$2.spec > $_spec 2> /dev/null
105                 echo "SUSE-SLE 11-SP2: "
106                 _pkg_spec_get "$_spec" "$3"
107                 echo
108
109                 osc -A https://api.suse.de cat SUSE:SLE-10-SP4:Update:Test/$2/$2.spec > $_spec 2> /dev/null
110                 echo "SUSE-SLE 10-SP4: "
111                 _pkg_spec_get "$_spec" "$3"
112                 echo
113
114                 osc -A https://api.suse.de cat SUSE:SLE-9-SP4:Update:Test/$2/$2.spec > $_spec 2> /dev/null
115                 echo "SUSE-SLE 9-SP4: "
116                 _pkg_spec_get "$_spec" "$3"
117                 echo
118
119                 rm -f $_spec
120         ;;
121         set)
122                 case $2 in
123                         tag)
124                                 # $3 tag name/--help
125                                 # $4 old value
126                                 # $5 new value
127                                 # $6 specfile
128
129                                 if [ -z "$6" ]
130                                 then
131                                         specfile=`cat .osc/_package | sed 's/\..*//'`.spec
132                                 else
133                                         specfile="$6"
134                                 fi
135
136                                 oldval=`grep ^$3 $specfile | tr -s ' ' | sed 's/^'"$3: "'//'`
137                                 if [ "$4" != "$oldval" ] 
138                                 then
139                                         echo "old value supplied does not match the actual value, sorry"
140                                         exit 1
141                                 fi
142                                 sed -r -i s/\(^$3:\\s+\)$4$/\\1$5/g $specfile
143                                 exit 0
144                         ;;
145                         -h|--help)
146                                 echo "usage: pkg spec set tag [OLD_VALUE] [NEW_VALUE] [SPECFILE]"
147                                 echo "when no specfile specified, specfile is deduced from .osc/_package"
148                                 exit 1
149                         ;;
150                 esac
151         ;;
152         add)
153                 case $2 in
154                         patch)
155                         #parse arguments
156                         if [ "$3" = "-h" -o "$3" = "--help" ]
157                         then
158                                 echo "usage: ..."
159                                 exit 1
160                         fi 
161
162                         patchname="$3"
163
164                         if [ -z "$4" ]
165                         then
166                                 specfile=`cat .osc/_package | sed 's/\..*//'`.spec
167                         else
168                                 case $4 in
169                                         *.spec)
170                                                 specfile="$4"
171                                         ;;
172                                         *)
173                                                 options="$4"
174                                                 if [ -z "$5" ]
175                                                 then
176                                                         specfile=`cat .osc/_package | sed 's/\..*//'`.spec
177                                                 else
178                                                         specfile="$5"
179                                                 fi
180                                         ;;
181                                 esac
182                         fi
183
184                         grep -q '^Patch[0-9]*' $specfile
185                         retcode=$?
186
187                         #if there is no patch yet
188                         if [ $retcode = 1 ]
189                         then
190                                 #find last source
191                                 linenum=`cat $specfile | grep -n '^Source[0-9]*' | tail -n 1 | sed 's/:.*$//'`
192         
193                                 #edit with sed
194                                 let linenum=linenum+1
195                                 sed -i "${linenum} i Patch0:         $patchname" $specfile
196
197                                 #find end of prep phase
198                                 linenum=`cat $specfile | grep -n '^%prep' | tail -n 1 | sed 's/:.*$//'`
199                                 while [ -n "`sed -n "${linenum}p" $specfile`" ]
200                                 do 
201                                         let linenum=linenum+1 ; 
202                                 done
203
204                                 #edit with sed
205                                 sed -i "${linenum} i %patch0 $options" $specfile
206
207                         else
208                                 #find last patch
209                                 linenum=""
210                                 patchnum=""
211
212                                 linenum=`cat $specfile | grep -n '^Patch[0-9]*' | tail -n 1 | sed 's/:.*$//'`
213                                 patchnum=`cat $specfile | grep -o '^Patch[0-9]*' | tail -n 1 | sed 's/^Patch//'`
214
215                                 let patchnum=patchnum+1
216                                 let linenum=linenum+1
217         
218                                 sed -i "${linenum} i Patch$patchnum:         $patchname" $specfile
219
220                                 #find last patch in prep phase
221                                 linenum=""
222                                 patchnum=""
223                                 linenum=`cat $specfile | grep -n '^%patch[0-9]*' | tail -n 1 | sed 's/:.*$//'`
224                                 patchnum=`cat $specfile | grep -o '^%patch[0-9]*' | tail -n 1 | sed 's/^%patch//'`
225
226                                 let patchnum=patchnum+1
227                                 let linenum=linenum+1
228
229                                 sed -i "${linenum} i %patch${patchnum} $options" $specfile
230                         fi
231                 esac
232         ;;
233         merge)
234                 # $2 = specfile 1 - in
235                 # $3 = specfile 2 - in
236                 # $4 = specfile 3 - out
237
238                 if [ "$2" = '-h' -o "$2" = '--help' ]
239                 then
240                         echo "usage: ..."
241                         exit 1
242                 fi
243
244                 last1="$(cat $2 | grep ^Patch[0-9]* | tail -n 1)"
245                 last2="$(cat $3 | grep ^Patch[0-9]* | tail -n 1)"
246
247                 num1="$(echo $last1 | grep -o ^Patch[0-9]* | sed 's/Patch//')"
248                 num2="$(echo $last2 | grep -o ^Patch[0-9]* | sed 's/Patch//')"
249
250                 name1="$(echo $last1 | tr -s ' ' | grep -o ' .*$')"
251                 name2="$(echo $last2 | tr -s ' ' | grep -o ' .*$')"
252
253                 #if specfiles have the last patch with the same number
254                 if [ "$num1" = "$num2" ]
255                 then
256                         #check if files are different
257                         if [ "$name1" != "$name2" ]
258                         then
259                                 #write to $3 both patches, one incremented
260                                 let newnum2=num2+1
261                 
262                                 cp $2 $4
263                                 newlast2="$(echo "$last2" | sed "s/Patch$num2/Patch$newnum2/")"
264                                 echo "$last1"
265                                 echo "$newlast2"
266                                 sed -i "/$last1/ a\
267                                 $newlast2" $4
268                         fi
269                 fi
270                 exit 0
271         ;;
272
273         fixsles|fixrelease|fix)
274                 if [ "$2" = '-h' -o "$2" = '--help' ]
275                 then
276                         echo "usage: pkg spec fixsles [specfile]"
277                         exit 1
278                 fi
279
280                 if [ -n "$2" ]
281                 then
282                         specfile="$2"
283                 else
284                         specfile=`cat .osc/_package | sed 's/\..*//'`.spec
285                 fi
286                 
287                 sed -i 's/^Release.*$/Release:        0/g' $specfile
288         ;;
289
290         -h|--help)
291                 usage
292         ;;
293 esac