xref: /aosp_15_r20/external/toybox/mkroot/packages/plumbing (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/echo run this from "make root"
2
3# Plumbing to download files
4
5[ -z "$ROOT" ] && echo "no" && exit 1
6mkdir -p "${DOWNLOAD:=$PWD/root_download}" || exit 1
7
8### Functions to download, extract, and clean up after source packages.
9
10# Usage: download HASH URL
11# Grabs source from URL confirming SHA1 hash (Basically "wget $2")
12# If extracted source is in $DOWNLOAD (no version) build will use that instead
13download() {
14  local FILE="$(basename "$2")" WGET=wget
15  [ -d "$DOWNLOAD/${FILE/-*/}" ] && echo "$FILE" local && return 0
16  X=0; while true; do
17    [ "$(sha1sum < "$DOWNLOAD/$FILE" 2>/dev/null)" == "$1  -" ] &&
18      echo "$FILE" confirmed && break
19    rm -f $DOWNLOAD/${FILE/-[0-9]*/}-[0-9]* || exit 1
20    [ $X -eq 0 ] && X=1 || [ $X -eq 1 ] && X=2 WGET=/usr/bin/wget || exit 1
21    $WGET "$2" -O "$DOWNLOAD/$FILE"
22  done
23}
24
25# Usage: setupfor PACKAGE
26# Extracts source tarball (or snapshot a repo) to create disposable build dir.
27# Basically "tar -xvz -C $TEMP -f $DOWNLOAD/$1.tar.gz && cd $NEWDIR"
28setupfor() {
29  PACKAGE="$(basename "$1")"
30  announce "$PACKAGE" && cd "$TEMP" && rm -rf "$PACKAGE" || exit 1
31  if [ -d "$DOWNLOAD/$PACKAGE" ]; then
32    cp -la "$DOWNLOAD/$PACKAGE/." "$PACKAGE" && cd "$PACKAGE" || exit 1
33  else
34    local DIR=$(mktemp -dp.)
35    tar xvafC "$DOWNLOAD/$PACKAGE"-*.t* "$DIR" &&
36    mv "$DIR"/* "$PACKAGE" && rmdir "$DIR" && cd "$PACKAGE" || exit 1
37  fi
38}
39
40# Usage: cleanup
41# Delete setupfor's dir, exiting if build failed (basically "rm -rf $PACKAGE")
42cleanup() {
43  [ $? -ne 0 ] && exit 1
44  [ -z "$PACKAGE" ] && exit 1
45  [ ! -z "$NO_CLEANUP" ] && return
46  cd .. && rm -rf "$PACKAGE"* || exit 1
47}
48