1#!/bin/bash
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -ex
18
19function usage {
20  cat <<EOF
21Usage: $0 src_product target_product src_dir [dist_dir]
22
23Create artifacts for |target_product| from the artifacts of |src_product|.
24
25src_product
26        Product name to copy artifacts from. e.g. aosp_arm64
27
28target_product
29        Product name to copy artifacts to. e.g. aosp_arm64_pubsign
30
31src_dir
32        Directory containing source artifacts.
33
34dist_dir
35        Optional. If given then generate any artifact under dist_dir.
36        Otherwise generate under environment variable DIST_DIR.
37EOF
38}
39
40if [[ $# -lt 3 ]]; then
41  usage
42  exit 1
43fi
44
45SRC_PRODUCT="$1"
46TARGET_PRODUCT="$2"
47SRC_DIR="$3"
48
49if [[ $# -ge 4 ]]; then
50  DIST_DIR="$4"
51fi
52
53readonly SRC_PRODUCT
54readonly TARGET_PRODUCT
55readonly SRC_DIR
56readonly DIST_DIR
57
58if [[ -z "${DIST_DIR}" ]]; then
59  echo >&2 '$DIST_DIR is not specified'
60  exit 1
61fi
62
63# Create output directory if not already present
64mkdir -p "${DIST_DIR}" || true
65
66if [[ ! -d "${DIST_DIR}" ]]; then
67  echo >&2 'Cannot create $DIST_DIR or $DIST_DIR is non-existence'
68  exit 1
69fi
70
71# Show the artifacts to be copied in the log
72echo "Artifacts to copy:"
73find "${SRC_DIR}" || true
74echo
75
76# Don't copy logs/ and files whose name starts with $SRC_PRODUCT
77rsync --verbose --archive --copy-links --exclude='logs' \
78  --exclude='*.zip' "${SRC_DIR}/" "${DIST_DIR}"
79
80# Rename ${SRC_PRODUCT}-xxx.zip to ${TARGET_PRODUCT}-xxx.zip
81ZIP_PATHNAMES="$(find -L "${SRC_DIR}" -type f -name '*.zip')"
82
83echo "ZIP files to be copied and renamed:"
84echo "${ZIP_PATHNAMES}"
85echo
86
87for SRC_PATHNAME in ${ZIP_PATHNAMES} ; do
88  SRC_FILENAME="$(basename ${SRC_PATHNAME})"
89  TARGET_FILENAME="${SRC_FILENAME/${SRC_PRODUCT}/${TARGET_PRODUCT}}"
90  cp --verbose --archive --dereference "${SRC_PATHNAME}" "${DIST_DIR}/${TARGET_FILENAME}"
91done
92