#!/bin/bash
DIRNAME=$(dirname "$(readlink -e "$0")")
DIR=$(cd "$DIRNAME" || exit 112; pwd)

if ! test -f "$1" ; then
  echo "syntax: unapk [apk file]"
  exit 0
fi

filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"

DIR_TEMP="./${filename}.temp"
DIR_RESULT="./${filename}.decompiled"
TIMEOUT=200s

mkdir -pv $DIR_RESULT
mkdir -pv $DIR_TEMP

filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
       
unzip -o $1 -d $DIR_TEMP >/dev/null
         
for i in ${DIR_TEMP}/*.dex;do
    basen=$( basename $i );
    timeout -k $TIMEOUT $TIMEOUT $DIR/dex2jar -o ${DIR_TEMP}/${basen}.jar ${i}
    timeout -k $TIMEOUT $TIMEOUT $DIR/unjar -od ${DIR_TEMP}/${basen}.jar.sources ${DIR_TEMP}/${basen}.jar
    mv ${DIR_TEMP}/${basen}.jar.sources $DIR_RESULT/
done

rm -rf $DIR_TEMP



