[Android] lotherius:
I recently have started releasing Roms on the G2X. Coming from the Vibrant world where there were so many leaks and versions, that everyone forced wipes on updates, I was shocked when many roms were No-Wipe... Just seemed crazy.

Then on top of that, I realized I had to deal with the fact that custom kernels were ext4 while the stock kernel for the base I was using was ext3.

So I needed a script that could figure out what Rom was currently installed. The requirements:

1) Figure out what Rom is currently installed.

2) WIPE data and convert to ext3 if a non-compatible Rom is installed.

3) DO NOT WIPE data if a compatible Rom is already installed.

4) Install ext3 kernel ONLY if we wiped the partitions

5) Leave existing kernel alone if user already has compatible Rom

6) Backup files that are tied to kernel version (on LG, it's wireless.ko)

I searched high and low, and couldn't find any updater scripts that actually did this. The best I could find was this very useful post: http://xdaforums.com/showthread.php?t=974450 that showed how to use ifelse and file_getprop. He has a lot of good ideas, but what was missing was an actual full updater-script that could be used.

Also, I needed to be able to backup a file from /system and deal with what happens if that file didn't actually exist.

So I wrote wrote my own updater script.

The basic process:

1: Extract needed tools from updater zip to /tmp

2: Run a shell script (tsugib.sh). It copies default.prop to /tmp/build.prop (just a kludge in case the next step fails).

3: Script then copies /system/build.prop to /tmp/build.prop. This is so we can still acess build.prop AFTER formatting /system

4: Control returns to edify, where we use an ifelse statement reading the properties with file_getprop to determine whether or not to wipe.

5: Normal install procedes.

6: Instead of just installing the kernel, we check the backed up build.prop again.

7: If keeping existing kernel, run a shell script (tsugir.sh) to restore the kernel library that was backed up.

8: If not keeping the kernel, then we install a new kernel.

Examples - Keep in mind, these scripts are written specifically for an LG G2X... you will need to modify them to work with your particular phone. In particular, I've REMOVED all sections that symlink files or set permissions. I have commented fully, so hopefully you can see how to integrate this with your phone's own version of updater-scripts.

tusgib.sh - this script does the backing up of build.prop and the kernel-linked wireless.ko file for later restoral if we don't flash a new kernel.

I'm sure I could have also checked to see if build.prop actually exists, before placing the dummy file, but why have a conditional statement if I can just place the dummy file (which won't match on my file_getprop anyway). We have to place the dummy file first, because if the user has formatted /system, then Edify will abort when attempting the file_getprop.

#!/sbin/sh
#
# Backup current install files.
# Kludgy hack to prevent having to replace kernel
#
cp /system/lib/modules/wireless.ko /tmp/wireless.ko
#
# Kludge since edify breaks if build.prop does not exist.
cp /default.prop /tmp/build.prop
#
# now if there *is* a build.prop, we'll overwrite the default.prop with the correct values.
#
cp /system/build.prop /tmp/build.prop
exit 0

tsugir.sh

I use this file to restore the wireless.ko file which is linked to kernel version. I have to restore the file since it is located in /system, which is wiped regardless of whether we wiped /data. Again, Rom specific. You may not even need this step if you have no files that are linked to kernel version.

#!/sbin/sh
#
# Restore current wireless.ko
# Kludgy hack to prevent having to replace kernel
#
cp /tmp/wireless.ko /system/lib/modules/wireless.ko
exit 0

And, here is the sample updater-script.

#
# Splash Message for the User
#
ui_print("");
ui_print("Tsugi 2.3.4 Smart Updater");
ui_print("");

show_progress(0.500000, 0);
ui_print("Preparing to Install...");

#
# LG G2XPartition Info
#
# Highly recommended to research and put your own phone's partition info here
# so you have a handy reference when editing updater-script.
#
# op7 = /data,	- pillage on full wipe
# op1 = /system, - pillage only with full update (doesn't require wipe tho)
# op2 = /cache, - always pillage to avoid issues.
# op8 = /emmc, 1p1 = /sdcard - don't pillage unless u like mad users
#
# Kludges to make this a smart updater.
# 1: Backup build.prop so it can be accessed after installing /system
# 2: Check if Tsugi is already installed
# - NO: Perform FULL WIPE and INSTALL NEW KERNEL
# - YES: DO NOT WIPE, backup and RESTORE WIRELESS.KO
#

ui_print(" Checking Existing Install...");
# extract needed tools
package_extract_file("system/bin/tsugib.sh", "/tmp/tsugib.sh");
package_extract_file("system/bin/tsugir.sh", "/tmp/tsugir.sh");
set_perm(0, 0, 0777, "/tmp/tsugib.sh");
set_perm(0, 0, 0777, "/tmp/tsugir.sh");

