make sure that the "pandora" specific packages are installed last
[pandora-misc.git] / hotfix_updater / hotfix / updater.sh
1 #!/bin/bash
2
3 #
4 # Variables that should be manually adjusted for new versions
5 #
6
7 # kernels we are sure we want to update
8 oldkern="\
9 3112d1782a90c2c87ae17a152a35deae \
10 b00a5d617f11366689488395b19411de \
11 aed218fe59ff93618bddd2b52b020014 \
12 53ca541a471f726eb1103f19d4306e61 \
13 b66eb9ddee6ae95b682e20a1ac429413 \
14 c53a33cae520f3c4dc5f558d35602225 \
15 467643e4b4f7b17dd7366ddb5395857b \
16 262ca590cb2153328d90800e0d88247d \
17 6020b4ff1d1fa3900c765caaa6a1bb48 \
18 4500538eca6650ff254195e6be86de02 \
19 2d7c0ce238d6f1714ed7eb0312eb10f2 \
20 "
21 newkern=2abf194bcbe2fca56ef48286d7c7145c
22 currkern=$(md5sum /boot/uImage | cut -d" " -f1)
23
24 # u-boot versions we want to update
25 oldubootvers="\
26 U-Boot_2010.03_(May_19_2010_-_18:30:30) \
27 U-Boot_2010.03-dirty_(Jun_27_2010_-_18:24:31) \
28 U-Boot_2010.03_(Jun_18_2010_-_20:43:36) \
29 "
30
31 # Please list the name of the current kernel image used by okpg here. It is
32 # basically the output of this command:
33 # sudo opkg list-installed | grep kernel-image
34 # If this exact name is matched, the upgrade of the kernel package as well
35 # as the kernel modules will be skipped, saving about 50% of the runtime of
36 # this installer!
37 OPKG_KERNEL_NAME="kernel-image-2.6.27.46-omap1 - 2.6.27-pandora+r24+git4bc490f3aa30d331d624faf2816851211a05f0b7.2-r24.5"
38
39 # Forcefully removing packages: this list is basically the collection of
40 # package names that are meant to be removed. This collection is a side
41 # product of the "get-updated-packages.sh" script. Please make also sure to
42 # add noteworthy packages in the variable "MAIN_UPDATER_TEXT" below.
43 $PACKAGES_FOR_REMOVAL="abiword claws-mail gnumeric libgles-omap3-rawdemos libgnt0 libgoffice-0.8-8 libgoffice-0.8-plugin-plot-barcol libgoffice-0.8-plugin-plot-distrib libgoffice-0.8-plugin-plot-pie libgoffice-0.8-plugin-plot-radar libgoffice-0.8-plugin-plot-surface libgoffice-0.8-plugin-plot-xy libgoffice-0.8-plugin-reg-linear libgoffice-0.8-plugin-reg-logfit libgoffice-0.8-plugin-smoothing libgsf-gnome-1-114 libgstfarsight-0.10-0 libnice libpurple libpurple-plugin-ssl libpurple-plugin-ssl-gnutls libpurple-protocol-aim libpurple-protocol-icq libpurple-protocol-irc libpurple-protocol-msn libpurple-protocol-xmpp libpurple-protocol-yahoo pandora-wallpaper-community pidgin pidgin-data"
44
45 # Some texts used for display in zenity
46 MAIN_UPDATER_TITLE="Update Package 5"
47 MAIN_UPDATER_TEXT="\
48 This PND updates your Pandora OS. You can safely delete it after it has finished.\n\
49 This pack includes all updates from previous Hotfix Packs as well.\n\n\
50 This Update will REMOVE the following applications:\n\
51 AbiWord, Gnumeric, Pidgin, ClawsMail as well as the community wallpapers.\n\n\
52 Do you want to start the upgrade now? "
53 CUSTOM_KERNEL_INSTALLED_TEXT="\
54 You seem to have custom or newer kernel in flash.\n\n\
55 Update it anyway?\n\
56 (if unsure, select Yes)" 
57 CUSTOM_UBOOT_INSTALLED_TEXT="\
58 You seem to have custom or newer u-boot in flash.\n\n\
59 Update it anyway?\n\
60 (if unsure, select Yes)"
61
62
63 #
64 # Functions used during the script
65 #
66
67 err()
68 {
69         echo "$@" >> /tmp/updater_err.log
70 }
71
72
73 log()
74 {
75         echo "$@" >> /tmp/updater.log
76         echo "$@" >&2
77 }
78
79
80 #function for installing all packages in folder
81 # parameters:
82 # $1 path for packages
83 # $2 start percentage for zenity (int!)
84 # $3 end percentage for zenity (int!)
85 # $4 step width for zenity ($4 percent point per gui update / opkg run)
86 # $5 message displayed in zenity during the update
87 install_all_ipk_in_folder()
88 {
89         PACKAGE_PATH=$1
90         START_PERCENTAGE=$2
91         END_PERCENTAGE=$3
92         STEP_PERCENTAGE=$4
93         ZENITY_UPDATE_MESSAGE=$5
94
95         # counting for kernel files:
96         NUMBER_PACKAGES=`ls $PACKAGE_PATH/*ipk | wc -l`
97
98         let DIFF_PERCENTAGE=$END_PERCENTAGE-$START_PERCENTAGE
99         let STEP_NUMBER=$NUMBER_PACKAGES*$STEP_PERCENTAGE/$DIFF_PERCENTAGE
100
101         # set the start values so that at the first run the zenity bar is already updated.
102         VAR=$STEP_NUMBER
103         PERCENTAGE=$START_PERCENTAGE
104
105         PACKAGE_LIST=""
106         # iterate over all packages in the given path
107         for my_package in $PACKAGE_PATH/*ipk
108         do
109                 # packages have to be installed in "batches" of several packages or
110                 # opkg takes 30s per package! because of this gather a list of packages
111                 # and install them whenever a treshold is reached!      
112                 
113                 # only run the zenity update as well as opkg when "enough" packages are reached
114                 if [ $VAR -eq $STEP_NUMBER ]
115                 then
116                         # opkg will err out if the package list is empty which happens in the first iteration
117                         if [ -n "$PACKAGE_LIST" ]
118                         then
119                                 # actually install the current block of packages
120                                 opkg install --nodeps --force-depends --force-overwrite $PACKAGE_LIST >> ./opkg.log
121                                 # after installing, reset PACKAGE_LIST to empty
122                                 PACKAGE_LIST=""
123                         fi
124                         
125                         echo "$PERCENTAGE"
126                         echo "$ZENITY_UPDATE_MESSAGE"
127                         let PERCENTAGE=$PERCENTAGE+$STEP_PERCENTAGE
128                         VAR=0
129                 fi
130                 let VAR=VAR+1
131                 
132                 # populate package list by appending the current package
133                 PACKAGE_LIST="$PACKAGE_LIST $my_package"
134         done
135
136         # there might still be some packages left to work on, do so now:
137         opkg install --nodeps --force-depends --force-overwrite $PACKAGE_LIST >> ./opkg.log
138 }
139
140
141 update_kernel()
142 {
143         have_error=false
144         rm /boot/vmli*
145         rm /boot/uImage.old 
146         rm /boot/uImage-*
147         needfree=$(ls -lk uImage | grep uImage | cut -d" " -f5)
148         currfree=$(df /boot | grep boot | awk '{print $4}')
149         if [ $currfree -lt $needfree ];
150         then
151                 err "There is not enough diskspace on /boot/ to update the kernel.\nKernel couldn't be updated."
152                 log "Kernel not updated - not enough diskspace on /boot/"
153                 have_error=true
154         else
155                 cp uImage /boot/uImage.new 
156                 sync
157                 bad_checksum=false
158                 currkern=$(md5sum /boot/uImage.new | cut -d" " -f1)
159                 if [ "$currkern" = "$newkern" ];
160                 then
161                         mv /boot/uImage /boot/uImage.old 
162                         mv /boot/uImage.new /boot/uImage 
163                         currkern=$(md5sum /boot/uImage | cut -d" " -f1)
164                         if [ "$currkern" != "$newkern" ];
165                         then
166                                 rm /boot/uImage 
167                                 mv /boot/uImage.old /boot/uImage 
168                                 bad_checksum=true
169                         fi
170                 else
171                         rm /boot/uImage.new 
172                         bad_checksum=true
173                 fi
174                 if $bad_checksum;
175                 then
176                         err "There was a checksum error while copying the kernel.\nKernel couldn't be updated. Please check your SD-Card and try to recopy the PND-File."
177                         log "Kernel not updated - checksum error"
178                         have_error=true
179                 fi
180         fi
181
182         if ! $have_error;
183         then
184                 log "Kernel successfully updated"
185                 echo "The kernel has been updated. You need to restart your system." > /tmp/updreboot
186         fi
187         sync
188 }
189
190
191 update_uboot()
192 {
193         if flash_eraseall /dev/mtd1 && nandwrite -p /dev/mtd1 u-boot.bin;
194         then
195                 # XXX: perhaps we need to check if write went ok?
196                 log "u-boot.bin flashed."
197         else
198                 log "u-boot.bin flashing failed."
199         fi
200 }
201
202
203
204 #
205 # The "real" script itself where the work is done...
206 #
207
208
209 # cleanup of old log files
210 rm /tmp/updater.log
211 rm ./opkg.log
212 rm /tmp/updreboot
213 rm /tmp/updater_err.log
214
215
216 if zenity --question --title="$MAIN_UPDATER_TITLE" --text="$MAIN_UPDATER_TEXT" --ok-label="Start now" --cancel-label="Don't do it";
217 then
218         # inside the huge ( )-Block the normal update is done
219         (
220
221         # Save list of services
222
223         mkdir /tmp/rcsave
224         cp -r /etc/rc* /tmp/rcsave
225         opkg remove --force-depends pandora-lcd-state >> ./opkg.log
226         rm /var/lib/opkg/*
227
228         #
229         # Remove old files
230         #
231         echo "5"
232         echo "# Removing old packages"
233         # Forcefully removing packages: this list is basically the collection of
234         # package names that are meant to be removed. This collection is a side
235         # product of the "get-updated-packages.sh" script. Please make also sure to
236         # add noteworthy packages in the variable "MAIN_UPDATER_TEXT" above.
237         opkg remove --nodeps --force-depends $PACKAGES_FOR_REMOVAL
238
239
240         #
241         # Kernel Update
242         #
243
244         modup=$(opkg list-installed | grep kernel-image)
245         if [ "$modup" != "$OPKG_KERNEL_NAME" ];
246         then
247                 # call the function for installing ipks
248                 # $1 path for packages
249                 # $2 start percentage for zenity (int!)
250                 # $3 end percentage for zenity (int!)
251                 # $4 step width for zenity ($4 percent point per gui update / opkg run)
252                 # $5 message displayed in zenity during the update
253                 install_all_ipk_in_folder packages/kernel 7 50 6 "# Updating Kernel Modules... this will take a while..."
254         fi     
255
256         echo "50"
257         echo "# Updating kernel if needed"
258
259         kernel_known=false
260         for oknl in $oldkern $newkern;
261         do
262                 if [ "$oknl" = "$currkern" ];
263                 then
264                         kernel_known=true
265                         break
266                 fi
267         done
268
269         if [ "$kernel_known" = "true" -o -z "$currkern" ];
270         then
271                 if [ "$currkern" != "$newkern" ];
272                 then
273                         update_kernel
274                 else
275                         log "Kernel already up-to-date"
276                 fi
277         else
278                 if zenity --question --title="Custom kernel?" --text="$CUSTOM_KERNEL_INSTALLED_TEXT" --ok-label="Yes" --cancel-label="No";
279                 then
280                         update_kernel
281                 else
282                         log "Kernel update skipped"
283                 fi
284         fi
285
286
287         #
288         # u-boot Update
289         #
290
291
292         echo "55"
293         echo "# U-Boot if needed"
294
295         if [ -f u-boot.bin ];
296         then
297                 rm /tmp/u-boot.bin.nand 2> /dev/null
298                 nanddump -o -b -q -f /tmp/u-boot.bin.nand /dev/mtd1
299                 uboot_nand_ver=`strings /tmp/u-boot.bin.nand | grep 'U-Boot 20' | head -n 1 | sed 's/ /_/g'`
300                 uboot_ver=`strings u-boot.bin | grep 'U-Boot 20' | head -n 1 | sed 's/ /_/g'`
301                 uboot_size=`stat -c %s u-boot.bin`
302
303                 dd if=/tmp/u-boot.bin.nand of=/tmp/u-boot.bin.nand.cmp bs=$uboot_size count=1
304                 if ! cmp u-boot.bin /tmp/u-boot.bin.nand.cmp
305                 then
306                         uboot_need_to_ask=true
307                         for oldver in $oldubootvers;
308                         do
309                                 if [ "$oldver" = "$uboot_nand_ver" ];
310                                 then
311                                         uboot_need_to_ask=false
312                                         break
313                                 fi
314                         done
315
316                         if $uboot_need_to_ask;
317                         then
318                                 if zenity --question --title="Custom u-boot?" --text="$CUSTOM_UBOOT_INSTALLED_TEXT" --ok-label="Yes" --cancel-label="No";
319                                 then
320                                         update_uboot
321                                 else
322                                         log "u-boot update skipped\n(nand has $uboot_nand_ver)"
323                                 fi
324                         else
325                                 update_uboot
326                         fi
327                 else
328                         log "u-boot already up-to-date\n($uboot_nand_ver)"
329                 fi
330         fi
331
332
333         echo "60"
334         echo "# Preparing OS update."
335
336
337         #
338         # Update IPKs.
339         #
340
341         # call the function for installing ipks
342         # $1 path for packages
343         # $2 start percentage for zenity (int!)
344         # $3 end percentage for zenity (int!)
345         # $4 step width for zenity ($4 percent point per gui update / opkg run)
346         # $5 message displayed in zenity during the update
347         install_all_ipk_in_folder packages/other 60 80 6 "# Updating OS... this will take a while."
348
349         # Install those packages that come from openpandora.org seperately at the end to make sure that the correct config files are replaced.
350         
351         # call the function for installing ipks
352         # $1 path for packages
353         # $2 start percentage for zenity (int!)
354         # $3 end percentage for zenity (int!)
355         # $4 step width for zenity ($4 percent point per gui update / opkg run)
356         # $5 message displayed in zenity during the update
357         install_all_ipk_in_folder packages/pandora 80 95 6 "# Updating OS... this will take a while."
358
359         log "Packages updated"
360
361         echo "95"
362         echo "# Finalizing update"
363
364         rm -r /etc/rc*
365         cp -r /tmp/rcsave/* /etc/
366         rm -R /tmp/rcsave
367
368
369         if [ -f /etc/rc5.d/S20apmd ];
370         then
371                 update-rc.d -f xinetd remove
372                 update-rc.d -f avahi-daemon remove
373                 update-rc.d -f apmd remove
374                 update-rc.d -f banner remove
375                 update-rc.d -f portmap remove
376                 update-rc.d -f blueprobe remove
377                 update-rc.d -f pandora-lcd-state remove
378                 update-rc.d -f pandora-state start 39 S . stop 31 0 1 6 .
379         fi
380
381         chmod 666 /etc/pointercal  
382         user=$(cat /tmp/currentuser)
383         cp /etc/skel/.vimrc /home/$user/.vimrc --no-clobber
384         chown $user:$user /home/$user/.vimrc
385
386         sync
387         log "Final scripts finished"
388
389         echo "100"
390         echo "# Update finished"
391
392         ) |
393         # Zenity Progress goes here
394         zenity --progress \
395                 --title="Installing Updates..." \
396                 --text="Updating System..." \
397                 --percentage=0
398
399         # check for erros and display the final "I am about to be done" message
400         err=`cat /tmp/updater_err.log`
401         if [ -z "$err" ]; then
402                 err="Your system has been updated and will now reboot."
403         fi
404         update=$(cat /tmp/updater.log)
405         zenity --info --title="Update finished" --text "$err\n\n${update}"
406
407
408         #if [ -f /tmp/updreboot ];
409         #then
410         #       reboot=`cat /tmp/updreboot`
411         #       zenity --info --title="You need to reboot" --text "${reboot}"
412         #       rm /tmp/updreboot
413         #fi
414         reboot
415 fi