1#!/bin/bash -e
2
3# Copyright 2017 Google Inc. All rights reserved.
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# Script that takes a list of files on stdin and converts it to arguments to jar on stdout
18# Usage:
19#        find $dir -type f | sort | jar-args.sh $dir > jar_args.txt
20#        jar cf out.jar @jar_args.txt
21
22case $(uname) in
23  Linux)
24    extended_re=-r
25    ;;
26  Darwin)
27    extended_re=-E
28    ;;
29  *) echo "unknown OS:" $(uname) >&2 && exit 1;;
30esac
31
32if [ "$1" == "--test" ]; then
33  in=$(mktemp)
34  expected=$(mktemp)
35  out=$(mktemp)
36  cat > $in <<EOF
37a
38a/b
39a/b/'
40a/b/"
41a/b/\\
42a/b/#
43a/b/a
44EOF
45  cat > $expected <<EOF
46
47-C 'a' 'b'
48-C 'a' 'b/\\''
49-C 'a' 'b/"'
50-C 'a' 'b/\\\\'
51-C 'a' 'b/#'
52-C 'a' 'b/a'
53EOF
54  cat $in | $0 a > $out
55
56  if cmp $out $expected; then
57    status=0
58    echo "PASS"
59  else
60    status=1
61    echo "FAIL"
62    echo "got:"
63    cat $out
64    echo "expected:"
65    cat $expected
66  fi
67  rm -f $in $expected $out
68  exit $status
69fi
70
71# In order, the regexps:
72#   - Strip $1/ from the beginning of each line, and everything from lines that just have $1
73#   - Escape single and double quotes, '#', ' ', and '\'
74#   - Prefix each non-blank line with -C $1
75sed ${extended_re} \
76  -e"s,^$1(/|\$),," \
77  -e"s,(['\\]),\\\\\1,g" \
78  -e"s,^(.+),-C '$1' '\1',"
79