#
# backup files to keep
# backs up current build.prop to /tmp so it can be checked
# /system must be mounted for this.
#
run_program("/sbin/busybox", "mount", "/system");
run_program("/tmp/tsugib.sh");
#
# Now unmount system since having it mounted will
# break formatting (if we format).
#
unmount("/system");

#
# Since I didn't include a CLEAR way to identify my Rom in early versions,
# I'll identify it by the LG release version for now
# but this may change in the future
#
# Any value can be checked here, such as ro.modversion or ro.developer or any that you just
# add to the file yourself.
#

ifelse(
file_getprop("/tmp/build.prop", "ro.build.lge.version.release") == "LG-L95G-V10c_final",
(
ui_print("Compatible Rom Detected. Not wiping.");

),
(
ui_print("Incompatible Rom. Wiping.");
run_program("/sbin/mke2fs", "/dev/block/mmcblk0p7");
run_program("/sbin/tune2fs", "-j", "/dev/block/mmcblk0p7");
run_program("/sbin/mke2fs", "/dev/block/mmcblk0p1");
run_program("/sbin/tune2fs", "-j", "/dev/block/mmcblk0p1");
run_program("/sbin/mke2fs", "/dev/block/mmcblk0p2");
run_program("/sbin/tune2fs", "-j", "/dev/block/mmcblk0p2");
)
);

#
# On LG this does not wipe user data.
# Just the cache
# Make sure you have the correct paths.
#
ui_print(" Clearing dalvik");
run_program("/sbin/busybox", "mount", "/data");
delete_recursive("/data/local");
delete_recursive("/data/dalvik-cache");

#
# Completely wipes /system partition, but this leaves user data alone.
#
ui_print(" Clearing system");
run_program("/sbin/busybox", "mount", "/system");
delete_recursive("/system");
#
# I found that if I don't wipe /cache there can sometimes be issues.
#
ui_print(" Clearing cache");
run_program("/sbin/busybox", "mount", "/cache");
delete_recursive("/cache");

#
# Normal rom extraction...
#
ui_print("Extracting new Rom...");
package_extract_dir("system", "/system");

#
# IN THIS SECTION YOU WOULD SYMLINK YOUR TOOLBOX,
# BUSYBOX and ANY OTHER TOOLS YOU NEED.
#
#
show_progress(0.500000, 40);

ui_print(" Linking Tools...");
# many many lines removed for demonstration purposes.

#
# Extract any data (such as preinstalled apps that user can uninstall)
#
ui_print("Extracting Data....");
package_extract_dir("data", "/data");

ui_print(" Setting Permissions....");
#
# Much setting of permissions removed for demo purposes.
#

#
# Extract anything to the user's SDCard such as ringtones, etc
#
package_extract_dir("sdcard", "/sdcard");

show_progress(0.200000, 0);

#
# If coming from compatible Rom, then
# don't touch kernel. Kernel will remain as user had it. Restore wireless.ko from backup.
#
# If incompatible Rom, then install new Kernel.
# We check the BACKED UP build.prop since the already installed one would NOW match which
# may not be correct.
#
# Use whatever method your rom uses for installing the kernel. It may be very different.
#

ifelse(
file_getprop("/tmp/build.prop", "ro.build.lge.version.release") == "LG-L95G-V10c_final",
(
ui_print("Leaving Existing Kernel.");
run_program("/tmp/tsugir.sh");
),
(
ui_print("Installing LG Kernel.");
package_extract_file("boot.img", "/tmp/boot.img");
run_program("/sbin/busybox", "dd", "if=/dev/zero", "of=/dev/block/mmcblk0p5");
run_program("/sbin/busybox", "dd", "if=/tmp/boot.img", "of=/dev/block/mmcblk0p5");
delete("/tmp/boot.img");
)
);

#
# Now do any post-kernel install cleanup.
#
ui_print(" Cleaning Up....");

unmount("/data");
unmount("/system");
unmount("/cache");

ui_print(" ");
show_progress(0.100000, 0);
ui_print(" ");
ui_print("Injection Complete");
ui_print(" ");
ui_print("You may now #OCCUPY your G2X.");

Also it may be possible that some updater binaries don't support ifelse. I haven't heard of any that don't, but I always see the warning that it may not be supported.

If you need to do updater-script troubleshooting, you can adb into recovery and should be able to view the recovery log in the /tmp directory.

Now, keep in mind.. this updater-script is based on LG G2X partitions and install techniques. Your Rom may do it very differently, but as long as your updater binary and version of recovery supports all the needed features (mine did), then you can use a similar method on your own updater-script.