#!/system/bin/sh
#
# Script to reinstall all apps that have libraries.
# useful after a wipe data when using SwitchSD (S2SD)
#
# Usage: applibs2data
#
# Author: Michelasso @ androidiani.com
#
# Version: 1.0

# Check if S2SD directories are mounted
if [ "$(mount | grep /data/app)" == "" ] || [ "$(mount | grep /sd-ext)" == "" ]; then
	echo "This script requires Switch2SD (S2SD) to be installed and running at boot time" 
	echo "Exiting..."
	exit 2
fi

# reinstall all apps missing at least one armeabi library
echo "Reinstalling applications missing local libraries"
cd /data/app
for app in $(ls *.apk); do
	appdir=$(echo ${app%-*})
#
# Check list of libraries for all installed applications
#
	for pathlib in $(unzip -l $app | grep 'lib/armeabi/' | grep '.so' | awk '{print $NF}'); do
		lib=/data/data/$appdir/lib/$(basename $pathlib)
		if [ -e $lib ]; then
			echo Library $lib already installed
		else
#
# Re-install the application. The libraries will be extracted in the app's data
#
			pm install -r -f $app
			break
		fi
	done
done
