#!/bin/bash
#CsHeng @2014.10.23

# get current shell absolute dir
CWD=$(cd "$(dirname "$0")"; pwd)
#CWD=$(dirname $(readlink -f $0))

SOURCEDIR=
ALIGNDIR=

echo ${SOURCEDIR}

while getopts :s:o: OPTION
do
    case ${OPTION} in
            s)
            SOURCEDIR=$OPTARG
            ;;
        	o)
            ALIGNDIR=$OPTARG
            ;;
        	\?)

            ;;
    esac
done

[ -z ${ALIGNDIR} ] && ALIGNDIR=${SOURCEDIR}/aligned
[ ! -d ${ALIGNDIR} ] && mkdir -p ${ALIGNDIR}

for APKFILE in `ls ${SOURCEDIR} | grep '.*apk$'`
do
echo ZipAlign ${SOURCEDIR}/${APKFILE}
zipalign 4 ${SOURCEDIR}/${APKFILE} ${ALIGNDIR}/${APKFILE}
rm ${SOURCEDIR}/${APKFILE} && mv ${ALIGNDIR}/${APKFILE} ${SOURCEDIR}/${APKFILE}
done
rm -d ${ALIGNDIR}
echo "done zipalign"
