[Miscellaneous Android Development] surfer63:
JDfense mentioned that he couldn't get this script to work no matter what he tried. We had some private discussion about this. So after my holiday (last week) I did all kind of checks and tests and it turns out that this script won't work on most roms for the following reasons:

the standard android shell (/bin/sh) is a very simple one and doesn't support the "if [ <some statement> ]" syntax.

The archos busybox version I use in my roms does support these statements, but some of the other busybox binaries I tried the last few days from several roms don't support it.

The above script will not work, neither in my roms, as I started it with #!/bin/sh. I did this in this topic to make it as general as possible. However all my real scripts always start with "#!/system/bin/busybox sh". I always use the busybox shell command interpreter as it has a more extensive set than the minimalistic android sh shell. So if I hadn't tried to be so "general as possible" and just had left the #!/system/bin/busybox sh (mind the path where your own busybox is though) it might have worked if your busybox supported the "if [ <some statement> ]" syntax.

Now what do we need to do to make it work on every shell interpreter? We need to choose a syntax that every shell interpreter understands which means that we need to use the case/esac syntax (as you all already knew off course :D)

We need to rewrite the script as follows:

#!/bin/sh
busybox chmod 777 data/app
for i in /system/install/*.apk;
do
apkonly=`busybox basename $i`
ls -l /data/app/$apkonly
# if it exists it will be displayed, otherwise an error is generated
case "$?" in
0)
echo "nothing to do. apk already in place"
;;
*)
# everything apart from 0 is an error code
busybox cp $i /data/app
busybox chmod 644 /data/app/$apkonly
esac
done

Note: I used the simple #!/bin/sh android shell on purpose as it should work with every shell interpreter.

Now this script still has a disadvantage! In this way apps are installed as non-system apps as they are copied to the /data/app folder which makes it possible for the user to uninstall them in case he/she doesn't want them.

However, upon every reboot the script checks whether the apk exists in the /data/app folder. This means that if a user uninstalled an apk, the script will reinstall it again on the next boot. Bit frustrating for the user I assume. :)

So we could also do with a much simpler check/script:

#!/bin/sh
ls -l /data/apps_installed
case "$?" in
0)
echo "Nothing to do. file exists"
;;
*)
echo "file doesn't exist. Start copying"
cp /system/install/*.apk /data/app
busybox touch "/data/apps_installed" ;
esac

This checks upon startup for the (0 byte) file "apps_installed" in /data. If it exists we obviously did our copy action. If it doesn't exist (first boot after flash) it will copy the apks and then create the file.

= Another option, possibly from another folder.

Some of the applications don't support the simple "copy to" action. They really need to be installed. This can be done from the terminal/shell with an option.

However: Use this with great care as you can brick your tablet during startup which means that you have to reflash/unbrick it.

Some apks can be simply installed without further interference. Some apks want user interaction. In a boot script this will cause your tablet to hang as you have no possibility to react via the keyboard/screen.

The script would look like this:

#!/bin/sh
ls -l /data/apps_installed
case "$?" in
0)
echo "Nothing to do. file exists"
;;
*)
echo "file doesn't exist. Start copying"
for i in /system/install/*.apk;
do
apkonly=`busybox basename $i`
pm install $i /data/app/$apkonly
done
busybox touch "/data/apps_installed" ;
esac

note the "pm install $i": That one does the trick (or it does the brick :D).

note 2: I can't use a simple script, like the previous copy script, as an install has to be done per apk. I now used the same source folder (/system/install) but you could also use something like /system/copy and /system/install. Installing an apk is much slower then copying an apk.

note 3: If you want to check the "pm install some.apk" from a terminal, make sure your first go into root mode in the terminal. A rooted rom doesn't mean that your are immediately root in a terminal. So first issue the "su" command. This will bring you in root mode (actually a second shell as root).

EDIT: Updated the last script. It turns out that if you have a writable /system (like with aufs.ko) you do need to specify the target folder and app name. So I added the apkonly function together with a "pm ..." target specification.

After all: I think I will copy this in a wiki article as it is a long post and I'm sure more rom builders also ran into this issue.