[mkbundle] Fix mapping length (#13061)
[mono-project.git] / sdks / builds / unzip-android-archive.sh
blob14739bb227b43150991358156dbcac608ae46b53
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
19 # Special case archives - archives which have no single root directory when unpacked and thus
20 # the workaround below doesn't work for them.
21 # Entry format: grep regex matching the END of path in $1 (no trailing $ necessary)
23 SPECIAL_CASE_ARCHIVES="
24 cmake-.*\.zip
27 HAVE_SPECIAL_CASE=no
28 for sac in $SPECIAL_CASE_ARCHIVES; do
29 if echo $1 | grep "${sac}$" > /dev/null 2>&1; then
30 HAVE_SPECIAL_CASE=yes
31 break
33 done
35 if [ "$HAVE_SPECIAL_CASE" == "no" ]; then
36 # This weird syntax is necessary because some zip archives do not contain a separate
37 # entry for the root directory but merely a collection of its subdirectories (for instance
38 # platform-tools). The very first entry in the archive is retrieved, then its path is read and
39 # finally the part up to the first '/' of the path is retrieved as the root directory of
40 # the archive. With platform-tools the last part is necessary because otherwise the root directory
41 # would end up being reported as `platform-tools/adb` as this is the first entry in the archive and
42 # there's no `platform-tools/` entry
43 ZIP_ROOT_DIR=$(unzip -qql "$1" | head -n1 | tr -s ' ' | cut -d' ' -f5- | tr '/' ' ' | cut -d' ' -f1)
46 # We need a temporary directory because some archives (emulator) have their root directory named the
47 # same as a file/directory inside it (emulator has emulator/emulator executable for instance) and
48 # moving such a file/directory to .. wouldn't work
49 ARCHIVE_TEMP_DIR=$(mktemp -d -t unzip_android_archive_XXXXXXXX)
51 unzip "$1" -d "$ARCHIVE_TEMP_DIR"
52 mkdir -p "$2"
54 if [ -z "$ZIP_ROOT_DIR" ]; then
55 mv -f "$ARCHIVE_TEMP_DIR"/* "$2"
56 else
57 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" ';'