#!/system/bin/sh
# Automatic ZipAlign by Wes Garner
# ZipAlign files in /data that have not been previously ZipAligned (using md5sum)
# Thanks to oknowton for the changes

# Changelog:
# 1.1 (12/1/09) Switched to zipalign -c 4 to check the apk instead of MD5 (oknowton)
# 1.0 (11/30/09) Original

LOG_FILE=/data/repack.log
    if [ -e $LOG_FILE ]; then
    	rm $LOG_FILE;
    fi;
    	
echo "Starting Automatic Repack $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
    for apk in *.apk ; do
        base_apk=`basename $apk .apk`
        mkdir $base_apk
        cd $base_apk
        unzip ../$apk
        zip -r -0 $base_apk.zip *
        cp $base_apk.zip ../$apk
        cd ..
        rm -rf $base_apk
	zipalign -c 4 $apk;
	ZIPCHECK=$?;
	if [ $ZIPCHECK -eq 1 ]; then
		echo ZipAligning $base_apk  | tee -a $LOG_FILE;
		zipalign -f 4 $apk /cache/$base_apk;
			if [ -e /cache/$base_apk ]; then
				cp -f -p /cache/$base_apk $apk  | tee -a $LOG_FILE;
				rm /cache/$base_apk;
			else
				echo ZipAligning $base_apk Failed  | tee -a $LOG_FILE;
			fi;
	else
		echo ZipAlign already completed on $apk  | tee -a $LOG_FILE;
	fi;
       done;
echo "Automatic repack finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
