#!/usr/bin/env bash
# Fixing 'adb restore' for devices with Android 7 (Nougat) and above
# Some of them don't restore a backup if the app itself isn't yet installed
# This script works around that by extracting and installing the APK
# before calling 'adb restore'
opts='s:h'

ADBOPTS=
HELP=0
while getopts $opts arg; do
  case $arg in
    :) echo "$0 requires an argument:"; exit 1 ;;
    s) if [[ -n "$(adb devices | grep $OPTARG)" ]]; then
         ADBOPTS="-s $OPTARG"
       else
         echo "Device with serial $OPTARG is not present."
         exit 2
       fi ;;
    h) HELP=1 ;;
  esac
done
shift $((OPTIND-1))


[[ -z "$1" || $HELP -gt 0 ]] && {
  echo -e "\n\033[1;37mabrestore\033[0m"
  echo "Working around 'adb restore' issues on Nougat and above"
  echo
  echo "Syntax:"
  echo -e "  $0 [-s <serial>] <ADB Backup File>\n"
  echo "Example:"
  echo -e "  $0 com.foo.bar.ab\n"
  echo -e "  $0 -s 12345abc com.foo.bar.ab\n"
  exit 1
}
pkgname="${1%%.ab}"

if [[ "$(ldd "$(which openssl)" | grep '^[[:space:]]*libz\.')" ]]; then #"
    unzlib="openssl zlib -d"
else
    [[ -z "$(which zlib-flate)" ]] && {
      echo "Cannot find openssl, or it is not configured with zlib support."
      echo "zlib-flate cannot be found either, so we cannot uncompress the backup."
      echo "On Linux, zlib-flate is part of the qpdf package."
      echo
      exit 5
    }
    unzlib="zlib-flate -uncompress"
fi

# Getting the APK file
dd if="${1}" bs=24 skip=1 2>/dev/null | $unzlib | gzip -9 -c > "${pkgname}".tar
tar -xzf "${pkgname}.tar" --wildcards '*.apk' --strip-components=3
mv base.apk ${pkgname}.apk
rm -f "${pkgname}".tar

# Running Install and Restore
adb $ADBOPTS install ${pkgname}.apk
[[ $? -eq 0 ]] && rm ${pkgname}.apk
adb $ADBOPTS restore $1
