blob: f0a96036a4908dfea907b6721568e2010ad9b377 [file] [log] [blame]
David Brazdilc53ea952020-10-15 12:41:26 +00001#!/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
18set -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'.
22function 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
31ARGS=( "$@" )
32NUM_ARGS=${#ARGS[@]}
33
34POS_DIVIDER=-1
35for i in $(seq 0 $(expr $NUM_ARGS - 1))
36do
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
47done
48
49if [ "$POS_DIVIDER" -eq -1 ]
50then
51 echo "Divider expected among command line inputs" 1>&2
52 exit 1
53fi
54
55NUM_INPUT=${POS_DIVIDER}
56NUM_OUTPUT=$(expr $NUM_ARGS - $POS_DIVIDER - 1)
57
58if [ "$NUM_INPUT" -ne "$NUM_OUTPUT" ]
59then
60 echo "Number of inputs does not match number of outputs" 1>&2
61 exit 1
62fi
63
64for i in $(seq 0 $(expr $NUM_INPUT - 1))
65do
66 INPUT="${ARGS[$i]}"
67 OUTPUT="${ARGS[$NUM_INPUT + $i + 1]}"
68 mkdir -p "$(dirname "$OUTPUT")"
69 cp "$INPUT" "$OUTPUT"
70done