@ECHO OFF
setlocal enabledelayedexpansion

set workdir=%~dp0
%workdir:~0,2%
cd %workdir%

set CPU_ABI=armeabi-v7a
set CPU_ABI2=armeabi

set logfile=apk2lib.log

if exist %logfile% del %logfile%

if exist lib rmdir /S /Q lib
mkdir lib

for %%s in (*.apk) do (
    for /f %%a in ('unzip -l "%%s" ^| gawk "{print $4}" ^| findstr "^lib/armeabi"') do (
        unzip -Ppassword -q -o "%%s" "%%a" -d lib
    )
    
    for /f %%a in ('aapt dump badging "%%s" ^| gawk -F "'" "/package: name/ {print $2}"') do (
        set pn=%%a
        mkdir .\lib\!pn!
        echo "%%s" package name: "!pn!" >> %logfile%
    )

    if exist lib\lib\%CPU_ABI% (
        copy lib\lib\%CPU_ABI%\* .\lib\!pn!

        for %%k in (lib\lib\%CPU_ABI%\*) do (
            echo extract "lib\%CPU_ABI%\%%k" from "%%s" >> %logfile%
        )
    ) else (
        if exist lib\lib\%CPU_ABI2% (
            copy lib\lib\%CPU_ABI2%\* .\lib\!pn!
            for %%k in (lib\lib\%CPU_ABI2%\*) do (
                echo extract "lib\%CPU_ABI2%\%%k" from "%%s" >> %logfile%
            )
        )
    )
    
    if exist lib\lib rmdir /S /Q lib\lib
    
    set f=%%s
    set nf=!f: =_!
    if not "!f!" == "!nf!" (
        rename "!f!" "!nf!"
        echo rename "!f!" to "!nf!" >> %logfile%
    )
)

for %%s in (app\*.apk) do (    
    set f=%%s
    set nf=!f: =_!
    if not "!f!" == "!nf!" (
        move "!f!" "!nf!"
        echo rename "!f!" to "!nf!" >> %logfile%
    )
)
