[sdks] Fix typo + Fix verbosity of unzip-android-archive.sh (#8245)
[mono-project.git] / sdks / builds / unzip-android-archive.sh
blob70c111c0dc987c5447a9721dcc203bab0d9bf4b1
1 #!/usr/bin/env bash
3 set -xe
5 function cleanup()
7 if [ -n "$ARCHIVE_TEMP_DIR" ]; then
8 rm -rf "$ARCHIVE_TEMP_DIR"
9 fi
12 trap cleanup 0
14 ## Parameters:
15 # $1: path to source archive
16 # $2: destination directory
18 # This weird syntax is necessary because some zip archives do not contain a separate
19 # entry for the root directory but merely a collection of its subdirectories (for instance
20 # platform-tools). The very first entry in the archive is retrieved, then its path is read and
21 # finally the part up to the first '/' of the path is retrieved as the root directory of
22 # the archive. With platform-tools the last part is necessary because otherwise the root directory
23 # would end up being reported as `platform-tools/adb` as this is the first entry in the archive and
24 # there's no `platform-tools/` entry
25 ZIP_ROOT_DIR=$(unzip -qql "$1" | head -n1 | tr -s ' ' | cut -d' ' -f5- | tr '/' ' ' | cut -d' ' -f1)
27 # We need a temporary directory because some archives (emulator) have their root directory named the
28 # same as a file/directory inside it (emulator has emulator/emulator executable for instance) and
29 # moving such a file/directory to .. wouldn't work
30 ARCHIVE_TEMP_DIR=$(mktemp -d)
32 unzip "$1" -d "$ARCHIVE_TEMP_DIR"
33 mkdir -p "$2"
34 find "$ARCHIVE_TEMP_DIR/$ZIP_ROOT_DIR/" -maxdepth 1 -not \( -name "$ZIP_ROOT_DIR" -and -type d \) -and -not -name . -and -not -name .. -exec mv -f '{}' "$2" ';'