David Brazdil | c53ea95 | 2020-10-15 12:41:26 +0000 | [diff] [blame] | 1 | #!/usr/bin/env 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 | ## |
| 17 | |
| 18 | set -euo pipefail |
| 19 | |
| 20 | # Wrapper around 'expr' that handles the fact that it returns code 1 |
| 21 | # if the result is zero/null. That messes with 'set -e'. |
| 22 | function expr { |
| 23 | eval 'val=$($(which expr) $@); ret=$?' |
| 24 | if [ "$ret" != 0 -a "$ret" != 1 ] |
| 25 | then |
| 26 | return $ret |
| 27 | fi |
| 28 | echo "$val" |
| 29 | } |
| 30 | |
| 31 | ARGS=( "$@" ) |
| 32 | NUM_ARGS=${#ARGS[@]} |
| 33 | |
| 34 | POS_DIVIDER=-1 |
| 35 | for i in $(seq 0 $(expr $NUM_ARGS - 1)) |
| 36 | do |
| 37 | if [ "${ARGS[$i]}" == "--" ] |
| 38 | then |
| 39 | if [ "$POS_DIVIDER" -eq -1 ] |
| 40 | then |
| 41 | POS_DIVIDER=$i |
| 42 | else |
| 43 | echo "Multiple dividers in command line inputs" 1>&2 |
| 44 | exit 1 |
| 45 | fi |
| 46 | fi |
| 47 | done |
| 48 | |
| 49 | if [ "$POS_DIVIDER" -eq -1 ] |
| 50 | then |
| 51 | echo "Divider expected among command line inputs" 1>&2 |
| 52 | exit 1 |
| 53 | fi |
| 54 | |
| 55 | NUM_INPUT=${POS_DIVIDER} |
| 56 | NUM_OUTPUT=$(expr $NUM_ARGS - $POS_DIVIDER - 1) |
| 57 | |
| 58 | if [ "$NUM_INPUT" -ne "$NUM_OUTPUT" ] |
| 59 | then |
| 60 | echo "Number of inputs does not match number of outputs" 1>&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | for i in $(seq 0 $(expr $NUM_INPUT - 1)) |
| 65 | do |
| 66 | INPUT="${ARGS[$i]}" |
| 67 | OUTPUT="${ARGS[$NUM_INPUT + $i + 1]}" |
| 68 | mkdir -p "$(dirname "$OUTPUT")" |
| 69 | cp "$INPUT" "$OUTPUT" |
| 70 | done |